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. }  

执行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值