Thread join()

package com.ztesoft.quartz_maven.quartz;maven

public class Test {ide

public static  class ThreadTesterA implements Runnable {


	@Override
	public void run() {
		try {
			System.out.println("線程A:開始");
			Thread t2 = new Thread(new ThreadTesterB());  // 在這裏插入線程B, 此時線程A會等到線程B結束才結束。 注意 join()
			t2.start();
			t2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("線程A: 終於等到線程B結束啦,我也要結束啦");
	}
}

public static  class ThreadTesterB implements Runnable {

	private int i;

	@Override
	public void run() {
		while (i <= 10) {
			System.out.println("我是線程B,  A 你先等會兒....我要輸出10次,這是第 " + i  +"次......");
			i++;
		}
		System.out.println();
	}
}
	public static void main(String[] args) throws InterruptedException {
		Thread t1 = new Thread(new ThreadTesterA());
		t1.start();

	}

}線程

相關文章
相關標籤/搜索