java 保證線程順序的幾種方式

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

[java] view plain copy
  1. public class MyService {  
  2.   
  3.     private volatile int orderNum = 1;  
  4.   
  5.     public synchronized void methodA() {  
  6.         try {  
  7.             while (orderNum != 1) {  
  8.                 wait();  
  9.             }  
  10.             for (int i = 0; i < 2; i++) {  
  11.                 System.out.println("AAAAA");  
  12.             }  
  13.             orderNum = 2;  
  14.             notifyAll();  
  15.         } catch (InterruptedException e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
  19.   
  20.     public synchronized void methodB() {  
  21.         try {  
  22.             while (orderNum != 2) {  
  23.                 wait();  
  24.             }  
  25.             for (int i = 0; i < 2; i++) {  
  26.                 System.out.println("BBBBB");  
  27.             }  
  28.             orderNum = 3;  
  29.             notifyAll();  
  30.         } catch (InterruptedException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34.   
  35.     public synchronized void methodC() {  
  36.         try {  
  37.             while (orderNum != 3) {  
  38.                 wait();  
  39.             }  
  40.             for (int i = 0; i < 2; i++) {  
  41.                 System.out.println("CCCCC");  
  42.             }  
  43.             orderNum = 1;  
  44.             notifyAll();  
  45.         } catch (InterruptedException e) {  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49. }  

[java] view plain copy
  1. import service.MyService;  
  2. public class ThreadAA extends Thread {  
  3.   
  4.     private MyService dbtools;  
  5.   
  6.     public ThreadAA(MyService dbtools) {  
  7.         super();  
  8.         this.dbtools = dbtools;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void run() {  
  13.         dbtools.methodA();  
  14.     }  
  15.   
  16. }  

[java] view plain copy
  1. import service.MyService;  
  2. public class ThreadBB extends Thread {  
  3.   
  4.     private MyService dbtools;  
  5.   
  6.     public ThreadBB(MyService dbtools) {  
  7.         super();  
  8.         this.dbtools = dbtools;  
  9.     }  
  10.   
  11.     @Override  
  12.     public void run() {  
  13.         dbtools.methodB();  
  14.     }  
  15.   
  16. }  

[java] view plain copy
  1. import service.MyService;  
  2. public class ThreadCC extends Thread {  
  3.   
  4.     private MyService dbtools;  
  5.   
  6.     public ThreadCC(MyService dbtools) {  
  7.         this.dbtools = dbtools;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void run() {  
  12.         dbtools.methodC();  
  13.     }  
  14.   
  15. }  

[java] view plain copy
  1. import extthread.ThreadCC;  
  2. import service.MyService;  
  3. import extthread.ThreadAA;  
  4. import extthread.ThreadBB;  
  5.   
  6. public class Run {  
  7.   
  8.     public static void main(String[] args) {  
  9.         MyService myService = new MyService();  
  10.         for (int i = 0; i < 2; i++) {  
  11.             ThreadBB output = new ThreadBB(myService);  
  12.             output.start();  
  13.             ThreadAA input = new ThreadAA(myService);  
  14.             input.start();  
  15.             ThreadCC threadCC = new ThreadCC(myService);  
  16.             threadCC.start();  
  17.         }  
  18.     }  
  19.   
  20. }  

執行結果:

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