首页 新闻 会员 周边

利用condition实现顺序执行

0
悬赏园豆:10 [已解决问题] 解决于 2017-08-03 09:41

要用condition实现类似这样的效果

thread a 1
thread a 2
thread a 3

thread b 1
thread b 2
thread b 3

thread c 1
thread c 2
thread c 3

thread a 1
thread a 2
thread a 3

thread b 1
thread b 2
thread b 3

thread c 1
thread c 2
thread 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 }

结果,只能打印一轮。。。

 

 

夏威夷8080的主页 夏威夷8080 | 初学一级 | 园豆:151
提问于:2017-08-02 16:57
< >
分享
最佳答案
0

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

conditionA.signal();

conditionC.await(); 

 

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

收获园豆:8
苍枫露雨 | 小虾三级 |园豆:1027 | 2017-08-02 18:10

 非常感谢,点醒梦中人

夏威夷8080 | 园豆:151 (初学一级) | 2017-08-03 09:44
其他回答(1)
0

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

收获园豆:2
花飘水流兮 | 园豆:13560 (专家六级) | 2017-08-02 21:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册