Top Description Fields Constructors Methods
java.util.concurrent.atomic

public Class AtomicLongArray

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.lang.invoke.MethodHandles, .VarHandle, java.util.function.LongBinaryOperator, .LongUnaryOperator

A long array in which elements may be updated atomically. See the VarHandle specification for descriptions of the properties of atomic accesses.
Author
Doug Lea
Since
1.5

Field Summary

Modifier and TypeField and Description
private static final VarHandle
private final long[]
private static final long

Constructor Summary

AccessConstructor and Description
public
AtomicLongArray(int
the length of the array
length
)

Creates a new AtomicLongArray of the given length, with all elements initially zero.

public
AtomicLongArray(long[]
the array to copy elements from
array
)

Creates a new AtomicLongArray with the same length as, and all elements copied from, the given array.

Method Summary

Modifier and TypeMethod and Description
public final long

Returns:

the updated value
accumulateAndGet
(int
the index
i
,
long
the update value
x
,
LongBinaryOperator
a side-effect-free function of two arguments
accumulatorFunction
)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the element at index i with the results of applying the given function to the current and given values, returning the updated value.

public long

Returns:

the updated value
addAndGet
(int
the index
i
,
long
the value to add
delta
)

Atomically adds the given value to the element at index i, with memory effects as specified by VarHandle#getAndAdd.

public final long

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchange
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)

Atomically sets the element at index i to newValue if the element's current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchange.

public final long

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeAcquire
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)

Atomically sets the element at index i to newValue if the element's current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchangeAcquire.

public final long

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeRelease
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)

Atomically sets the element at index i to newValue if the element's current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchangeRelease.

public final boolean

Returns:

true if successful. False return indicates that the actual value was not equal to the expected value.
compareAndSet
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)

Atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#compareAndSet.

public final long

Returns:

the updated value
decrementAndGet
(int
the index
i
)

Atomically decrements the value of the element at index i, with memory effects as specified by VarHandle#getAndAdd.

public final long

Returns:

the current value
get
(int
the index
i
)

Returns the current value of the element at index i, with memory effects as specified by VarHandle#getVolatile.

public final long

Returns:

the value
getAcquire
(int
the index
i
)

Returns the current value of the element at index i, with memory effects as specified by VarHandle#getAcquire.

public final long

Returns:

the previous value
getAndAccumulate
(int
the index
i
,
long
the update value
x
,
LongBinaryOperator
a side-effect-free function of two arguments
accumulatorFunction
)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the element at index i with the results of applying the given function to the current and given values, returning the previous value.

public final long

Returns:

the previous value
getAndAdd
(int
the index
i
,
long
the value to add
delta
)

Atomically adds the given value to the element at index i, with memory effects as specified by VarHandle#getAndAdd.

public final long

Returns:

the previous value
getAndDecrement
(int
the index
i
)

Atomically decrements the value of the element at index i, with memory effects as specified by VarHandle#getAndAdd.

public final long

Returns:

the previous value
getAndIncrement
(int
the index
i
)

Atomically increments the value of the element at index i, with memory effects as specified by VarHandle#getAndAdd.

public final long

Returns:

the previous value
getAndSet
(int
the index
i
,
long
the new value
newValue
)

Atomically sets the element at index i to newValue and returns the old value, with memory effects as specified by VarHandle#getAndSet.

public final long

Returns:

the previous value
getAndUpdate
(int
the index
i
,
LongUnaryOperator
a side-effect-free function
updateFunction
)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the element at index i with the results of applying the given function, returning the previous value.

public final long

Returns:

the value
getOpaque
(int
the index
i
)

Returns the current value of the element at index i, with memory effects as specified by VarHandle#getOpaque.

public final long

Returns:

the value
getPlain
(int
the index
i
)

Returns the current value of the element at index i, with memory semantics of reading as if the variable was declared non-volatile.

public final long

Returns:

the updated value
incrementAndGet
(int
the index
i
)

Atomically increments the value of the element at index i, with memory effects as specified by VarHandle#getAndAdd.

public final void
lazySet(int
the index
i
,
long
the new value
newValue
)

Sets the element at index i to newValue, with memory effects as specified by VarHandle#setRelease.

public final int

Returns:

the length of the array
length
()

Returns the length of the array.

public final void
set(int
the index
i
,
long
the new value
newValue
)

Sets the element at index i to newValue, with memory effects as specified by VarHandle#setVolatile.

public final void
setOpaque(int
the index
i
,
long
the new value
newValue
)

