Naming Thread and Current Thread


Naming Thread

The Thread class provides methods to change and get the name of a thread. By default, each thread has a name i.e. thread-0, thread-1 and so on. By we can change the name of the thread by using setName() method. The syntax of setName() and getName() methods are given below:

  1. public String getName(): is used to return the name of a thread.
  2. public void setName(String name): is used to change the name of a thread.

Example of naming a thread

Flash Soon


Output:

Current Thread

The currentThread() method returns a reference of currently executing thread.

Flash Soon


Example of currentThread() method

Flash Soon


Output:

Copyright 2017 Design& Development