What is the Thread.toString() method in Java?
Overview
This Thread.toString() method is used to extract the thread’s name, priority, and thread group as a string. It does not take any argument value.
Note: The
Threadclass in Java helps to tackle system-designed and user-defined threads.
Syntax
// Signature
String toString()
Return value
It returns a String demonstration of this thread containing the thread’s name, priority and thread group.
Example
// Load Thread classimport java.lang.Thread;// Main classpublic class Main extends Thread {public void run() {System.out.println( "Thread Name : "+ Thread.currentThread().getName()+ " " + Thread.currentThread().toString());}public static void main(String args[]) {// creating threadsMain thread1=new Main();Main thread2=new Main();// call run() methodthread1.start();thread1.setName("thread1");thread2.start();thread2.setName("thread2");}}
Explanation
In the code above, we have two threads, thread1 and thread2. The start() creates threads and calls run() individually to start each thread execution. Finally, toString() in run() prints current thread resources as discussed above as strings.
- Line 7: We call
Thread.getName()to extract the current thread name. - Line 8: We call the
Thread.toString()method to get the current thread string representation. - Line 12–13: We create threads
thread1&thread2. - Line 15–18: We call the
start()function to start thread execution. Thestart()function invokes therun()function from theThreadclass.
Output
It will print thread1 and thread2 name, priority, and thread group between square brackets.
Threads with different priorities
// Load Thread classimport java.lang.Thread;// Main classpublic class Main extends Thread {public void run() {System.out.println( "Thread Name: "+ Thread.currentThread().getName()+ "\n\t" + Thread.currentThread().toString());}public static void main(String args[]) {// creating three threadsMain thread1= new Main();Main thread2= new Main();Main thread3= new Main();// call run() method// first thread with maximum prioritythread1.start();thread1.setName("thread1");thread1.setPriority(MAX_PRIORITY);// second thread with minimum prioritythread2.start();thread2.setName("thread2");thread2.setPriority(MIN_PRIORITY);// third thread with default prioritythread3.start();thread3.setName("thread3");}}
Explanation
In the above code snippet, we have three threads named thread1, thread2, and thread3 with different priorities MAX_PRIORITY, MIN_PRIORITY, and default priority, respectively. The toString() method is invoked to get current thread resources.
Note: To learn more about priorities, click here.
Line 8: We call
toString()to extract the current thread name, priority in queue, and thread group name.Line 12–14: We create three threads named
thread1,thread2, andthread3.Line 17: We call
start()to initiatethread1execution. It invokesrun()to performthread1tasks.Line 18: The command
thread1.setName()changes its default name tothread1.Line 19: The command
thrlead1.setPriority()changes its priority to .MAX_PRIORITY Value=10
Line 21: We call
start()to initiatethread2execution. It invokesrun()to performthread2tasks.Line 22: The command
thread2.setName("thread2")will change its default name tothread2.Line 23:
thread2.setPriority("thread2")changes its priority to .MIN_PRIORITY Value=1
Line 25: We call
start()to initiatethread3execution. It invokesrun()to performthread3tasks with .default priority Value=5 Line 26:
thread3.setName("thread3")will change its default name tothread3.
Output
The above code prints thread1, thread2, and thread3 names, priorities, and thread groups between square brackets to the console.