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 Thread class 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 class
import java.lang.Thread;
// Main class
public 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 threads
Main thread1=new Main();
Main thread2=new Main();
// call run() method
thread1.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. The start() function invokes the run() function from the Thread class.

Output

It will print thread1 and thread2 name, priority, and thread group between square brackets.

Threads with different priorities

// Load Thread class
import java.lang.Thread;
// Main class
public 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 threads
Main thread1= new Main();
Main thread2= new Main();
Main thread3= new Main();
// call run() method
// first thread with maximum priority
thread1.start();
thread1.setName("thread1");
thread1.setPriority(MAX_PRIORITY);
// second thread with minimum priority
thread2.start();
thread2.setName("thread2");
thread2.setPriority(MIN_PRIORITY);
// third thread with default priority
thread3.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, 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 MAX_PRIORITYValue=10.

  • 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 MIN_PRIORITYValue=1.

  • Line 25: We call start() to initiate thread3 execution. It invokes run() to perform thread3 tasks with default priorityValue=5.

    • Line 26: thread3.setName("thread3") will change its default name to thread3.

Output

The above code prints thread1, thread2, and thread3 names, priorities, and thread groups between square brackets to the console.

Free Resources