日期:2014-05-20 浏览次数:20663 次
public String toString() { ThreadGroup group = getThreadGroup(); if (group != null) { return "Thread[" + getName() + "," + getPriority() + "," + group.getName() + "]"; } else { return "Thread[" + getName() + "," + getPriority() + "," + "" + "]"; } }
------解决方案--------------------
看API,Thread实现了Runnable接口。currentThread是一个静态方法。
在main方法中, Thread.currentThread ().getName,得到的是主线程,名字叫Thread-0。
Thread.currentThread () 得到main方法中的主线程:Thread[Thread-0,5,main]
------解决方案--------------------
Thread.currentThread ().getName 运行得到:Thread-0
Thread-0就是当前线程(主线程)的名字。
Thread.currentThread () 运行得到:Thread[Thread-0,5,main]
Thread.currentThread ()这个的值就是当前线程(主线程)的对象。
Thread[Thread-0,5,main]Thread-0不就是上面的线程名嘛,5表示线程优先级,main是线程组的名称。(API中就说明了)System.out.printn(Thread.currentThread ());会自动调用对象的toString()方法,这是Java的特性。也就是说,Thread[Thread-0,5,main]是Thread.currentThread ()的toString()的返回值。
------解决方案--------------------
Thread[Thread-0,5,main]是自动调用toString方法打印的
public String toString() { ThreadGroup group = getThreadGroup(); if (group != null) { return "Thread[" + getName() + "," + getPriority() + "," + group.getName() + "]"; } else { return "Thread[" + getName() + "," + getPriority() + "," + "" + "]"; } }