[concurrency-interest] increment()/decrement() methods on atomicInteger ?

Doug Lea dl at cs.oswego.edu
Mon May 30 08:59:31 EDT 2005

Yechiel Feffer wrote:
> Hi
> In some cases it is desirable to increment/decrement an atomic integer, 
> without getting the result (for example- in a set implementation when 
> you add/remove an element (updating an atomic "size" which is just a 
> "recommendation").  Would'nt it be helpful to add
> increment/decrement methods, which donot return a value ?  Assuming  
> that an atomic increment can be delegated to the
> processor level (at least for volatile) - this can save the CAS retry cycle

Consider using the weakCompareAndSet method for this sort of thing.

   void maybeIncrement(AtomicInteger k) {
      int oldValue = k.get();
      k.weakCompareAndSet(oldValue, oldValue+1);

Disclaimer: On x86, amd64, and sparc in hotspot, weakCompareAndSet and 
map to the same instruction, so you won't see much difference.


More information about the Concurrency-interest mailing list