一、Condition.await()做了什么?

 1 public final void await() throws InterruptedException {
 2             if (Thread.interrupted())
 3                 throw new InterruptedException();
 4             Node node = addConditionWaiter();
 5             int savedState = fullyRelease(node);
 6             int interruptMode = 0;
 7             while (!isOnSyncQueue(node)) {
 8                 LockSupport.park(this);
 9                 if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
10                     break;
11             }
12             if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
13                 interruptMode = REINTERRUPT;
14             if (node.nextWaiter != null) // clean up if cancelled
15                 unlinkCancelledWaiters();
16             if (interruptMode != 0)
17                 reportInterruptAfterWait(interruptMode);
18         }
 1 //1.当前持有锁并调用await()方法的线程构建成一个condition队列
 2 private Node addConditionWaiter() {
 3             Node t = lastWaiter;
 4             if (t != null && t.waitStatus != Node.CONDITION) {
 5                 unlinkCancelledWaiters();
 6                 t = lastWaiter;
 7             }
 8             //获得锁的线程构建成一个Node
 9             Node node = new Node(Thread.currentThread(), Node.CONDITION);
10             if (t == null)
11                 firstWaiter = node;
12             else
13                 t.nextWaiter = node;
14             lastWaiter = node;
15             return node;
16         }

02Condition条件控制_JUC

 1 //2.释放锁,唤醒阻塞状态下的线程
 2 final int fullyRelease(Node node) {
 3         boolean failed = true;
 4         try {
 5             int savedState = getState();
 6             if (release(savedState)) {
 7                 failed = false;
 8                 return savedState;
 9             } else {
10                 throw new IllegalMonitorStateException();
11             }
12         } finally {
13             if (failed)
14                 node.waitStatus = Node.CANCELLED;
15         }
16     }
1 //3.判断节点是否在AQS同步队列中
2 final boolean isOnSyncQueue(Node node) {
3         if (node.waitStatus == Node.CONDITION || node.prev == null)
4             return false;
5         if (node.next != null) // If has successor, it must be on queue
6             return true;
7         //遍历查找
8         return findNodeFromTail(node);
9     }

02Condition条件控制_JUC_02

 

 

二、Condition.signal()方法做了啥?

  1.原tail节点是CANCELED状态

  2.condition的节点transfer到aqs队列之后,通过lock.unlock()唤醒

1 public final void signal() {
2             if (!isHeldExclusively())
3                 throw new IllegalMonitorStateException();
4             Node first = firstWaiter;
5             if (first != null)
6                 doSignal(first);
7         }
1 private void doSignal(Node first) {
2             do {
3                 if ( (firstWaiter = first.nextWaiter) == null)
4                     lastWaiter = null;
5                 first.nextWaiter = null;
6             } while (!transferForSignal(first) &&
7                      (first = firstWaiter) != null);
8         }
 1 final boolean transferForSignal(Node node) {
 2         if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
 3             return false;
 4         //将节点从Condition队列移出加入AQS队列
 5         Node p = enq(node);
 6         int ws = p.waitStatus;
 7         if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
 8             LockSupport.unpark(node.thread);
 9         return true;
10     }