Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of printing a thread instance in Java?

For example,

System.out.println( Thread.currentThread() );

gives

Thread[main,5,main] 

What does [main,5,main] refer to? I am guessing perhaps one of them is the name, but I want to know what it all means precisely.

like image 296
torde Avatar asked Dec 06 '22 04:12

torde


2 Answers

From the javadoc of Thread:

public String toString()

Returns a string representation of this thread, including the thread's name, priority, and thread group.

like image 146
Simon Groenewolt Avatar answered Dec 20 '22 16:12

Simon Groenewolt


In the result:

Thread[main,5,main]

  • main is the thread's name
  • 5 is the priority and
  • main is the thread group.

The function currentThread() returns a reference to the currently executing thread object and when we try to print any object, the toString() method of the corresponding class gets called, so in this case toString() method of the Thread class gets called and it returns a string representation of this thread, including the thread's name, priority, and thread group.

like image 38
codaddict Avatar answered Dec 20 '22 17:12

codaddict