/**
* Acquires in shared uninterruptible mode.
* @param arg the acquire argument
*/
private void doAcquireShared(int arg) {
final Node node = addWaiter(Node.SHARED);
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();
if (p == head) {
int r = tryAcquireShared(arg);
if (r >= 0) {
setHeadAndPropagate(node, r);
p.next = null; // help GC
if (interrupted)
selfInterrupt();
failed = false;
return;
}
}
if (shouldParkAfterFailedAcquire(p, node) &&
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
释放锁的操作和Exclusive有点不同,
{@link java.util.concurrent.locks.AbstractQueuedSynchronizer#doAcquireShared}自旋
获取资源的时候,{@link java.util.concurrent.locks.AbstractQueuedSynchronizer#setHeadAndPropagate}
/**
* Sets head of queue, and checks if successor may be waiting
* in