如何讓三個線程有序執行?

利用Thread中的join方法

代碼如下:

package Test;

public class Test17 {
	public static void main(String[] args) {
	    Thread t1 = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("線程t1執行了。。。");
			}
	    	
	    });
	    Thread t2 = new Thread(new Runnable() {
	    	
	    	@Override
	    	public void run() {
	    		try {
					t1.join();
					System.out.println("線程t2執行了。。。");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    	}
	    	
	    });
	    Thread t3 = new Thread(new Runnable() {
	    	
	    	@Override
	    	public void run() {
	    		try {
					t2.join();
					System.out.println("線程t3執行了。。。");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	    	}
	    	
	    });
	    
	    t1.start();
	    t2.start();
	    t3.start();
	    
	    
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章