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; // help GC
failed = false;
return interrupted;
}
if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}当我阅读Jdk1.8的AbstractQueuedSynchronizer源代码时,我想到了AbstractQueuedSynchronizer的acquireQueued方法,对于一个很长的time.On条件,调用将进入‘time.On’块,cancelAcquire方法?跳出循环必须是'return‘逻辑,其中’time.On‘被设置为false。
发布于 2018-07-26 21:28:41
我猜cancelAcquire()会在tryAcquire(arg)抛出IllegalMonitorStateException之后见面。
发布于 2019-08-23 16:42:48
如果当前线程争用锁被中断,那么If条件将保持为真:
if (shouldParkAfterFailedAcquire(p,node) && parkAndCheckInterrupt()) = true;
然后,将执行finally块中的代码。
否则,它将一直被阻塞,直到调用LockSupport.unpark方法。
发布于 2021-01-31 15:27:32
当你重写tryAcquire()时,可能会抛出错误或异常。用final标记acquireQueued()。关于tryAcquire()抛出Error的示例类ReentrantLock.FairSync
https://stackoverflow.com/questions/50732104
复制相似问题