为了账号安全,请及时绑定邮箱和手机立即绑定

利用condition实现顺序执行

利用condition实现顺序执行

德玛西亚99 2018-12-07 00:04:44
要用condition实现类似这样的效果 thread a 1thread a 2thread a 3 thread b 1thread b 2thread b 3 thread c 1thread c 2thread c 3 thread a 1thread a 2thread a 3 thread b 1thread b 2thread b 3 thread c 1thread c 2thread c 3 。。。 为什么我写的代码只能打印一轮? 代码: 1 /** 2 * 利用condition实现顺序执行 3 * @author ko 4 * 5 */ 6 public class MyService { 7 protected ReentrantLock lock = new ReentrantLock(); 8 protected Condition conditionA = lock.newCondition(); 9 protected Condition conditionB = lock.newCondition(); 10 protected Condition conditionC = lock.newCondition(); 11 12 public void printA() throws InterruptedException{ 13 lock.lock(); 14 for (int j = 0; j < 10; j++) { 15 for (int i = 1; i < 4; i++) { 16 System.out.println(Thread.currentThread().getName()+" "+i); 17 } 18 System.out.println(""); 19 conditionA.await(); 20 conditionB.signal(); 21 } 22 lock.unlock(); 23 } 24 public void printB() throws InterruptedException{ 25 lock.lock(); 26 for (int j = 0; j < 10; j++) { 27 for (int i = 1; i < 4; i++) { 28 System.out.println(Thread.currentThread().getName()+" "+i); 29 } 30 System.out.println(""); 31 conditionB.await(); 32 conditionC.signal(); 33 } 34 lock.unlock(); 35 } 36 public void printC() throws InterruptedException{ 37 lock.lock(); 38 for (int j = 0; j < 10; j++) { 39 for (int i = 1; i < 4; i++) { 40 System.out.println(Thread.currentThread().getName()+" "+i); 41 } 42 System.out.println(""); 43 conditionC.await(); 44 conditionA.signal(); 45 } 46 lock.unlock(); 47 } 48 } 1 public class ThreadA extends Thread { 2 protected MyService myService; 3 4 public ThreadA(MyService myService) { 5 super(); 6 this.myService = myService; 7 } 8 9 public void run() { 10 try { 11 myService.printA(); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 } 16 } 1 public class ThreadB extends Thread { 2 protected MyService myService; 3 4 public ThreadB(MyService myService) { 5 super(); 6 this.myService = myService; 7 } 8 9 public void run() { 10 try { 11 myService.printB(); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 } 16 } 1 public class ThreadC extends Thread { 2 protected MyService myService; 3 4 public ThreadC(MyService myService) { 5 super(); 6 this.myService = myService; 7 } 8 9 public void run() { 10 try { 11 myService.printC(); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 } 16 } 1 /** 2 * 测试类 3 * @author ko 4 * 5 */ 6 public class Test { 7 public static void main(String[] args) { 8 MyService myService = new MyService(); 9 10 ThreadA ta = new ThreadA(myService); 11 ta.setName("thread a"); 12 ta.start(); 13 14 ThreadB tb = new ThreadB(myService); 15 tb.setName("thread b"); 16 tb.start(); 17 18 ThreadC tc = new ThreadC(myService); 19 tc.setName("thread c"); 20 tc.start(); 21 } 22 } 结果,只能打印一轮。。。    
查看完整描述

2 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

printA,B,C 这三个方法中修改一下await和signal的顺序:

conditionA.signal();

conditionC.await(); 

 

先signal 再  await,不然执行到conditionC.await(); 的时候线程c进入阻塞状态,这个时候还没有执行conditionA.signal(), 就陷入死锁了。

查看完整回答
反对 回复 2018-12-16
?
慕森卡

TA贡献1806条经验 获得超8个赞

命题的思路有问题,顺序执行即为串行,串行就是不开线程。

查看完整回答
反对 回复 2018-12-16
  • 2 回答
  • 0 关注
  • 561 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号