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

public Class AtomicIntegerArray

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

An int 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 int[]
private static final long

Constructor Summary

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

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

public
AtomicIntegerArray(int[]
the array to copy elements from
array
)

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

Method Summary

Modifier and TypeMethod and Description
public final int

Returns:

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

Returns:

the updated value
addAndGet
(int
the index
i
,
int
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 int

Returns:

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

Returns:

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

Returns:

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

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 int

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 int

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 int

Returns:

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

Returns:

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

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 int

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 int

Returns:

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

Returns:

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

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 int

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 int

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
,
int
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
,
int
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
,
int
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
,
int
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
,
int
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 int

Returns:

the updated value
updateAndGet
(int
the index
i
,
IntUnaryOperator
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
,
int
the expected value
expectedValue
,
int
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
,
int
the expected value
expectedValue
,
int
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
,
int
the expected value
expectedValue
,
int
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
,
int
the expected value
expectedValue
,
int
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
,
int
the expected value
expectedValue
,
int
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 int[] array
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

AtomicIntegerArrayback to summary
public AtomicIntegerArray(int length)

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

Parameters
length:int

the length of the array

AtomicIntegerArrayback to summary
public AtomicIntegerArray(int[] array)

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

Parameters
array:int[]

the array to copy elements from

Exceptions
NullPointerException:
if array is null

Method Detail

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

the update value

accumulatorFunction:IntBinaryOperator

a side-effect-free function of two arguments

Returns:int

the updated value

Since
1.8
addAndGetback to summary
public final int addAndGet(int i, int 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:int

the value to add

Returns:int

the updated value

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

the expected value

newValue:int

the new value

Returns:int

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

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

the expected value

newValue:int

the new value

Returns:int

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

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

the expected value

newValue:int

the new value

Returns:int

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, int expectedValue, int 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:int

the expected value

newValue:int

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 int 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:int

the updated value

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

the current value

getAcquireback to summary
public final int 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:int

the value

Since
9
getAndAccumulateback to summary
public final int getAndAccumulate(int i, int x, IntBinaryOperator 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:int

the update value

accumulatorFunction:IntBinaryOperator

a side-effect-free function of two arguments

Returns:int

the previous value

Since
1.8
getAndAddback to summary
public final int getAndAdd(int i, int 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:int

the value to add

Returns:int

the previous value

getAndDecrementback to summary
public final int 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:int

the previous value

getAndIncrementback to summary
public final int 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:int

the previous value

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

the new value

Returns:int

the previous value

getAndUpdateback to summary
public final int getAndUpdate(int i, IntUnaryOperator 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:IntUnaryOperator

a side-effect-free function

Returns:int

the previous value

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

the value

Since
9
getPlainback to summary
public final int 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:int

the value

Since
9
incrementAndGetback to summary
public final int 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:int

the updated value

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

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

Parameters
i:int

the index

newValue:int

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, int newValue)

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

Parameters
i:int

the index

newValue:int

the new value

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

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

Parameters
i:int

the index

newValue:int

the new value

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

the new value

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

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

Parameters
i:int

the index

newValue:int

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

a side-effect-free function

Returns:int

the updated value

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

the expected value

newValue:int

the new value

Returns:boolean

true if successful

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

the expected value

newValue:int

the new value

Returns:boolean

true if successful

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

the expected value

newValue:int

the new value

Returns:boolean

true if successful

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

the expected value

newValue:int

the new value

Returns:boolean

true if successful

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

the expected value

newValue:int

the new value

Returns:boolean

true if successful

Since
9