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()方法。
美味餐厅模拟经营模拟器
美味餐厅模拟经营模拟器带来深度沉浸的餐饮管理体验,您将化身餐
高校模拟器2025汉化版
高校模拟器2025是一款很好玩的模拟手游,游戏是以女子高中的
一亿小目标手游
如果让你的人生从来,你可以像王健林一样霸气的表示一亿小目标吗
一亿小目标手机版2019
一亿小目标手机版2019趣味休闲模拟游戏,玩家在游戏中将会通
巴啦啦梦幻餐厅
巴啦啦梦幻餐厅是一款充满魔幻色彩的二次元模拟经营手游,游戏界