Java Reference
In-Depth Information
Here's how to atomically set the minimum between an observed value of 10 and an existing
atomic integer:
int min = atomicInteger.accumulateAndGet(10, Integer::min);
Adders and accumulators
The Java API recommends using the new classes LongAdder, LongAccumulator, Double-Adder,
and DoubleAccumulator instead of the Atomic classes equivalent when multiple threads update
frequently but read less frequently (for example, in the context of statistics). These classes are
designed to grow dynamically to reduce thread contention.
The classes LongAdder and DoubleAdder support operations for additions, whereas
LongAccumulator and DoubleAccumulator are given a function to combine values. For example,
to calculate the sum of several values, you can use a LongAdder as follows.
Listing B.1. LongAdder to calculate the sum of values
Or you can use a LongAccumulator as follows.
Listing B.2. LongAccumulator to calculate the sum of values
B.2.2. ConcurrentHashMap
The ConcurrentHashMap class was introduced to provide a more modern HashMap, which is
concurrent friendly. ConcurrentHashMap allows concurrent add and updates that lock only
certain parts of the internal data structure. Thus, read and write operations have improved
performance compared to the synchronized Hashtable alternative.
 
Search WWH ::




Custom Search