# DoubleAccumulator

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/DoubleAccumulator#pubctors)\|[Methods](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#inhmethods)  

# DoubleAccumulator

`
public


class
DoubleAccumulator
`  
`


``

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.DoubleAccumulator |

<br />

*** ** * ** ***

One or more variables that together maintain a running`double`value updated using a supplied function. When updates (method[accumulate(double)](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#accumulate(double))) are contended across threads, the set of variables may grow dynamically to reduce contention. Method[get()](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())(or, equivalently,[doubleValue()](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#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.

The supplied accumulator function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. For predictable results, the accumulator function should be commutative and associative within the floating point tolerance required in usage contexts. The function is applied with an existing value (or identity) as one argument, and a given update as the other argument. For example, to maintain a running maximum value, you could supply`Double::max`along with`Double.NEGATIVE_INFINITY`as the identity. 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.

Class[DoubleAdder](https://developer.android.com/reference/java/util/concurrent/atomic/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)`.

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                                                                                                                                                                                                   ||
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[DoubleAccumulator](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#DoubleAccumulator(java.util.function.DoubleBinaryOperator,%20double))`(`[DoubleBinaryOperator](https://developer.android.com/reference/java/util/function/DoubleBinaryOperator)` accumulatorFunction, double identity) ` Creates a new instance using the given accumulator function and identity element. |

|                                                                                                                                                                                                                 ### Public methods                                                                                                                                                                                                                  ||
|-----------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                               | ` `[accumulate](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#accumulate(double))`(double x) ` Updates with the given value.                                                                                                                                                                                                         |
| ` double`                                                             | ` `[doubleValue](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#doubleValue())`() ` Equivalent to[get()](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get()).                                                                                                                                |
| ` float`                                                              | ` `[floatValue](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#floatValue())`() ` Returns the[current value](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())as a`float`after a narrowing primitive conversion.                                                                           |
| ` double`                                                             | ` `[get](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())`() ` Returns the current value.                                                                                                                                                                                                                                        |
| ` double`                                                             | ` `[getThenReset](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#getThenReset())`() ` Equivalent in effect to[get()](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())followed by[reset()](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#reset()). |
| ` int`                                                                | ` `[intValue](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#intValue())`() ` Returns the[current value](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())as an`int`after a narrowing primitive conversion.                                                                                |
| ` long`                                                               | ` `[longValue](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#longValue())`() ` Returns the[current value](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())as a`long`after a narrowing primitive conversion.                                                                              |
| ` void`                                                               | ` `[reset](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#reset())`() ` Resets variables maintaining updates to the identity value.                                                                                                                                                                                                   |
| ` `[String](https://developer.android.com/reference/java/lang/String) | ` `[toString](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#toString())`() ` Returns the String representation of the current value.                                                                                                                                                                                                 |

| ### 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

### DoubleAccumulator

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

```
public DoubleAccumulator (DoubleBinaryOperator accumulatorFunction, 
                double identity)
```

Creates a new instance using the given accumulator function and identity element.

<br />

|                                             Parameters                                             ||
|-----------------------|-----------------------------------------------------------------------------|
| `accumulatorFunction` | `DoubleBinaryOperator`: a side-effect-free function of two arguments <br /> |
| `identity`            | `double`: identity (initial value) for the accumulator function <br />      |

## Public methods

### accumulate

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

```
public void accumulate (double x)
```

Updates with the given value.

<br />

|           Parameters            ||
|-----|----------------------------|
| `x` | `double`: the value <br /> |

### doubleValue

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

```
public double doubleValue ()
```

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

<br />

|              Returns               ||
|----------|--------------------------|
| `double` | the current value <br /> |

### floatValue

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

```
public float floatValue ()
```

Returns the[current value](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())as a`float`after a narrowing primitive conversion.

<br />

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

### get

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

```
public double get ()
```

Returns the current value. 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 value is being calculated might not be incorporated.

<br />

|              Returns               ||
|----------|--------------------------|
| `double` | the current value <br /> |

### getThenReset

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

```
public double getThenReset ()
```

Equivalent in effect to[get()](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())followed by[reset()](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#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                 ||
|----------|-------------------------------|
| `double` | the value before reset <br /> |

### intValue

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

```
public int intValue ()
```

Returns the[current value](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())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 ()
```

Returns the[current value](https://developer.android.com/reference/java/util/concurrent/atomic/DoubleAccumulator#get())as a`long`after a narrowing primitive conversion.

<br />

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

### reset

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

```
public void reset ()
```

Resets variables maintaining updates to the identity value. This method may be a useful alternative to creating a new updater, 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 />

### 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 current value.

<br />

|                                                          Returns                                                          ||
|--------------------------------------------------------------------|-------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | the String representation of the current value <br /> |