join实现顺序执行线程
join()
等待线程摧毁
join和synchronized区别
join
在内部使用wait()
方法进行等待
synchronized
关键字使用的是“对象监视器”原理做为同步
实例
class A implements Runnable {
@Override
public void run() {
System.out.println("A");
}
}
class B implements Runnable {
@Override
public void run() {
System.out.println("B");
}
}
class C implements Runnable {
@Override
public void run() {
System.out.println("C");
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
Thread aThread = new Thread(new A());
Thread bThread = new Thread(new B());
Thread cThread = new Thread(new C());
aThread.start();
aThread.join();
bThread.start();
bThread.join();
cThread.start();
cThread.join();
System.out.println("main");
}
}
网友评论