Naming Thread and Current ThreadNaming ThreadThe 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 the setName() method. The syntax of setName() and getName() methods are given below: We can also set the name of a thread directly when we create a new thread using the constructor of the class. Example of naming a thread : Using setName() MethodFileName: TestMultiNaming1.java Test it NowOutput: Name of t1:Thread-0 Name of t2:Thread-1 After changing name of t1:Sonoo Jaiswal running... running... Example of naming a thread : Without Using setName() MethodOne can also set the name of a thread at the time of the creation of a thread, without using the setName() method. Observe the following code. FileName: ThreadNamingExample.java Output: Thread - 1: JavaTpoint1 Thread - 2: JavaTpoint2 The thread is executing.... The thread is executing.... Current ThreadThe currentThread() method returns a reference of the currently executing thread. Example of currentThread() methodFileName: TestMultiNaming2.java Test it NowOutput: Thread-0 Thread-1 Next TopicThread Priority |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India