- 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
广告