Join方法如何在Thread中工作。如果在run方法中写入join方法,那么它将陷入僵局。只需要知道其发生原因。
代码片段:
public class ThreadSchuduling extends Thread{
static ThreadSchuduling threadObj3;
public ThreadSchuduling(){
System.out.println("Default Constructor");
}
public ThreadSchuduling(String name){
System.out.println("Parameter Constructor");
}
public void run(){
try{
threadObj3.join();
}catch(Exception e){
System.out.println("Error in RUN "+e);
}
System.out.println(Thread.currentThread().getName());
for(int i = 0; i < 10; i++){
System.out.println("Value is = "+i);
}
}
public static void main(String[] args) {
ThreadSchuduling threadObj1 = new ThreadSchuduling("Thread1");
ThreadSchuduling threadObj2 = new ThreadSchuduling("Thread2");
threadObj3 = new ThreadSchuduling("Thread3");
ThreadSchuduling threadObj4 = new ThreadSchuduling("Thread4");
threadObj1.start();
threadObj2.start();
threadObj3.start();
System.out.println("Thread 3 is started");
threadObj4.start();
try{
threadObj3.join();
}catch(Exception e){
System.out.println("Errpr "+e);
}
System.out.println("Main Method completed");
}
}
我只想在thread1和thread2之前完成thread3
最佳答案
您没有解释threadObj3
是什么...是对同一线程的引用吗?如果是这样,它会死锁是可以理解的-它正在等待直到完成,而因为正在等待它不会执行!
您实际上想达到什么目的?
关于java - Java Thread Join方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3505194/