-1
class MyRunnable implements Runnable
{
MyRunnable(String name)
{
new Thread(this, name).start();
}
public void run()
{
System.out.println("run() called by " + Thread.currentThread().getName());
System.out.println(Thread.currentThread().getName());
}
}
public class TestClass
{
public static void main(String[] args)
{
System.out.println(Thread.currentThread().getName());
Thread.currentThread().setName("First");
MyRunnable mr = new MyRunnable("MyRunnable");
mr.run();
Thread.currentThread().setName("Second");
mr.run();
}
}
輸出將是 主, 首先, 其次, MyRunnableThread.currentThread()。setName()調用線程的run方法嗎?
爲什麼到Thread.currentThread()的調用的setName( 「第一」)。調用run()方法?
是什麼讓你覺得它呢? – Paul
'new Thread(this,name).start();'在ctor中?不不不**。永遠不要這樣做。甚至不是一個笑話。 –
你正在處理競爭條件。 – nhaarman