Condition接口提供了與Object阻塞(wait())與喚醒(notify()或notifyAll())相似的功能,只不過Condition接口提供了更為豐富的功能,如:限定等待時長等。Condition需要與Lock結合使用,需要通過鎖對象獲取Condition。
一、基本使用
基於Condition實現生產者、消費者模式。代碼基本與Object#wait()和Object#notify()類似,只不過我們使用Lock替換了synchronized關鍵字。
生產者
public class Producer implements Runnable {
private Lock lock;
private Condition condition;
private Queue<String> queue;
private int maxSize;
public Producer(Lock lock, Condition condition, Queue<String> queue, int maxSize) {
this.lock = lock;
this.condition = condition;
this.queue = queue;
this.maxSize = maxSize;
}
@Override
public void run() {
int i = 0;
for (; ; ) {
lock.lock();
// 如果滿了,則阻塞
while (queue.size() == maxSize) {
System.out.println("生產者隊列滿了,等待...");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
queue.add("一個消息:" + ++i);
System.out.printf("生產者%s生產了一個消息:%s\n", Thread.currentThread().getName(), i);
condition.signal();
lock.unlock();
}
}
}
消費者
public class Consumer implements Runnable {
private Lock lock;
private Condition condition;
private Queue<String> queue;
private int maxSize;
public Consumer(Lock lock, Condition condition, Queue<String> queue, int maxSize) {
this.lock = lock;
this.condition = condition;
this.queue = queue;
this.maxSize = maxSize;
}
@Override
public void run() {
for (; ; ) {
lock.lock();
while (queue.isEmpty()) {
System.out.println("消費者隊列為空,等待...");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
String obj = queue.remove();
System.out.printf("消費者%s消費一個消息:%s\n", Thread.currentThread().getName(), obj);
condition.signal();
lock.unlock();
}
}
}
測試類
public class ConditionProducerConsumer {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
Queue<String> queue = new LinkedBlockingQueue<>();
int maxSize = 10;
Producer producer = new Producer(lock, condition, queue, maxSize);
Consumer consumer = new Consumer(lock, condition, queue, maxSize);
new Thread(producer).start();
new Thread(consumer).start();
}
}
二、源碼分析
上述示例中使用的Lock是ReentrantLock,關於它的lock方法與unlock方法的原理詳見ReentrantLock實現原理。上述示例中的Condition對象是調用了Lock#newCondition()方法,源碼如下:
public class ReentrantLock implements Lock, java.io.Serializable {
...
public Condition newCondition() {
return sync.newCondition();
}
abstract static class Sync extends AbstractQueuedSynchronizer {
...
final ConditionObject newCondition() {
return new ConditionObject();
}
...
}
...
}
上述的ConditionObject定義在AQS中,如下:
public abstract class AbstractQueuedSynchronizer
extends AbstractOwnableSynchronizer
implements java.io.Serializable {
...
public class ConditionObject implements Condition, java.io.Serializable {
...
}
...
}
首先來分析下Condition#await()方法
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
Node node = addConditionWaiter();
int savedState = fullyRelease(node);
int interruptMode = 0;
while (!isOnSyncQueue(node)) {
LockSupport.park(this);
if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
break;
}
if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
interruptMode = REINTERRUPT;
if (node.nextWaiter != null) // clean up if cancelled
unlinkCancelledWaiters();
if (interruptMode != 0)
reportInterruptAfterWait(interruptMode);
}
private Node addConditionWaiter() {
Node t = lastWaiter;
// If lastWaiter is cancelled, clean out.
if (t != null && t.waitStatus != Node.CONDITION) {
unlinkCancelledWaiters();
t = lastWaiter;
}
Node node = new Node(Thread.currentThread(), Node.CONDITION);
if (t == null)
firstWaiter = node;
else
t.nextWaiter = node;
lastWaiter = node;
return node;
}
根據AQS隊列的特性,若有多個線程執行lock#lock()方法,會將處於阻塞狀態的線程維護到一個雙向鏈表中,如下:

假設當前是線程A獲取到鎖,其他線程執行lock#lock()方法時,將會構建成一個上述鏈表。
若獲取鎖的線程(線程A)執行Condition#await()方法,則會將當前線程添加至Condition隊列中,如下:

然后在調用fullyRelease()方法時會釋放當前線程的鎖,然后喚醒處於阻塞隊列中的下一個線程:

在調用isOnSyncQueue()方法時會檢查當前節點是否在同步隊列中,若不存在,則會調用LockSupport.park()進行阻塞。
假設當前線程A是生產者線程,調用await()方法后,會釋放鎖,並且將當前線程加入到Condition隊列中。此時,消費者能獲取到鎖資源,然后繼續執行。假設線程B是消費者線程,當添加一個元素后會調用condition#signal()方法,定義如下:
public final void signal() {
if (!isHeldExclusively())
throw new IllegalMonitorStateException();
Node first = firstWaiter;
if (first != null)
doSignal(first);
}
private void doSignal(Node first) {
do {
if ( (firstWaiter = first.nextWaiter) == null)
lastWaiter = null;
first.nextWaiter = null;
} while (!transferForSignal(first) &&
(first = firstWaiter) != null);
}
final boolean transferForSignal(Node node) {
/*
* If cannot change waitStatus, the node has been cancelled.
*/
if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
return false;
/*
* Splice onto queue and try to set waitStatus of predecessor to
* indicate that thread is (probably) waiting. If cancelled or
* attempt to set waitStatus fails, wake up to resync (in which
* case the waitStatus can be transiently and harmlessly wrong).
*/
Node p = enq(node);
int ws = p.waitStatus;
if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
LockSupport.unpark(node.thread);
return true;
}
執行signal()方法,會將Condition隊列中的第一個節點移除,將其變為同步隊列中的尾結點,如下:

至此,完成了Condition隊列轉換為同步隊列的過程。后續流程基本就是重復以上操作。
本文詳細介紹了單個Condition隊列的執行流程,其實一個Lock中可以有多個Condition隊列,比如:JUC中提供的LinkedBlockingDeque、ArrayBlockingQueue等
