AQS相关(lock、unlock、await、signal)
节点状态
| |
| static final int CANCELLED = 1; |
| |
| |
| |
| |
| |
| static final int SIGNAL = -1; |
| |
| |
| |
| |
| |
| static final int CONDITION = -2; |
| |
| |
| |
| static final int PROPAGATE = -3; |
参考资料
lock
方法流程

通过流程可以看出,公平与非公平只针对新线程与同步队列中的线程。
源码:
| final void lock() { |
| |
| acquire(1); |
| } |
| |
| public final void acquire(int arg) { |
| |
| if (!tryAcquire(arg) && |
| |
| acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) |
| |
| selfInterrupt(); |
| } |
| |
| protected final boolean tryAcquire(int acquires) { |
| |
| final Thread current = Thread.currentThread(); |
| |
| int c = getState(); |
| |
| if (c == 0) { |
| |
| if (!hasQueuedPredecessors() && |
| |
| compareAndSetState(0, acquires)) { |
| |
| setExclusiveOwnerThread(current); |
| return true; |
| } |
| } |
| |
| else if (current == getExclusiveOwnerThread()) { |
| int nextc = c + acquires; |
| if (nextc < 0) |
| throw new Error("Maximum lock count exceeded"); |
| setState(nextc); |
| return true; |
| } |
| return false; |
| } |
| final void lock() { |
| |
| if (compareAndSetState(0, 1)) |
| setExclusiveOwnerThread(Thread.currentThread()); |
| else |
| |
| acquire(1); |
| } |
| protected final boolean tryAcquire(int acquires) { |
| return nonfairTryAcquire(acquires); |
| } |
| final boolean nonfairTryAcquire(int acquires) { |
| |
| final Thread current = Thread.currentThread(); |
| int c = getState(); |
| if (c == 0) { |
| |
| if (compareAndSetState(0, acquires)) { |
| setExclusiveOwnerThread(current); |
| return true; |
| } |
| } |
| else if (current == getExclusiveOwnerThread()) { |
| int nextc = c + acquires; |
| if (nextc < 0) |
| throw new Error("Maximum lock count exceeded"); |
| setState(nextc); |
| return true; |
| } |
| return false; |
| } |
| |
| final boolean acquireQueued(final Node node, int arg) { |
| boolean failed = true; |
| try { |
| boolean interrupted = false; |
| for (;;) { |
| |
| final Node p = node.predecessor(); |
| |
| if (p == head && tryAcquire(arg)) { |
| |
| setHead(node); |
| p.next = null; |
| failed = false; |
| return interrupted; |
| } |
| |
| if (shouldParkAfterFailedAcquire(p, node) && |
| parkAndCheckInterrupt()) |
| interrupted = true; |
| } |
| } finally { |
| |
| if (failed) |
| cancelAcquire(node); |
| } |
| } |
wait & signal (等待与唤醒)
阻塞队列中,如果当前队列为空,则会调用 notEmpty.await()
方法,notEmpty
为 ConditionObject
,通常阻塞队列中会有两个 ConditionObject
,一个负责【取(notEmpty)】的线程,一个负责【存(notFull)】的线程。一个 ConditionObject
就是一个等待队列,调用 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) |
| unlinkCancelledWaiters(); |
| if (interruptMode != 0) |
| reportInterruptAfterWait(interruptMode); |
| } |
| 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; |
| } |
| public final boolean release(int arg) { |
| |
| if (tryRelease(arg)) { |
| Node h = head; |
| |
| if (h != null && h.waitStatus != 0) |
| unparkSuccessor(h); |
| return true; |
| } |
| return false; |
| } |