Thread中,join()方法的作用是调用线程等待该线程完成后,才能继续用下运行。
public class TestThread5 {
public static void main(String[] args) throws InterruptedException {
Runner0 run5 = new Runner0();
Thread th5 = new Thread(run5);
th5.start();
th5.join();//join()方法用在此处是为了等待主线程结束后运行子线程
for(int i=0;i<5;i++){
System.out.println("子线程:"+i);
}
}
}
class Runner0 implements Runnable{
public void run(){
for(int i=0;i<5;i++)
System.out.println("主线程:"+i);
}
}
上述代码的运行结构如下所示:
当然,如果不使用join()方法
public class TestThread6{
public static void main(String[] args) throws InterruptedException {
Runner0 run5 = new Runner0();
Thread th5 = new Thread(run5);
th5.start();
// th5.join();
for(int i=0;i<4;i++){
System.out.println("子线程:"+i);
}
}
}
class Runner0 implements Runnable{
public void run(){
for(int i=0;i<4;i++)
System.out.println("主线程:"+i);
}
}
如上代码注释掉jion()方法,
根据上面两个不同的代码,输出的不同,很容易就能理解join()方法。
原神祈愿模拟器最新版
原神祈愿模拟器手机版是一款完整汉化的趣味原神抽卡模拟小游戏,
宝宝森林美食完整版
宝宝森林美食游戏最新版是一款十分有趣的休闲益智游戏,帮助宝宝
g沙盒仇恨官方英文版(gorebox)
G沙盒仇恨英文原版是一款最近非常火热的沙盒模拟类游戏,在这里
迷你世界测试服最新版2024
迷你世界测试服2021最新版,即迷你世界的先遣服版本,用户能
闪耀暖暖最新版2024
闪耀暖暖手游这是非常好玩的换装手游,游戏内容丰富有趣,游戏环