当前位置: 代码迷 >> 综合 >> 多线程之join
  详细解决方案

多线程之join

热度:66   发布时间:2023-10-11 17:50:31.0

t.join()方法阻塞调用此方法的线程(calling thread),直到线程t完成,此线程再继续;通常用于在main()主线程内,等待其它线程完成再结束main()主线程。

package thread;
/*** T1执行之后T2  之后T3* @author zhoushiwen**/
public class Thread1 {public static void main(String[] args) throws Exception{Thread t1 = new Thread("t1"){@Overridepublic void run() {System.out.println("t1.....");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}};Thread t2 = new Thread("t2"){@Overridepublic void run() {try {t1.join();} catch (InterruptedException e1) {e1.printStackTrace();}System.out.println("t2.....");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}};Thread t3 = new Thread("t3"){@Overridepublic void run() {try {t2.join();} catch (InterruptedException e1) {e1.printStackTrace();}System.out.println("t3.....");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("t3.....over");}};t1.start();t2.start();t3.start();}}





  相关解决方案