//join是获取处理器独占权,不与其他线程并行,执行了此方法的线程将获取处理器独占权,等其执行完,其他线程才能继续执行
@Test
public void test2() {
Thread t1 = new Thread(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("111");
});
Thread t2 = new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("222");
});
Thread t3 = new Thread(() ->
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("333");
});
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t3.start();
try {
t3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
网友评论