
- Java 併發教程
- 併發 - 首頁
- 併發 - 概述
- 併發 - 環境設定
- 併發 - 主要操作
- 執行緒間通訊
- 併發 - 同步
- 併發 - 死鎖
- 實用類示例
- 併發 - ThreadLocal
- 併發 - ThreadLocalRandom
- Lock 示例
- 併發 - Lock
- 併發 - ReadWriteLock
- 併發 - Condition
- 原子變數示例
- 併發 - AtomicInteger
- 併發 - AtomicLong
- 併發 - AtomicBoolean
- 併發 - AtomicReference
- 併發 - AtomicIntegerArray
- 併發 - AtomicLongArray
- 併發 - AtomicReferenceArray
- Executor 示例
- 併發 - Executor
- 併發 - ExecutorService
- ScheduledExecutorService
- 執行緒池示例
- 併發 - newFixedThreadPool
- 併發 - newCachedThreadPool
- newScheduledThreadPool
- newSingleThreadExecutor
- 併發 - ThreadPoolExecutor
- ScheduledThreadPoolExecutor
- 高階示例
- 併發 - Futures 和 Callables
- 併發 - Fork-Join 框架
- 併發集合
- 併發 - BlockingQueue
- 併發 - ConcurrentMap
- ConcurrentNavigableMap
- 併發有用資源
- 併發 - 快速指南
- 併發 - 有用資源
- 併發 - 討論
Java 併發 - Condition 介面
java.util.concurrent.locks.Condition 介面提供了一種執行緒能夠掛起其執行的能力,直到給定的條件為真。Condition 物件必須繫結到一個 Lock,並且必須使用 newCondition() 方法獲取。
Condition 方法
以下是 Condition 類中可用的一些重要方法列表。
序號 | 方法 & 描述 |
---|---|
1 | public void await() 導致當前執行緒等待,直到它被訊號通知或中斷。 |
2 | public boolean await(long time, TimeUnit unit) 導致當前執行緒等待,直到它被訊號通知或中斷,或指定的等待時間過去。 |
3 | public long awaitNanos(long nanosTimeout) 導致當前執行緒等待,直到它被訊號通知或中斷,或指定的等待時間過去。 |
4 | public long awaitUninterruptibly() 導致當前執行緒等待,直到它被訊號通知。 |
5 | public long awaitUntil() 導致當前執行緒等待,直到它被訊號通知或中斷,或指定的截止時間過去。 |
6 | public void signal() 喚醒一個等待執行緒。 |
7 | public void signalAll() 喚醒所有等待執行緒。 |
示例
以下 TestThread 程式演示了 Condition 介面的這些方法。在這裡,我們使用了 signal() 來通知,並使用 await() 來掛起執行緒。import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class TestThread { public static void main(String[] args) throws InterruptedException { ItemQueue itemQueue = new ItemQueue(10); //Create a producer and a consumer. Thread producer = new Producer(itemQueue); Thread consumer = new Consumer(itemQueue); //Start both threads. producer.start(); consumer.start(); //Wait for both threads to terminate. producer.join(); consumer.join(); } static class ItemQueue { private Object[] items = null; private int current = 0; private int placeIndex = 0; private int removeIndex = 0; private final Lock lock; private final Condition isEmpty; private final Condition isFull; public ItemQueue(int capacity) { this.items = new Object[capacity]; lock = new ReentrantLock(); isEmpty = lock.newCondition(); isFull = lock.newCondition(); } public void add(Object item) throws InterruptedException { lock.lock(); while(current >= items.length) isFull.await(); items[placeIndex] = item; placeIndex = (placeIndex + 1) % items.length; ++current; //Notify the consumer that there is data available. isEmpty.signal(); lock.unlock(); } public Object remove() throws InterruptedException { Object item = null; lock.lock(); while(current <= 0) { isEmpty.await(); } item = items[removeIndex]; removeIndex = (removeIndex + 1) % items.length; --current; //Notify the producer that there is space available. isFull.signal(); lock.unlock(); return item; } public boolean isEmpty() { return (items.length == 0); } } static class Producer extends Thread { private final ItemQueue queue; public Producer(ItemQueue queue) { this.queue = queue; } @Override public void run() { String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"}; try { for(String number: numbers) { System.out.println("[Producer]: " + number); } queue.add(null); } catch (InterruptedException ex) { ex.printStackTrace(); } } } static class Consumer extends Thread { private final ItemQueue queue; public Consumer(ItemQueue queue) { this.queue = queue; } @Override public void run() { try { do { Object number = queue.remove(); System.out.println("[Consumer]: " + number); if(number == null) { return; } } while(!queue.isEmpty()); } catch (InterruptedException ex) { ex.printStackTrace(); } } } }
這將產生以下結果。
輸出
[Producer]: 1 [Producer]: 2 [Producer]: 3 [Producer]: 4 [Producer]: 5 [Producer]: 6 [Producer]: 7 [Producer]: 8 [Producer]: 9 [Producer]: 10 [Producer]: 11 [Producer]: 12 [Consumer]: null
廣告