Java多線程運行時序問題

兩種方案:
        1>利用Thread.join()方法,使C進程等待AB進程完成後執行
        2>利用CountdownLatch定義一個計數器,在AB進程裏用CountdownLatch. countDown()方法使計數器減少,在等待進程C中使用CountDownLatch.await()方法等待,直到計數器變爲0,纔開始執行
1.    思路:
a)    建立A B C三個線程,空跑模擬線程運行。
b)    在父進程中通過start()啓動各子線程。
c)    利用上述兩種方案完成任務。
2.    代碼:
TestThread.java

public class TestThread {
    public static void main(String[] args){
        Thread A=new Thread(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while(i<18){
                    try {
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    i++;
                }
                System.out.println("A OK");
            }
        });
        Thread B=new Thread(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while(i<10){
                    try {
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    i++;
                }
                System.out.println("B OK");
            }
        });
        Thread C=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    A.join();
                    B.join();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                int i=0;
                while(i<6){
                    try {
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    i++;
                }
                System.out.println("C OK");
            }
        });
        A.start();
        B.start();
        C.start();
    }
}


TestThread2.java

import java.util.concurrent.CountDownLatch;
public class TestThread2 {
    public static void main(String[] args){
        CountDownLatch count=new CountDownLatch(2);
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while(i<18){
                    try {
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    i++;
                }
                System.out.println("A OK");
                count.countDown();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i=0;
                while(i<10){
                    try {
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    i++;
                }
                System.out.println("B OK");
                count.countDown();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    count.await();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                int i=0;
                while(i<6){
                    try {
                        Thread.sleep(1000);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    i++;
                }
                System.out.println("C OK");
            }
        }).start();
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章