我需要java.util.concurrent.CountDownLatch.countDown()原子般的保证。
我分块调用countDown,finally因此我确信我正确使用了它。尽管我认为应该没有,但有时我会看到一两个突出的闩锁。
(我还没有通过检查Java源代码来验证。)
我需要保证 java.util.concurrent.CountDownLatch.countDown() 是原子的。
我向你保证,它绝对是原子的。如果不是的话,这将是一个严重的错误。我希望如果您调试代码,您会发现代码问题。
(我还没有通过检查Java源代码来验证。)
这是代码跟踪:
public void countDown() {
sync.releaseShared(1);
}
Run Code Online (Sandbox Code Playgroud)
默认实现sync是:
public boolean tryReleaseShared(int releases) {
// Decrement count; signal when transition to zero
for (;;) {
int c = getState();
if (c == 0)
return false;
int nextc = c-1;
if (compareAndSetState(c, nextc))
return nextc == 0;
}
}
Run Code Online (Sandbox Code Playgroud)
compareAndSetState使用Unsafe支持AtomicInteger和许多其他类的同一类的调用。
return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
Run Code Online (Sandbox Code Playgroud)
如果它被打破了,那么很大一部分就java.util.concurrent被打破了。
| 归档时间: |
|
| 查看次数: |
659 次 |
| 最近记录: |