Thread Join()

有線程T1,T2和T3,將如何確保線程T2運行後T1後,T3運行在T2線程後。

public class JoinTest{
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable(){
            @Override
            public void run(){
                System.out.println("t1");
            }
        });
        Thread t2 = new Thread(new Runnable(){
            @Override
            public void run(){
                try{
                    t1.join();

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