为什么下面代码的输出是Thread
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]? 请解释
因为thread.toString()
返回此线程的字符串表示forms,包括线程的名称,优先级和线程组。
返回此线程的字符串表示forms,包括线程的名称,优先级和线程组。
资料来源: https : //docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#toString()
因为:
/** * 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() + "," + "" + "]"; } }