Condition实现多线程顺序打印

Condition实现多线程顺序打印:

  

 1 import java.util.concurrent.locks.Condition;
 2 import java.util.concurrent.locks.ReentrantLock;
 3 
 4 public class Run {
 5     
 6     volatile public static int nextPrintWho = 1;
 7     private static ReentrantLock lock = new ReentrantLock();
 8     final private static Condition conditionA = lock.newCondition();
 9     final private static Condition conditionB = lock.newCondition();
10     final private static Condition conditionC = lock.newCondition();
11     
12     public static void main(String[] args) {
13         
14         Thread threadA = new Thread() {
15             @Override
16             public void run() {
17                 try {
18                     lock.lock();
19                     while (nextPrintWho != 1) {
20                         conditionA.await();
21                     }
22                     for (int i = 0; i < 3; i++) {
23                         System.out.println("ThreadA " + (i+1));
24                     }
25                     nextPrintWho = 2;
26                     conditionB.signalAll();
27                 } catch (InterruptedException e) {
28                     e.printStackTrace();
29                 }finally {
30                     lock.unlock();
31                 }
32             };
33         };
34         Thread threadB = new Thread() {
35             @Override
36             public void run() {
37                 try {
38                     lock.lock();
39                     while (nextPrintWho != 2) {
40                         conditionB.await();
41                     }
42                     for (int i = 0; i < 3; i++) {
43                         System.out.println("ThreadB " + (i+1));
44                     }
45                     nextPrintWho = 3;
46                     conditionC.signalAll();
47                 } catch (InterruptedException e) {
48                     e.printStackTrace();
49                 }finally {
50                     lock.unlock();
51                 }
52             };
53         };
54         Thread threadC = new Thread() {
55             @Override
56             public void run() {
57                 try {
58                     lock.lock();
59                     while (nextPrintWho != 3) {
60                         conditionC.await();
61                     }
62                     for (int i = 0; i < 3; i++) {
63                         System.out.println("ThreadC " + (i+1));
64                     }
65                     nextPrintWho = 1;
66                     conditionA.signalAll();
67                 } catch (InterruptedException e) {
68                     e.printStackTrace();
69                 }finally {
70                     lock.unlock();
71                 }
72             };
73         };
74         Thread[] aArray = new Thread[5];
75         Thread[] bArray = new Thread[5];
76         Thread[] cArray = new Thread[5];
77         for (int i = 0; i < 5; i++) {
78             aArray[i] = new Thread(threadA);
79             bArray[i] = new Thread(threadB);
80             cArray[i] = new Thread(threadC);
81             aArray[i].start();
82             bArray[i].start();
83             cArray[i].start();
84         }
85     }
86 }

运行结果如下:

  

 

转载于:https://www.cnblogs.com/wang1001/p/9579889.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值