join
概述
join:当前线程等待子线程执行结束
join(), join(long millis), join(long millis,int nanos)
- 模拟采集服务器指标案例
Thread t1 = new Thread(() -> {
System.out.println("开始采集服务器1");
try {
Thread.sleep(5_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("服务器1采集结束");
});
Thread t2 = new Thread(() -> {
System.out.println("开始采集服务器2");
try {
Thread.sleep(20_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("服务器2采集结束");
});
Thread t3 = new Thread(() -> {
System.out.println("开始采集服务器3");
try {
Thread.sleep(30_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("服务器3采集结束");
});
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
System.out.println("采集完毕!");