[concurrency-interest] {Long, Double}Adder, {Long, Double}MaxUpdater

Doug Lea dl at cs.oswego.edu
Tue Aug 2 14:54:36 EDT 2011

To help round out the set of tools needed for most
problems needing scalable scalar updates, jsr166e
now also contains a Double version of adder, as well as
Long and Double versions of updaters for maintaining
a running maximum. They all have similar APIs, that
now extend java.lang.Number.

The case for releasing versions for Doubles was
especially strong since the only supported alternative
was using locks. We do not support an AtomicDouble
class because the atomic notion of compareAndSet
(which relies on bitwise equality) clashes with
double-precision "==" equals (which does not strictly
rely on bitwise equality, for example for NaN and
+0.0 vs -0.0). However, we can and do still use
bitwise-equals-based CAS internally in DoubleAdder just to
manage contention, without any visible external impact.
The only byproduct is that interconversion overhead slows
down uncontended performance just barely enough to notice,
but performance under contention is vastly better
than alternatives.

You cannot mix sum and max computations in the same objects,
because these classes internally rely on a form of
reduction requiring associative operations with identity.
(If and when lambdas and scalar function types become
available, we might consider a more general form that
supports any monoid. But sum and max are by far the most
common forms so might as well in any case remain specially
coded. Note that you can also use MaxUpdaters for tracking a
minimum just by negating arguments, and can use Adders for
double products by taking logs.

     *  API specs:  http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166edocs/
     * jar file: http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166e.jar (compiled 
using Java7 javac).
     * Browsable CVS sources: 
     * Browsable CVS test file sources: 

More information about the Concurrency-interest mailing list