超详细 多线程协作输出不同内容
1.使用join插队控制单个执行顺序,也可以通过创建单个线程池来实现
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(t1);
executor.submit(t2);
executor.submit(t3);
executor.shutdown();
//此方法也可以实现有序执行
public class Test {
public static void main(String[] args) {
final Thread join1 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("join1");
}
}
}, "T1");
final Thread join2 = new Thread(new Runnable() {
public void run() {
try {
join1.join();
//在执行2之前判断1是否执行完,下面同理
System.out.println("join2");
} catch (InterruptedException e) {
e.printStackTrace();