为什么下面代码的输出是Thread [main,5,main]

时间:2016-04-20 14:28:50

标签: java multithreading main void

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

为什么上面代码的输出是 - Thread [main,5,main]? 请解释

3 个答案:

答案 0 :(得分:4)

  

返回此线程的字符串表示形式,包括线程   名称,优先级和线程组。

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

答案 1 :(得分:3)

因为thread.toString()返回此线程的字符串表示形式,包括线程的名称,优先级和线程组。

答案 2 :(得分:1)

因为:

/**
 * 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() + "," +
                        "" + "]";
    }
}