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
Thread
class in Java helps to tackle system-designed and user-defined threads.
// Signature
String toString()
It returns a String
demonstration of this thread containing the thread’s name, priority and thread group.
// 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");}}
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.
Thread.getName()
to extract the current thread name.Thread.toString()
method to get the current thread string representation.thread1
& thread2
.start()
function to start thread execution. The start()
function invokes the run()
function from the Thread
class.It will print thread1
and thread2
name, priority, and thread group between square brackets.
// 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");}}
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
, and thread3
.
Line 17: We call start()
to initiate thread1
execution. It invokes run()
to perform thread1
tasks.
Line 18: The command thread1.setName()
changes its default name to thread1
.
Line 19: The command thrlead1.setPriority()
changes its priority to
Line 21: We call start()
to initiate thread2
execution. It invokes run()
to perform thread2
tasks.
Line 22: The commandthread2.setName("thread2")
will change its default name to thread2
.
Line 23: thread2.setPriority("thread2")
changes its priority to
Line 25: We call start()
to initiate thread3
execution. It invokes run()
to perform thread3
tasks with
Line 26: thread3.setName("thread3")
will change its default name to thread3
.
The above code prints thread1
, thread2
, and thread3
names, priorities, and thread groups between square brackets to the console.