get and set name of a current thread

We can get and set name of thread by using following two methods of Thread class.

  • public final string getName()
  • public final void setName(String name)

currentThread().getName()

Method to get name of current thread.
Every thread in java have some name provided by JVM as Thread-0,Thread-1……Thread-n.

Syntax:

Example to get a name of current thread

Output

currentThread().setName()

Method to set name of a thread. We can change name of current thread as follow. We also can change name of “main” thread.

Syntax:

Example to change name of current thread

Output

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.