我想通过join让线程 a b c d顺序打印,但是现在是乱序打印
public class ExJoin extends Thread {
Thread thread;
public ExJoin(){
}
public ExJoin(Thread thread){
this.thread = thread;
}
@Override
public void run() {
try {
if (thread != null){
thread.join();
}
System.out.println(Thread.currentThread().getName()+" is running");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread threadA = new ExJoin();
threadA.setName("A");
Thread threadB = new ExJoin(threadA);
threadB.setName("B");
Thread threadC = new ExJoin(threadB);
threadC.setName("C");
Thread threadD = new ExJoin(threadC);
threadD.setName("D");
threadC.start();
threadD.start();
threadA.start();
threadB.start();
}
}
HUX布斯
慕盖茨4494581
相关分类