mar*_*ane 3 java multithreading
(Thread) A.join()-使当前线程等待线程A完成。我以为那是this.join()从run方法调用会导致线程死锁-它会等待自身完成。但是,这不会发生-代码可以编译并正常运行-没有死锁。那我打个电话this.join()怎么办?
OP在提供的示例中使用this.join()
内森(Nathan)在他的示例中使用Thread.currentThread()。join()代替。
通过使用this.join(),创建了一个新线程,该线程将被加入,但主线程是完整的。
@Nathan以下是预期的工作:
public class Main extends Thread {
@Override
public synchronized void run() {
super.run();
try {
this.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Main().start();
System.out.println("hello");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1912 次 |
| 最近记录: |