class LongAdder extends Striped64 with Serializable
One or more variables that together maintain an initially zero
long
sum. When updates (method #add
) are contended
across threads, the set of variables may grow dynamically to reduce
contention. Method #sum
(or, equivalently, #longValue
) returns the current total combined across the
variables maintaining the sum.
This class is usually preferable to java.util.concurrent.atomic.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 java.util.concurrent.ConcurrentHashMap
to maintain a scalable
frequency map (a form of histogram or multiset). For example, to
add a count to a ConcurrentHashMap
,
initializing if not already present, you can use
freqs.computeIfAbsent(key, k -> new LongAdder()).increment();
This class extends Number
, but does not define
methods such as equals
, hashCode
and
compareTo
because instances are expected to be mutated, and so are
not useful as collection keys.
- Since
1.8
- Alphabetic
- By Inheritance
- LongAdder
- Striped64
- Number
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
- new LongAdder()
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
add(x: Long): Unit
Adds the given value.
Adds the given value.
- x
the value to add
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
byteValue(): Byte
- Definition Classes
- Number
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
decrement(): Unit
Equivalent to
add(-1)
.Equivalent to
add(-1)
. -
def
doubleValue(): Double
Returns the
#sum
as adouble
after a widening primitive conversion. -
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
floatValue(): Float
Returns the
#sum
as afloat
after a widening primitive conversion. -
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
def
increment(): Unit
Equivalent to
add(1)
.Equivalent to
add(1)
. -
def
intValue(): Int
Returns the
#sum
as anint
after a narrowing primitive conversion. -
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
longValue(): Long
Equivalent to
#sum
. -
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
def
reset(): Unit
Resets variables maintaining the sum to zero.
Resets variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy, it should only be used when it is known that no threads are concurrently updating.
-
def
shortValue(): Short
- Definition Classes
- Number
-
def
sum(): Long
Returns the current sum.
Returns the current sum. The returned value is NOT an atomic snapshot; invocation in the absence of concurrent updates returns an accurate result, but concurrent updates that occur while the sum is being calculated might not be incorporated.
- returns
the sum
-
def
sumAndReset(): Long
Atomic variant of
#sumThenReset
Atomic variant of
#sumThenReset
- returns
the sum
-
def
sumThenReset(): Long
Equivalent in effect to
#sum
followed by#reset
.Equivalent in effect to
#sum
followed by#reset
. This method may apply for example during quiescent points between multithreaded computations. If there are updates concurrent with this method, the returned value is not guaranteed to be the final value occurring before the reset.- returns
the sum
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
Returns the String representation of the
#sum
. -
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )