【轉】 Java 指定線程執行順序(三種方式)

轉自:http://blog.csdn.net/difffate/article/details/63684290

方法一:通過共享對象鎖加上可見變量來實現。

    public class MyService {  
      
        private volatile int orderNum = 1;  
      
        public synchronized void methodA() {  
            try {  
                while (orderNum != 1) {  
                    wait();  
                }  
                for (int i = 0; i < 2; i++) {  
                    System.out.println("AAAAA");  
                }  
                orderNum = 2;  
                notifyAll();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
      
        public synchronized void methodB() {  
            try {  
                while (orderNum != 2) {  
                    wait();  
                }  
                for (int i = 0; i < 2; i++) {  
                    System.out.println("BBBBB");  
                }  
                orderNum = 3;  
                notifyAll();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
      
        public synchronized void methodC() {  
            try {  
                while (orderNum != 3) {  
                    wait();  
                }  
                for (int i = 0; i < 2; i++) {  
                    System.out.println("CCCCC");  
                }  
                orderNum = 1;  
                notifyAll();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
import service.MyService;  
public class ThreadAA extends Thread {  
  
    private MyService dbtools;  
  
    public ThreadAA(MyService dbtools) {  
        super();  
        this.dbtools = dbtools;  
    }  
  
    @Override  
    public void run() {  
        dbtools.methodA();  
    }  
  
}  
import service.MyService;  
public class ThreadBB extends Thread {  
  
    private MyService dbtools;  
  
    public ThreadBB(MyService dbtools) {  
        super();  
        this.dbtools = dbtools;  
    }  
  
    @Override  
    public void run() {  
        dbtools.methodB();  
    }  
  
}  
import service.MyService;  
public class ThreadCC extends Thread {  
  
    private MyService dbtools;  
  
    public ThreadCC(MyService dbtools) {  
        this.dbtools = dbtools;  
    }  
  
    @Override  
    public void run() {  
        dbtools.methodC();  
    }  
  
}  
import extthread.ThreadCC;  
import service.MyService;  
import extthread.ThreadAA;  
import extthread.ThreadBB;  
  
public class Run {  
  
    public static void main(String[] args) {  
        MyService myService = new MyService();  
        for (int i = 0; i < 2; i++) {  
            ThreadBB output = new ThreadBB(myService);  
            output.start();  
            ThreadAA input = new ThreadAA(myService);  
            input.start();  
            ThreadCC threadCC = new ThreadCC(myService);  
            threadCC.start();  
        }  
    }  
  
}  

執行結果:

可以看到線程的啟動按順序執行了。共享對象鎖,可以保證每個方法只能同時有一個線程進入,配合wait和notifyall方法,可以啟動或者喚醒線程。

方法二:通過主線程Join()

class T11 extends Thread {  
    public void run() {  
        System.out.println("in T1");  
    }  
}  
  
class T22 extends Thread {  
    public void run() {  
        System.out.println("in T2");  
    }  
}  
  
class T33 extends Thread {  
    public void run() {  
        System.out.println("in T3");  
    }  
}  
  
public class Test2 {  
    public static void main(String[] args) throws InterruptedException {  
        T11 t1 = new T11();  
        T22 t2 = new T22();  
        T33 t3 = new T33();  
        t1.start();  
        t1.join();  
        t2.start();  
        t2.join();  
        t3.start();  
    }  
}  

方法三:通過線程執行時Join()

class T1 extends Thread {  
    public void run(){  
        Random random = new Random();  
        try {  
            Thread.sleep(random.nextInt(1000));  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        System.out.println("in T1");  
    }  
}  
  
class T2 extends Thread{  
    private Thread thread;  
    public T2(Thread thread) {  
        this.thread = thread;  
    }  
  
    public void run(){  
        try {  
            thread.join();  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        System.out.println("in T2");  
    }  
}  
  
class T3 extends Thread{  
    private Thread thread;  
    public T3(Thread thread) {  
        this.thread = thread;  
    }  
  
    public void run(){  
        try {  
            thread.join();  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        System.out.println("in T3");  
    }  
}  
  
public class Test {  
    public static void main(String[] args) throws InterruptedException {  
        T1 t1 = new T1();  
        T2 t2 = new T2(t1);  
        T3 t3 = new T3(t2);  
        t2.start();  
        t1.start();  
        t3.start();  
    }  
}  
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 該文章轉自:http://blog.csdn.net/evankaka/article/details/44153...
    加來依藍閱讀 7,381評論 3 87
  • Java多線程學習 [-] 一擴展javalangThread類 二實現javalangRunnable接口 三T...
    影馳閱讀 2,994評論 1 18
  • 本文主要講了java中多線程的使用方法、線程同步、線程數據傳遞、線程狀態及相應的一些線程函數用法、概述等。 首先講...
    李欣陽閱讀 2,503評論 1 15
  • 寫在前面的話: 這篇博客是我從這里“轉載”的,為什么轉載兩個字加“”呢?因為這絕不是簡單的復制粘貼,我花了五六個小...
    SmartSean閱讀 4,793評論 12 45
  • 編程能力是一種解決問題的能力。如果問題沒能被很好地解決,知道再多也沒用。 編程能力是一種運用機器解決問題的能力。首...
    切切歆語閱讀 431評論 0 0