Java Reference
In-Depth Information
Method
Description
decrement()
(
LongAdder
only.) Equivalent to
add(-1)
.
Returns the
sum()
as a double value (after performing widening
primitive conversion on
LongAdder
).
doubleValue()
floatValue()
Returns the
sum()
as a float value after performing a widening prim-
itive conversion.
(
LongAdder
only.) Equivalent to
add(1)
.
increment()
Returns the
sum()
as an
int
value after performing a narrowing con-
version.
intValue()
Returns the
sum()
as a long value (after performing narrowing con-
version on
DoubleAdder
).
longValue()
Resets the variable's values to zero.
reset()
Returns the current summed value.
sum()
sumThenReset()
Returns the current summed value and then resets the variable's values
to zero.
toString()
Returns the
String
representation of the summed value.
Tip
In the same family as
DoubleAdder
and
LongAdder
are the
DoubleAc-
cumulator
and
LongAccumulator
classes. These classes allow one or more vari-
ables that are being maintained across threads to be updated using a supplied function.
Both of these classes accept an
accumulator
function as the first argument and an
identity as the second argument. When updates are applied across the thread, the set of
variables used to perform the calculations may grow dynamically to reduce contention.
For more information regarding these classes, which are new to Java 8, refer to the on-
line documentation:
http://docs.oracle.com/javase/8/docs/api/
Summary
It is important to understand the fundamentals of concurrency when developing applic-
ations. There is nothing worse than testing an application successfully, and then having
it fail with a deadlock once it is released into production. This chapter started with the
basics, demonstrating how to spawn a background task. It then went on to cover vari-