APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.concurrent.atomic

public Class AtomicReferenceArray<E>

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Type Parameters
<E>
The base class of elements held in this array
Imports
jdk.internal.misc.Unsafe, java.lang.invoke.MethodHandles, .VarHandle, java.lang.reflect.Array, java.util.Arrays, java.util.function.BinaryOperator, .UnaryOperator

An array of object references 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 Object[]
private static final long

Constructor Summary

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

Creates a new AtomicReferenceArray of the given length, with all elements initially null.

public
AtomicReferenceArray(E[]
the array to copy elements from
array
)

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

Method Summary

Modifier and TypeMethod and Description
public final E

Returns:

the updated value
accumulateAndGet
(int
the index
i
,
E
the update value
x
,
BinaryOperator<E>
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 final E

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchange
(int
the index
i
,
E
the expected value
expectedValue
,
E
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 E

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeAcquire
(int
the index
i
,
E
the expected value
expectedValue
,
E
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 E

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeRelease
(int
the index
i
,
E
the expected value
expectedValue
,
E
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
,
E
the expected value
expectedValue
,
E
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 E

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 E

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 E

Returns:

the previous value
getAndAccumulate
(int
the index
i
,
E
the update value
x
,
BinaryOperator<E>
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 E

Returns:

the previous value
getAndSet
(int
the index
i
,
E
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 E

Returns:

the previous value
getAndUpdate
(int
the index
i
,
UnaryOperator<E>
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 E

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 E

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 void
lazySet(int
the index
i
,
E
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.

private void
readObject(ObjectInputStream
the stream
s
)

Reconstitutes the instance from a stream (that is, deserializes it).

public final void
set(int
the index
i
,
E
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
,
E
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
,
E
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
,
E
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 E

Returns:

the updated value
updateAndGet
(int
the index
i
,
UnaryOperator<E>
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
,
E
the expected value
expectedValue
,
E
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
,
E
the expected value
expectedValue
,
E
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
,
E
the expected value
expectedValue
,
E
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
,
E
the expected value
expectedValue
,
E
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
,
E
the expected value
expectedValue
,
E
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 Object[] array
Annotations
@SuppressWarnings:serial
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

AtomicReferenceArrayback to summary
public AtomicReferenceArray(int length)

Creates a new AtomicReferenceArray of the given length, with all elements initially null.

Parameters
length:int

the length of the array

AtomicReferenceArrayback to summary
public AtomicReferenceArray(E[] array)

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

Parameters
array:E[]

the array to copy elements from

Exceptions
NullPointerException:
if array is null

Method Detail

accumulateAndGetback to summary
public final E accumulateAndGet(int i, E x, BinaryOperator<E> 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:E

the update value

accumulatorFunction:BinaryOperator<E>

a side-effect-free function of two arguments

Returns:E

the updated value

Since
1.8
compareAndExchangeback to summary
public final E compareAndExchange(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:E

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

Annotations
@SuppressWarnings:unchecked
Since
9
compareAndExchangeAcquireback to summary
public final E compareAndExchangeAcquire(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:E

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

Annotations
@SuppressWarnings:unchecked
Since
9
compareAndExchangeReleaseback to summary
public final E compareAndExchangeRelease(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:E

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

Annotations
@SuppressWarnings:unchecked
Since
9
compareAndSetback to summary
public final boolean compareAndSet(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:boolean

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

getback to summary
public final E 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:E

the current value

Annotations
@SuppressWarnings:unchecked
getAcquireback to summary
public final E 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:E

the value

Annotations
@SuppressWarnings:unchecked
Since
9
getAndAccumulateback to summary
public final E getAndAccumulate(int i, E x, BinaryOperator<E> 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:E

the update value

accumulatorFunction:BinaryOperator<E>

a side-effect-free function of two arguments

Returns:E

the previous value

Since
1.8
getAndSetback to summary
public final E getAndSet(int i, E 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:E

the new value

Returns:E

the previous value

Annotations
@SuppressWarnings:unchecked
getAndUpdateback to summary
public final E getAndUpdate(int i, UnaryOperator<E> 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:UnaryOperator<E>

a side-effect-free function

Returns:E

the previous value

Since
1.8
getOpaqueback to summary
public final E 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:E

the value

Annotations
@SuppressWarnings:unchecked
Since
9
getPlainback to summary
public final E 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:E

the value

Annotations
@SuppressWarnings:unchecked
Since
9
lazySetback to summary
public final void lazySet(int i, E newValue)

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

Parameters
i:int

the index

newValue:E

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

readObjectback to summary
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException

Reconstitutes the instance from a stream (that is, deserializes it).

Parameters
s:ObjectInputStream

the stream

Exceptions
IOException:
if an I/O error occurs
ClassNotFoundException:
if the class of a serialized object could not be found
setback to summary
public final void set(int i, E newValue)

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

Parameters
i:int

the index

newValue:E

the new value

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

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

Parameters
i:int

the index

newValue:E

the new value

Since
9
setPlainback to summary
public final void setPlain(int i, E 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:E

the new value

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

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

Parameters
i:int

the index

newValue:E

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 E updateAndGet(int i, UnaryOperator<E> 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:UnaryOperator<E>

a side-effect-free function

Returns:E

the updated value

Since
1.8
weakCompareAndSetback to summary
public final boolean weakCompareAndSet(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:boolean

true if successful

Annotations
@Deprecated
since:9
See Also
weakCompareAndSetPlain
weakCompareAndSetAcquireback to summary
public final boolean weakCompareAndSetAcquire(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:boolean

true if successful

Since
9
weakCompareAndSetPlainback to summary
public final boolean weakCompareAndSetPlain(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:boolean

true if successful

Since
9
weakCompareAndSetReleaseback to summary
public final boolean weakCompareAndSetRelease(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:boolean

true if successful

Since
9
weakCompareAndSetVolatileback to summary
public final boolean weakCompareAndSetVolatile(int i, E expectedValue, E 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:E

the expected value

newValue:E

the new value

Returns:boolean

true if successful

Since
9