2013-10-06 70 views

回答

0
public final int getAndDecrement() 

以原子減一的當前值。返回前值

AtomicInteger itsCounter = new AtomicInteger(); 
itsCounter.getAndDecrement(); 
+0

謝謝,但我實際上是在詢問否定和不遞減的價值 – mindreader

4

我會做這樣

public int getAndNegate(AtomicInteger i) { 
    for (;;) { 
     int current = i.get(); 
     int next = -current; 
     if (i.compareAndSet(current, next)) 
      return current; 
    } 
} 
相關問題