【发布时间】:2016-06-24 13:00:14
【问题描述】:
从我在这里读到的Thread join on itself ;
当join方法被自己调用时,它应该永远等待
我目前正在准备 ocajp 8 认证,为此经历了转储,当我得到这个问题时,我认为主要的应该永远等待
public class startinginconstructor extends Thread
{
private int x=2;
startinginconstructor()
{
start();
}
public static void main(String[] args) throws Exception
{
new startinginconstructor().makeitso();
}
public void makeitso() throws Exception
{
x=x-1;
System.out.println(Thread.currentThread().getName()+" about to call join ");
join();
System.out.println(Thread.currentThread().getName()+" makeitso completed ");
// above line shouldn't be executed (method shouldn't be completed as well )since it joined on itself..?
}
public void run()
{
System.out.println(Thread.currentThread().getName()+" run started ");
x*=2;
System.out.println(Thread.currentThread().getName()+" run about to complete ");
}
}
程序以以下输出结束
main about to call join
Thread-0 run started
Thread-0 run about to complete
main makeitso completed
我是否错误地理解了线程永远等待的含义,或者我错过了什么
注意:我知道从构造函数开始线程不是推荐的做法。这是转储中的确切问题,所以我只是粘贴了它(* 实际上不是很准确,我已经放置了 println 语句来查看程序的流程)
【问题讨论】:
标签: java multithreading wait