第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

利用condition實現(xiàn)順序執(zhí)行

利用condition實現(xiàn)順序執(zhí)行

德瑪西亞99 2018-12-07 00:04:44
要用condition實現(xiàn)類似這樣的效果 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實現(xiàn)順序執(zhí)行 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 } 結(jié)果,只能打印一輪。。。 ? ?
查看完整描述

2 回答

?
絕地?zé)o雙

TA貢獻1946條經(jīng)驗 獲得超4個贊

printA,B,C 這三個方法中修改一下await和signal的順序:

conditionA.signal();

conditionC.await();?

?

先signal 再 ?await,不然執(zhí)行到conditionC.await(); 的時候線程c進入阻塞狀態(tài),這個時候還沒有執(zhí)行conditionA.signal(), 就陷入死鎖了。

查看完整回答
反對 回復(fù) 2018-12-16
?
慕森卡

TA貢獻1806條經(jīng)驗 獲得超8個贊

命題的思路有問題,順序執(zhí)行即為串行,串行就是不開線程。

查看完整回答
反對 回復(fù) 2018-12-16
  • 2 回答
  • 0 關(guān)注
  • 557 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號