如果线程自身调用 join() 会发生什么
what happens if thread calls join() on itself
根据我在这里阅读的内容 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
我是不是误会了线程永远等待的意思,还是我遗漏了什么
note: I know starting thread from constructor is not a recommended practice.. this is exact question in the dumps so i just pasted it (* not pretty much exact actually,i have placed println statements to see flow of the program)
在您的示例中没有加入自身的线程。
您示例中的 main() 线程创建了一个新线程,然后它加入了新线程。
不要将 Thread
(即 java 对象)与 thread(代码的执行)混淆。您的所有方法都属于到同一个 Thread
对象,但它们 运行 在两个不同的 线程中 .
James 是正确的(来自我的 +1),我只是想让它更明确。这是发生了什么:
您的主线程构造 startinginconstructor
构造函数。
构造函数调用 start,这将导致 startinginconstructor 对象在新线程中具有其 运行 方法。
之后主线程会继续调用makeitso。在makeitso中this
是startinginconstructor对象,所以结果是主线程一直等待,直到startinginconstructor对象代表的线程结束。
Java 对象可以被任何线程调用,只是因为一个对象扩展了线程并不意味着该线程上正在发生对该方法的任何调用。
根据我在这里阅读的内容 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
我是不是误会了线程永远等待的意思,还是我遗漏了什么
note: I know starting thread from constructor is not a recommended practice.. this is exact question in the dumps so i just pasted it (* not pretty much exact actually,i have placed println statements to see flow of the program)
在您的示例中没有加入自身的线程。
您示例中的 main() 线程创建了一个新线程,然后它加入了新线程。
不要将 Thread
(即 java 对象)与 thread(代码的执行)混淆。您的所有方法都属于到同一个 Thread
对象,但它们 运行 在两个不同的 线程中 .
James 是正确的(来自我的 +1),我只是想让它更明确。这是发生了什么:
您的主线程构造 startinginconstructor
构造函数。
构造函数调用 start,这将导致 startinginconstructor 对象在新线程中具有其 运行 方法。
之后主线程会继续调用makeitso。在makeitso中this
是startinginconstructor对象,所以结果是主线程一直等待,直到startinginconstructor对象代表的线程结束。
Java 对象可以被任何线程调用,只是因为一个对象扩展了线程并不意味着该线程上正在发生对该方法的任何调用。