如何在当前值小于给定值时更新 AtomicInteger
?思路如下:
AtomicInteger ai = new AtomicInteger(0);
...
ai.update(threadInt); // this call happens concurrently
...
// inside AtomicInteger atomic operation
synchronized {
if (ai.currentvalue < threadInt)
ai.currentvalue = threadInt;
}
.update()
更新了ai
(不管你用它做什么),然后再检查新值。 - SubOptimal