# 1 8 - N U M B E R S - PA R T I V
N U M B E R S
• LongAdder


• LongAccumulator
• DoubleAdder


• DoubleAccumulator
TA K E WAY S
• Thread contention - Essentially thread contention is a condition where one
thread is waiting for a lock/object that is currently being held by another
thread. Therefore, this waiting thread cannot use that object until the other
thread has unlocked that particular object.


• Accumulator is a more generalised version of Adder
L O N G A D D E R
• This class is usually preferable to AtomicLong when multiple threads update a common
sum that is used for purposes such as collecting statistics, not for fine-grained
synchronization control. Under low update contention, the two classes have similar
characteristics. But under high contention, expected throughput of this class is
significantly higher, at the expense of higher space consumption.


• LongAdders can be used with a ConcurrentHashMap to maintain a scalable frequency
map (a form of histogram or multiset). For example, to add a count to a
ConcurrentHashMap<String,LongAdder> freqs, initializing if not already present, you can
use freqs.computeIfAbsent(key, k -> new LongAdder()).increment();,


• Extend Number to allow uniform access by tools and utilities that deal with numerically-
based classes
L O N G A D D E R - E XA M P L E
L O N G AC C U M U L AT O R
• This class is usually preferable to AtomicLong when multiple threads
update a common value that is used for purposes such as collecting
statistics, not for fine-grained synchronization control. Under low update
contention, the two classes have similar characteristics. But under high
contention, expected throughput of this class is significantly higher, at the
expense of higher space consumption.


• Class LongAdder provides analogs of the functionality of this class for the
common special case of maintaining counts and sums. The call new
LongAdder() is equivalent to new LongAccumulator((x, y) -> x + y, 0L).
L O N G AC C U M U L AT O R - E XA M P L E
D O U B L E A D D E R
• One or more variables that together maintain an initially zero double sum. When updates
(method add(double)) are contended across threads, the set of variables may grow
dynamically to reduce contention. Method sum() (or, equivalently doubleValue()) returns
the current total combined across the variables maintaining the sum. The order of
accumulation within or across threads is not guaranteed. Thus, this class may not be
applicable if numerical stability is required, especially when combining values of
substantially different orders of magnitude.


• This class is usually preferable to alternatives when multiple threads update a common
value that is used for purposes such as summary statistics that are frequently updated but
less frequently read.
D O U B L E A D D E R - E XA M P L E
D O U B L E AC C U M U L AT O R
• One or more variables that together maintain a running double value updated
using a supplied function. When updates (method accumulate(double)) are
contended across threads, the set of variables may grow dynamically to
reduce contention. Method get() (or, equivalently, doubleValue()) returns the
current value across the variables maintaining updates.


• This class is usually preferable to alternatives when multiple threads update a
common value that is used for purposes such as summary statistics that are
frequently updated but less frequently read.


• Class DoubleAdder provides analogs of the functionality of this class for the
common special case of maintaining sums. The call new DoubleAdder() is
equivalent to new DoubleAccumulator((x, y) -> x + y, 0.0).
D O U B L E AC C U M U L AT O R - E XA M P L E
R E F E R E N C E S
• [1] - https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/
lang/Number.html


• [2] - https://github.com/luizgustavocosta/16-bits

18 - Java Numbers - part 4

  • 1.
    # 1 8- N U M B E R S - PA R T I V
  • 2.
    N U MB E R S • LongAdder • LongAccumulator • DoubleAdder • DoubleAccumulator
  • 3.
    TA K EWAY S • Thread contention - Essentially thread contention is a condition where one thread is waiting for a lock/object that is currently being held by another thread. Therefore, this waiting thread cannot use that object until the other thread has unlocked that particular object. • Accumulator is a more generalised version of Adder
  • 4.
    L O NG A D D E R • This class is usually preferable to AtomicLong when multiple threads update a common sum that is used for purposes such as collecting statistics, not for fine-grained synchronization control. Under low update contention, the two classes have similar characteristics. But under high contention, expected throughput of this class is significantly higher, at the expense of higher space consumption. • LongAdders can be used with a ConcurrentHashMap to maintain a scalable frequency map (a form of histogram or multiset). For example, to add a count to a ConcurrentHashMap<String,LongAdder> freqs, initializing if not already present, you can use freqs.computeIfAbsent(key, k -> new LongAdder()).increment();, • Extend Number to allow uniform access by tools and utilities that deal with numerically- based classes
  • 5.
    L O NG A D D E R - E XA M P L E
  • 6.
    L O NG AC C U M U L AT O R • This class is usually preferable to AtomicLong when multiple threads update a common value that is used for purposes such as collecting statistics, not for fine-grained synchronization control. Under low update contention, the two classes have similar characteristics. But under high contention, expected throughput of this class is significantly higher, at the expense of higher space consumption. • Class LongAdder provides analogs of the functionality of this class for the common special case of maintaining counts and sums. The call new LongAdder() is equivalent to new LongAccumulator((x, y) -> x + y, 0L).
  • 7.
    L O NG AC C U M U L AT O R - E XA M P L E
  • 8.
    D O UB L E A D D E R • One or more variables that together maintain an initially zero double sum. When updates (method add(double)) are contended across threads, the set of variables may grow dynamically to reduce contention. Method sum() (or, equivalently doubleValue()) returns the current total combined across the variables maintaining the sum. The order of accumulation within or across threads is not guaranteed. Thus, this class may not be applicable if numerical stability is required, especially when combining values of substantially different orders of magnitude. • This class is usually preferable to alternatives when multiple threads update a common value that is used for purposes such as summary statistics that are frequently updated but less frequently read.
  • 9.
    D O UB L E A D D E R - E XA M P L E
  • 10.
    D O UB L E AC C U M U L AT O R • One or more variables that together maintain a running double value updated using a supplied function. When updates (method accumulate(double)) are contended across threads, the set of variables may grow dynamically to reduce contention. Method get() (or, equivalently, doubleValue()) returns the current value across the variables maintaining updates. • This class is usually preferable to alternatives when multiple threads update a common value that is used for purposes such as summary statistics that are frequently updated but less frequently read. • Class DoubleAdder provides analogs of the functionality of this class for the common special case of maintaining sums. The call new DoubleAdder() is equivalent to new DoubleAccumulator((x, y) -> x + y, 0.0).
  • 11.
    D O UB L E AC C U M U L AT O R - E XA M P L E
  • 12.
    R E FE R E N C E S • [1] - https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/ lang/Number.html • [2] - https://github.com/luizgustavocosta/16-bits