从我在这里所阅读的内容来看,线程本身就加入了;
当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语句以查看程序流程)
在您的示例中,没有任何线程可以自我连接。
您的示例中的main()线程创建一个新线程,然后将其加入新线程。
不要将线程Thread
对象(即代码的执行)与Java对象混淆。所有方法都属于同一个对象,但是它们在两个不同的线程中运行。Thread
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句