java.util.concurrent.locks.condition
接口提供一个线程挂起执行的能力,直到给定的条件为真。 condition
对象必须绑定到lock
,并使用newcondition()
方法获取对象。
以下是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){
queue.add(number);
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
[consumer]: 1
[producer]: 2
[consumer]: 2
[producer]: 3
[consumer]: 3
[producer]: 4
[consumer]: 4
[producer]: 5
[producer]: 6
[consumer]: 5
[producer]: 7
[consumer]: 6
[consumer]: 7
[producer]: 8
[consumer]: 8
[producer]: 9
[consumer]: 9
[producer]: 10
[consumer]: 10
[producer]: 11
[consumer]: 11
[producer]: 12
[consumer]: 12
[consumer]: null