# LongAdder

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#pubctors)\|[Methods](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#inhmethods)  

# LongAdder

`
public


class
LongAdder
`  
`


``

extends `[Number](https://developer.android.com/reference/java/lang/Number)`


``


implements

`[Serializable](https://developer.android.com/reference/java/io/Serializable)`


`

|---|---|---------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.lang.Number](https://developer.android.com/reference/java/lang/Number) ||
|   | ↳ | java.util.concurrent.atomic.LongAdder |

<br />

*** ** * ** ***

One or more variables that together maintain an initially zero`long`sum. When updates (method[add(long)](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#add(long))) are contended across threads, the set of variables may grow dynamically to reduce contention. Method[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())(or, equivalently,[longValue()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#longValue())) returns the current total combined across the variables maintaining the sum.

This class is usually preferable to[AtomicLong](https://developer.android.com/reference/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[ConcurrentHashMap](https://developer.android.com/reference/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<String,LongAdder> freqs`, initializing if not already present, you can use`freqs.computeIfAbsent(key, k -> new LongAdder()).increment();`

This class extends[Number](https://developer.android.com/reference/java/lang/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.

## Summary

|                                                                     ### Public constructors                                                                     ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[LongAdder](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#LongAdder())`() ` Creates a new adder with initial sum of zero. |

|                                                                                                                                                                                                     ### Public methods                                                                                                                                                                                                      ||
|-----------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                               | ` `[add](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#add(long))`(long x) ` Adds the given value.                                                                                                                                                                                                                   |
| ` void`                                                               | ` `[decrement](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#decrement())`() ` Equivalent to`add(-1)`.                                                                                                                                                                                                               |
| ` double`                                                             | ` `[doubleValue](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#doubleValue())`() ` Returns the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())as a`double`after a widening primitive conversion.                                                                         |
| ` float`                                                              | ` `[floatValue](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#floatValue())`() ` Returns the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())as a`float`after a widening primitive conversion.                                                                            |
| ` void`                                                               | ` `[increment](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#increment())`() ` Equivalent to`add(1)`.                                                                                                                                                                                                                |
| ` int`                                                                | ` `[intValue](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#intValue())`() ` Returns the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())as an`int`after a narrowing primitive conversion.                                                                                |
| ` long`                                                               | ` `[longValue](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#longValue())`() ` Equivalent to[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum()).                                                                                                                            |
| ` void`                                                               | ` `[reset](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#reset())`() ` Resets variables maintaining the sum to zero.                                                                                                                                                                                                 |
| ` long`                                                               | ` `[sum](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())`() ` Returns the current sum.                                                                                                                                                                                                                          |
| ` long`                                                               | ` `[sumThenReset](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sumThenReset())`() ` Equivalent in effect to[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())followed by[reset()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#reset()). |
| ` `[String](https://developer.android.com/reference/java/lang/String) | ` `[toString](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#toString())`() ` Returns the String representation of the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum()).                                                                                                   |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Number](https://developer.android.com/reference/java/lang/Number)` ` |--------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------| | ` byte`            | ` `[byteValue](https://developer.android.com/reference/java/lang/Number#byteValue())`() ` Returns the value of the specified number as a`byte`.       | | ` abstract double` | ` `[doubleValue](https://developer.android.com/reference/java/lang/Number#doubleValue())`() ` Returns the value of the specified number as a`double`. | | ` abstract float`  | ` `[floatValue](https://developer.android.com/reference/java/lang/Number#floatValue())`() ` Returns the value of the specified number as a`float`.    | | ` abstract int`    | ` `[intValue](https://developer.android.com/reference/java/lang/Number#intValue())`() ` Returns the value of the specified number as an`int`.         | | ` abstract long`   | ` `[longValue](https://developer.android.com/reference/java/lang/Number#longValue())`() ` Returns the value of the specified number as a`long`.       | | ` short`           | ` `[shortValue](https://developer.android.com/reference/java/lang/Number#shortValue())`() ` Returns the value of the specified number as a`short`.    | ||
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### LongAdder

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public LongAdder ()
```

Creates a new adder with initial sum of zero.

<br />

## Public methods

### add

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void add (long x)
```

Adds the given value.

<br />

|              Parameters              ||
|-----|---------------------------------|
| `x` | `long`: the value to add <br /> |

### decrement

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void decrement ()
```

Equivalent to`add(-1)`.

<br />

### doubleValue

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public double doubleValue ()
```

Returns the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())as a`double`after a widening primitive conversion.

<br />

|                                             Returns                                             ||
|----------|---------------------------------------------------------------------------------------|
| `double` | the numeric value represented by this object after conversion to type`double`. <br /> |

### floatValue

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public float floatValue ()
```

Returns the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())as a`float`after a widening primitive conversion.

<br />

|                                            Returns                                            ||
|---------|--------------------------------------------------------------------------------------|
| `float` | the numeric value represented by this object after conversion to type`float`. <br /> |

### increment

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void increment ()
```

Equivalent to`add(1)`.

<br />

### intValue

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int intValue ()
```

Returns the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())as an`int`after a narrowing primitive conversion.

<br />

|                                          Returns                                          ||
|-------|------------------------------------------------------------------------------------|
| `int` | the numeric value represented by this object after conversion to type`int`. <br /> |

### longValue

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public long longValue ()
```

Equivalent to[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum()).

<br />

|        Returns         ||
|--------|----------------|
| `long` | the sum <br /> |

### reset

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void reset ()
```

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.

<br />

### sum

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public long 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.

<br />

|        Returns         ||
|--------|----------------|
| `long` | the sum <br /> |

### sumThenReset

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public long sumThenReset ()
```

Equivalent in effect to[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum())followed by[reset()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#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.

<br />

|        Returns         ||
|--------|----------------|
| `long` | the sum <br /> |

### toString

Added in[API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString ()
```

Returns the String representation of the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum()).

<br />

|                                                                                                 Returns                                                                                                 ||
|--------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the String representation of the[sum()](https://developer.android.com/reference/java/util/concurrent/atomic/LongAdder#sum()) <br /> |