Sets the element at index i to newValue, with memory effects as specified by VarHandle#setOpaque.

public final void
setPlain(int
the index
i
,
long
the new value
newValue
)

Sets the element at index i to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.

public final void
setRelease(int
the index
i
,
long
the new value
newValue
)

Sets the element at index i to newValue, with memory effects as specified by VarHandle#setRelease.

public String

Returns:

the String representation of the current values of array
toString
()

Overrides java.lang.Object.toString.

Returns the String representation of the current values of array.

public final long

Returns:

the updated value
updateAndGet
(int
the index
i
,
LongUnaryOperator
a side-effect-free function
updateFunction
)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the element at index i with the results of applying the given function, returning the updated value.

public final boolean

Returns:

true if successful
weakCompareAndSet
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)
Deprecated since 9. This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet).

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

public final boolean

Returns:

true if successful
weakCompareAndSetAcquire
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetAcquire.

public final boolean

Returns:

true if successful
weakCompareAndSetPlain
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

public final boolean

Returns:

true if successful
weakCompareAndSetRelease
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetRelease.

public final boolean

Returns:

true if successful
weakCompareAndSetVolatile
(int
the index
i
,
long
the expected value
expectedValue
,
long
the new value
newValue
)

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSet.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

AAback to summary
private static final VarHandle AA
arrayback to summary
private final long[] array
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

AtomicLongArrayback to summary
public AtomicLongArray(int length)

Creates a new AtomicLongArray of the given length, with all elements initially zero.

Parameters
length:int

the length of the array

AtomicLongArrayback to summary
public AtomicLongArray(long[] array)

Creates a new AtomicLongArray with the same length as, and all elements copied from, the given array.

Parameters
array:long[]

the array to copy elements from

Exceptions
NullPointerException:
if array is null

Method Detail

