//看文档的说明很难理解join是怎么用的,其实很简单
package com.test.thread;
public class Join {
static HostThread host;
static GuestThread guest;
public static void main(String[] args) {
host = new HostThread();
guest = new GuestThread();
host.start();
}
static class HostThread extends Thread{
@Override
public void run() {
System.out.println("-----准备晚餐------");
guest.start();
System.out.println("------打电话通知客人过来-------");
try {
guest.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("------共进晚餐-------");
}
}
static class GuestThread extends Thread{
@Override
public void run() {
System.out.println("------客人:开始出发-------");
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("------客人:经过一段时间后到达目的地-------");
}
}
}
/*
输出:
-----准备晚餐------
------打电话通知客人过来-------
------客人:开始出发-------
------客人:经过一段时间后到达目的地-------
------共进晚餐-------
看完例子相信很容理解了...
*/
Thread类的join方法使用
最新推荐文章于 2023-03-25 09:39:49 发布