通過一道面試題瞭解Condition線程通訊,condition線程

來源:互聯網
上載者:User

通過一道面試題瞭解Condition線程通訊,condition線程
Condition

  • Condition介面描述了可能會與鎖有關聯的條件變數。這些變數在用法與使用Object.wait訪問的隱式監視器類似,但提供了更強大的功能。需要特別指出的是,單個Lock可能與多個Condition對象關聯。為了避免相容性問題,Condition方法的名稱與對應的Object版本中的不同;
  • 在Condition對象中,與wait、notify和notifyAll方法對應的分別是await、signal和signalAll;
  • Condition執行個體實質上被綁定到一個鎖上。要為特定Lock執行個體獲得Condition執行個體,請使用器newCondition()方法。
面試題
  1 package com.ccfdod.juc;  2   3 import java.util.concurrent.locks.Condition;  4 import java.util.concurrent.locks.Lock;  5 import java.util.concurrent.locks.ReentrantLock;  6   7 /**  8 * 題目:編寫一個程式,開啟 3 個線程,這三個線程的 ID 分別為 A、B、C,每個線程將自己的 ID 在螢幕上列印 10 遍,要求輸出的結果必須按順序顯示。  9 * 如:ABCABCABC…… 10 */ 11 class AlternateDemo { 12     //標誌當前由哪一個線程輸出,1代表A,2代表B,3代表C 13     private int number = 1; 14  15     Lock lock = new ReentrantLock(); 16  17     //Condition的強大之處在於它可以為多個線程間建立不同的Condition 18     Condition condition1 = lock.newCondition(); 19     Condition condition2 = lock.newCondition(); 20     Condition condition3 = lock.newCondition(); 21  22     // loopNum:當前迴圈輪數 23     public void loopA(int loopNum) { 24         //上鎖 25         lock.lock(); 26         try { 27             while (number != 1) { 28                 //等待 29                 condition1.await(); 30             } 31  32             System.out.println(Thread.currentThread().getName() + ", currentLoopNum is " + loopNum); 33             number = 2; 34             //喚醒 35             condition2.signal(); 36  37         } catch (InterruptedException e) { 38             e.printStackTrace(); 39         } finally { 40             //確保釋放鎖 41             lock.unlock(); 42         } 43     } 44  45     public void loopB(int loopNum) { 46         lock.lock(); 47         try { 48             while (number != 2) { 49                 condition2.await(); 50             } 51  52             System.out.println(Thread.currentThread().getName() + ", currentLoopNum is " + loopNum); 53             number = 3; 54             condition3.signal(); 55  56         } catch (InterruptedException e) { 57             e.printStackTrace(); 58         } finally { 59             lock.unlock(); 60         } 61     } 62  63     public void loopC(int loopNum) { 64         lock.lock(); 65         try { 66             while (number != 3) { 67                 condition3.await(); 68             } 69  70             System.out.println(Thread.currentThread().getName() + ", currentLoopNum is " + loopNum); 71             number = 1; 72             condition1.signal(); 73  74         } catch (InterruptedException e) { 75             e.printStackTrace(); 76         } finally { 77             lock.unlock(); 78         } 79     } 80 } 81  82 public class TestABCAlternate { 83  84     public static void main(String[] args) { 85         AlternateDemo ad = new AlternateDemo(); 86  87         new Thread(new Runnable() { 88  89             @Override 90             public void run() { 91                 for (int i = 0; i < 10; i++) 92                     ad.loopA(i); 93             } 94         }, "A").start(); 95  96         new Thread(new Runnable() { 97  98             @Override 99             public void run() {100                 for (int i = 0; i < 10; i++)101                     ad.loopB(i);102             }103         }, "B").start();104 105         new Thread(new Runnable() {106 107             @Override108             public void run() {109                 for (int i = 0; i < 10; i++)110                     ad.loopC(i);111             }112         }, "C").start();113     }114 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.