Why the out put of below code is Thread[main,5,mai

2019-03-06 18:36发布

问题:

public class test1 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Thread t = Thread.currentThread();
        System.out.println(t);
    }
}

Why the output of above code is - Thread[main,5,main] ? Please Explain

回答1:

Because thread.toString() returns a string representation of this thread, including the thread's name, priority, and thread group.



回答2:

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

Source: https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#toString()



回答3:

Because of:

/**
 * Returns a string representation of this thread, including the
 * thread's name, priority, and thread group.
 *
 * @return  a string representation of this thread.
 */
public String toString() {
    ThreadGroup group = getThreadGroup();
    if (group != null) {
        return "Thread[" + getName() + "," + getPriority() + "," +
                       group.getName() + "]";
    } else {
        return "Thread[" + getName() + "," + getPriority() + "," +
                        "" + "]";
    }
}