6
如何更新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;
}
請貼沒有任何編譯錯誤,即工作代碼.. – SMA
您發佈的代碼片段沒有意義。你首先更新'ai'(不管你用.update()表示的意思),並且用新的檢查值。 – SubOptimal
這是一個很好的問題,它非常有意義。對於批評:OP不可能爲此編寫工作代碼。如果他這樣做了,他需要知道答案,所以他只能發佈僞代碼。 –