Condition是AQS的一个内部类
里面维护了一个等待队列
public class ConditionObject implements Condition, java.io.Serializable {
private transient Node firstWaiter;
private transient Node lastWaiter;
public ConditionObject() { }
主要方法await()
public final void await() throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
//将当前线程构造成node放入的等待队列队尾
Node node = addConditionWaiter();
//释放锁,并且唤醒同步队列中的下一个
int savedState = fullyRelease(node);
int interruptMode = 0;
//判断该节点是在同步队列
while (!isOnSyncQueue(node)) {
//挂起线程,while自旋,直到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 node = new Node(Thread.currentThread(), Node.CONDITION);
if (t == null)
firstWaiter = node;
else
t.nextWaiter = node;
lastWaiter = node;
return node;
}
final int fullyRelease(Node node) {
boolean failed = true;
try {
int savedState = getState();
if (release(savedState)) {
failed = false;
return savedState;
} else {
throw new IllegalMonitorStateException();
}
} finally {
if (failed)
node.waitStatus = Node.CANCELLED;
}
}
public final boolean release(int arg) {
//释放锁
if (tryRelease(arg)) {
Node h = head;
if (h != null && h.waitStatus != 0)
//激活后续节点
unparkSuccessor(h);
return true;
}
return false;
}
总的来说,condition.await()方法,将当前线程持有的锁释放掉,构造成node放入condition的等待队列中,挂起该线程,并自旋等待node的同步状态。如果node被放入同步队列中,则尝试获取锁或再次放入同步队列并挂起。
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 (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
return false;
//放入同步队列
Node p = enq(node);
int ws = p.waitStatus;
if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
//激活当前线程
LockSupport.unpark(node.thread);
return true;
}
signal方法主要是将node从等待队列中迁移到同步队列中,并改变node状态,使await自旋终止。
但是这里发起signal的线程并没有释放锁,需要等本线程lock.unlock()执行完,内部执行了ReentrantLock的tryRelease方法,将state设为0,并激活后续同步列表中的节点,才真正完成了锁的释放。
合并reentrantLock 总的来说,lock.lock的本质是获取AQS中state为0状态,如果获取失败,则将试图获取的线程封装成到同步队列中,进行挂起。获取成功便继续执行操作。lock.unlock,本质是将AQS中的state状态值为0,即释放锁,并将同步队列中的下一个节点激活(不再挂起,抢锁)。
condition.await()方法是将已获取到锁的线程,tryRelease释放锁,并将该线程封装成节点从同步队列中移除,放入condition的等待队列中,并自旋检测该node状态,并激活同步队列中的下一个节点。
condition.signal()方法是将condition等待队列中的头节点移动到同步队列中,但此时本线程并未释放锁,故需等本线程执行lock.unclock方法,通过tryRelease将state置为0,并激活下一个同步队列节点进行tryAcquire抢锁。