accumulateAndGetback to summary
public final long accumulateAndGet(int i, long x, LongBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the element at index i with the results of applying the given function to the current and given values, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value of the element at index i as its first argument, and the given update as the second argument.

Parameters
i:int

the index

x:long

the update value

accumulatorFunction:LongBinaryOperator

a side-effect-free function of two arguments

Returns:long

the updated value

Since
1.8
addAndGetback to summary
public long addAndGet(int i, long delta)

Atomically adds the given value to the element at index i, with memory effects as specified by VarHandle#getAndAdd.

Parameters
i:int

the index

delta:long

the value to add

Returns:long

the updated value

compareAndExchangeback to summary
public final long compareAndExchange(int i, long expectedValue, long newValue)

Atomically sets the element at index i to newValue if the element's current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchange.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:long

the witness value, which will be the same as the expected value if successful

Since
9
compareAndExchangeAcquireback to summary
public final long compareAndExchangeAcquire(int i, long expectedValue, long newValue)

Atomically sets the element at index i to newValue if the element's current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchangeAcquire.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:long

the witness value, which will be the same as the expected value if successful

Since
9
compareAndExchangeReleaseback to summary
public final long compareAndExchangeRelease(int i, long expectedValue, long newValue)

Atomically sets the element at index i to newValue if the element's current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle#compareAndExchangeRelease.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:long

the witness value, which will be the same as the expected value if successful

Since
9
compareAndSetback to summary
public final boolean compareAndSet(int i, long expectedValue, long newValue)

Atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#compareAndSet.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:boolean

true if successful. False return indicates that the actual value was not equal to the expected value.

decrementAndGetback to summary
public final long decrementAndGet(int i)

Atomically decrements the value of the element at index i, with memory effects as specified by VarHandle#getAndAdd.

Equivalent to addAndGet(i, -1).

Parameters
i:int

the index

Returns:long

the updated value

getback to summary
public final long get(int i)

Returns the current value of the element at index i, with memory effects as specified by VarHandle#getVolatile.

Parameters
i:int

the index

Returns:long

the current value

getAcquireback to summary
public final long getAcquire(int i)

Returns the current value of the element at index i, with memory effects as specified by VarHandle#getAcquire.

Parameters
i:int

the index

Returns:long

the value

Since
9
getAndAccumulateback to summary
public final long getAndAccumulate(int i, long x, LongBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the element at index i with the results of applying the given function to the current and given values, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value of the element at index i as its first argument, and the given update as the second argument.

Parameters
i:int

the index

x:long

the update value

accumulatorFunction:LongBinaryOperator

a side-effect-free function of two arguments

Returns:long

the previous value

Since
1.8
getAndAddback to summary
public final long getAndAdd(int i, long delta)

Atomically adds the given value to the element at index i, with memory effects as specified by VarHandle#getAndAdd.

Parameters
i:int

the index

delta:long

the value to add

Returns:long

the previous value

getAndDecrementback to summary
public final long getAndDecrement(int i)

Atomically decrements the value of the element at index i, with memory effects as specified by VarHandle#getAndAdd.

Equivalent to getAndAdd(i, -1).

Parameters
i:int

the index

Returns:long

the previous value

getAndIncrementback to summary
public final long getAndIncrement(int i)

Atomically increments the value of the element at index i, with memory effects as specified by VarHandle#getAndAdd.

Equivalent to getAndAdd(i, 1).

Parameters
i:int

the index

Returns:long

the previous value

getAndSetback to summary
public final long getAndSet(int i, long newValue)

Atomically sets the element at index i to newValue and returns the old value, with memory effects as specified by VarHandle#getAndSet.

Parameters
i:int

the index

newValue:long

the new value

Returns:long

the previous value

getAndUpdateback to summary
public final long getAndUpdate(int i, LongUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the element at index i with the results of applying the given function, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.

Parameters
i:int

the index

updateFunction:LongUnaryOperator

a side-effect-free function

Returns:long

the previous value

Since
1.8
getOpaqueback to summary
public final long getOpaque(int i)

Returns the current value of the element at index i, with memory effects as specified by VarHandle#getOpaque.

Parameters
i:int

the index

Returns:long

the value

Since
9
getPlainback to summary
public final long getPlain(int i)

Returns the current value of the element at index i, with memory semantics of reading as if the variable was declared non-volatile.

Parameters
i:int

the index

Returns:long

the value

Since
9
incrementAndGetback to summary
public final long incrementAndGet(int i)

Atomically increments the value of the element at index i, with memory effects as specified by VarHandle#getAndAdd.

Equivalent to addAndGet(i, 1).

Parameters
i:int

the index

Returns:long

the updated value

lazySetback to summary
public final void lazySet(int i, long newValue)

Sets the element at index i to newValue, with memory effects as specified by VarHandle#setRelease.

Parameters
i:int

the index

newValue:long

the new value

Since
1.6
lengthback to summary
public final int length()

Returns the length of the array.

Returns:int

the length of the array

setback to summary
public final void set(int i, long newValue)

Sets the element at index i to newValue, with memory effects as specified by VarHandle#setVolatile.

Parameters
i:int

the index

newValue:long

the new value

setOpaqueback to summary
public final void setOpaque(int i, long newValue)

Sets the element at index i to newValue, with memory effects as specified by VarHandle#setOpaque.

Parameters
i:int

the index

newValue:long

the new value

Since
9
setPlainback to summary
public final void setPlain(int i, long newValue)

Sets the element at index i to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.

Parameters
i:int

the index

newValue:long

the new value

Since
9
setReleaseback to summary
public final void setRelease(int i, long newValue)

Sets the element at index i to newValue, with memory effects as specified by VarHandle#setRelease.

Parameters
i:int

the index

newValue:long

the new value

Since
9
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String representation of the current values of array.

Returns:String

the String representation of the current values of array

updateAndGetback to summary
public final long updateAndGet(int i, LongUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the element at index i with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.

Parameters
i:int

the index

updateFunction:LongUnaryOperator

a side-effect-free function

Returns:long

the updated value

Since
1.8
weakCompareAndSetback to summary
public final boolean weakCompareAndSet(int i, long expectedValue, long newValue)

Deprecated

since 9.

This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain be used instead.

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:boolean

true if successful

Annotations
@Deprecated
since:9
See Also
weakCompareAndSetPlain
weakCompareAndSetAcquireback to summary
public final boolean weakCompareAndSetAcquire(int i, long expectedValue, long newValue)

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetAcquire.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:boolean

true if successful

Since
9
weakCompareAndSetPlainback to summary
public final boolean weakCompareAndSetPlain(int i, long expectedValue, long newValue)

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:boolean

true if successful

Since
9
weakCompareAndSetReleaseback to summary
public final boolean weakCompareAndSetRelease(int i, long expectedValue, long newValue)

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetRelease.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:boolean

true if successful

Since
9
weakCompareAndSetVolatileback to summary
public final boolean weakCompareAndSetVolatile(int i, long expectedValue, long newValue)

Possibly atomically sets the element at index i to newValue if the element's current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSet.

Parameters
i:int

the index

expectedValue:long

the expected value

newValue:long

the new value

Returns:boolean

true if successful

Since
9