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

public abstract Class AtomicIntegerFieldUpdater<T>

extends Object
Class Inheritance
Known Direct Subclasses
java.util.concurrent.atomic.AtomicIntegerFieldUpdater.AtomicIntegerFieldUpdaterImpl
Type Parameters
<T>
The type of the object holding the updatable field
Imports
java.lang.reflect.Field, .Modifier, java.util.function.IntBinaryOperator, .IntUnaryOperator, jdk.internal.misc.Unsafe, jdk.internal.reflect.CallerSensitive, .Reflection, jdk.internal.vm.annotation.TrustFinalFields, java.lang.invoke.VarHandle

A reflection-based utility that enables atomic updates to designated non-static volatile int fields of designated classes, providing a subset of the functionality of class VarHandle that should be used instead. This class is designed for use in atomic data structures in which several fields of the same node are independently subject to atomic updates.

Note that the guarantees of the compareAndSet method in this class are weaker than in other atomic classes. Because this class cannot ensure that all uses of the field are appropriate for purposes of atomic access, it can guarantee atomicity only with respect to other invocations of compareAndSet and set on the same updater.

Object arguments for parameters of type T that are not instances of the class passed to newUpdater will result in a ClassCastException being thrown.

Author
Doug Lea
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
AtomicIntegerFieldUpdater.AtomicIntegerFieldUpdaterImpl<T>

Standard hotspot implementation using intrinsics.

Constructor Summary

AccessConstructor and Description
protected
AtomicIntegerFieldUpdater()

Protected do-nothing constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public final int

Returns:

the updated value
accumulateAndGet
(T
An object whose field to get and set
obj
,
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 field of the given object managed by this updater with the results of applying the given function to the current and given values, returning the updated value.

public int

Returns:

the updated value
addAndGet
(T
An object whose field to get and set
obj
,
int
the value to add
delta
)

Atomically adds the given value to the current value of the field of the given object managed by this updater.

public abstract boolean

Returns:

true if successful
compareAndSet
(T
An object whose field to conditionally set
obj
,
int
the expected value
expect
,
int
the new value
update
)

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value.

public int

Returns:

the updated value
decrementAndGet
(T
An object whose field to get and set
obj
)

Atomically decrements by one the current value of the field of the given object managed by this updater.

public abstract int

Returns:

the current value
get
(T
An object whose field to get
obj
)

Returns the current value held in the field of the given object managed by this updater.

public final int

Returns:

the previous value
getAndAccumulate
(T
An object whose field to get and set
obj
,
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 field of the given object managed by this updater with the results of applying the given function to the current and given values, returning the previous value.

public int

Returns:

the previous value
getAndAdd
(T
An object whose field to get and set
obj
,
int
the value to add
delta
)

Atomically adds the given value to the current value of the field of the given object managed by this updater.

public int

Returns:

the previous value
getAndDecrement
(T
An object whose field to get and set
obj
)

Atomically decrements by one the current value of the field of the given object managed by this updater.

public int

Returns:

the previous value
getAndIncrement
(T
An object whose field to get and set
obj
)

Atomically increments by one the current value of the field of the given object managed by this updater.

public int

Returns:

the previous value
getAndSet
(T
An object whose field to get and set
obj
,
int
the new value
newValue
)

Atomically sets the field of the given object managed by this updater to the given value and returns the old value.

public final int

Returns:

the previous value
getAndUpdate
(T
An object whose field to get and set
obj
,
IntUnaryOperator
a side-effect-free function
updateFunction
)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater with the results of applying the given function, returning the previous value.

public int

Returns:

the updated value
incrementAndGet
(T
An object whose field to get and set
obj
)

Atomically increments by one the current value of the field of the given object managed by this updater.

public abstract void
lazySet(T
An object whose field to set
obj
,
int
the new value
newValue
)

Eventually sets the field of the given object managed by this updater to the given updated value.

public static <
the type of instances of tclass
U
>
AtomicIntegerFieldUpdater<U>

Returns:

the updater
newUpdater
(Class<U>
the class of the objects holding the field
tclass
,
String
the name of the field to be updated
fieldName
)

Creates and returns an updater for objects with the given field.

public abstract void
set(T
An object whose field to set
obj
,
int
the new value
newValue
)

Sets the field of the given object managed by this updater to the given updated value.

public final int

Returns:

the updated value
updateAndGet
(T
An object whose field to get and set
obj
,
IntUnaryOperator
a side-effect-free function
updateFunction
)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater with the results of applying the given function, returning the updated value.

public abstract boolean

Returns:

true if successful
weakCompareAndSet
(T
An object whose field to conditionally set
obj
,
int
the expected value
expect
,
int
the new value
update
)

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

AtomicIntegerFieldUpdaterback to summary
protected AtomicIntegerFieldUpdater()

Protected do-nothing constructor for use by subclasses.

Method Detail

accumulateAndGetback to summary
public final int accumulateAndGet(T obj, int x, IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater 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 as its first argument, and the given update as the second argument.

Parameters
obj:T

An object whose field to get and set

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 int addAndGet(T obj, int delta)

Atomically adds the given value to the current value of the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get and set

delta:int

the value to add

Returns:int

the updated value

compareAndSetback to summary
public abstract boolean compareAndSet(T obj, int expect, int update)

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value. This method is guaranteed to be atomic with respect to other calls to compareAndSet and set, but not necessarily with respect to other changes in the field.

Parameters
obj:T

An object whose field to conditionally set

expect:int

the expected value

update:int

the new value

Returns:boolean

true if successful

decrementAndGetback to summary
public int decrementAndGet(T obj)

Atomically decrements by one the current value of the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get and set

Returns:int

the updated value

getback to summary
public abstract int get(T obj)

Returns the current value held in the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get

Returns:int

the current value

getAndAccumulateback to summary
public final int getAndAccumulate(T obj, int x, IntBinaryOperator accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater 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 as its first argument, and the given update as the second argument.

Parameters
obj:T

An object whose field to get and set

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 int getAndAdd(T obj, int delta)

Atomically adds the given value to the current value of the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get and set

delta:int

the value to add

Returns:int

the previous value

getAndDecrementback to summary
public int getAndDecrement(T obj)

Atomically decrements by one the current value of the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get and set

Returns:int

the previous value

getAndIncrementback to summary
public int getAndIncrement(T obj)

Atomically increments by one the current value of the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get and set

Returns:int

the previous value

getAndSetback to summary
public int getAndSet(T obj, int newValue)

Atomically sets the field of the given object managed by this updater to the given value and returns the old value.

Parameters
obj:T

An object whose field to get and set

newValue:int

the new value

Returns:int

the previous value

getAndUpdateback to summary
public final int getAndUpdate(T obj, IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater 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
obj:T

An object whose field to get and set

updateFunction:IntUnaryOperator

a side-effect-free function

Returns:int

the previous value

Since
1.8
incrementAndGetback to summary
public int incrementAndGet(T obj)

Atomically increments by one the current value of the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get and set

Returns:int

the updated value

lazySetback to summary
public abstract void lazySet(T obj, int newValue)

Eventually sets the field of the given object managed by this updater to the given updated value.

Parameters
obj:T

An object whose field to set

newValue:int

the new value

Since
1.6
newUpdaterback to summary
public static <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName)

Creates and returns an updater for objects with the given field. The Class argument is needed to check that reflective types and generic types match.

Parameters
<U>
the type of instances of tclass
tclass:Class<U>

the class of the objects holding the field

fieldName:String

the name of the field to be updated

Returns:AtomicIntegerFieldUpdater<U>

the updater

Annotations
@CallerSensitive
Exceptions
IllegalArgumentException:
if the field is not a volatile integer type
RuntimeException:
with a nested reflection-based exception if the class does not hold field or is the wrong type, or the field is inaccessible to the caller according to Java language access control
setback to summary
public abstract void set(T obj, int newValue)

Sets the field of the given object managed by this updater to the given updated value. This operation is guaranteed to act as a volatile store with respect to subsequent invocations of compareAndSet.

Parameters
obj:T

An object whose field to set

newValue:int

the new value

updateAndGetback to summary
public final int updateAndGet(T obj, IntUnaryOperator updateFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater 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
obj:T

An object whose field to get and set

updateFunction:IntUnaryOperator

a side-effect-free function

Returns:int

the updated value

Since
1.8
weakCompareAndSetback to summary
public abstract boolean weakCompareAndSet(T obj, int expect, int update)

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value. This method is guaranteed to be atomic with respect to other calls to compareAndSet and set, but not necessarily with respect to other changes in the field.

This operation may fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.

Parameters
obj:T

An object whose field to conditionally set

expect:int

the expected value

update:int

the new value

Returns:boolean

true if successful

java.util.concurrent.atomic back to summary

private final Class AtomicIntegerFieldUpdater.AtomicIntegerFieldUpdaterImpl<T>

extends AtomicIntegerFieldUpdater<T>
Class Inheritance
Annotations
@TrustFinalFields

Standard hotspot implementation using intrinsics.

Field Summary

Modifier and TypeField and Description
private final Class<?>
cclass

if field is protected, the subclass constructing updater, else the same as tclass

private final long
private final Class<T>
tclass

class holding the field

private static final Unsafe
U

Constructor Summary

AccessConstructor and Description
pack-priv
AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, final String fieldName, final Class<?> caller)

Method Summary

Modifier and TypeMethod and Description
private final void
accessCheck(T obj)

Checks that target argument is instance of cclass.

public final int
addAndGet(T obj, int delta)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.addAndGet.

Atomically adds the given value to the current value of the field of the given object managed by this updater.

public final boolean
compareAndSet(T obj, int expect, int update)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.compareAndSet.

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value.

public final int
decrementAndGet(T obj)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.decrementAndGet.

Atomically decrements by one the current value of the field of the given object managed by this updater.

public final int
get(T obj)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.get.

Returns the current value held in the field of the given object managed by this updater.

public final int
getAndAdd(T obj, int delta)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndAdd.

Atomically adds the given value to the current value of the field of the given object managed by this updater.

public final int
getAndDecrement(T obj)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndDecrement.

Atomically decrements by one the current value of the field of the given object managed by this updater.

public final int
getAndIncrement(T obj)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndIncrement.

Atomically increments by one the current value of the field of the given object managed by this updater.

public final int
getAndSet(T obj, int newValue)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndSet.

Atomically sets the field of the given object managed by this updater to the given value and returns the old value.

public final int
incrementAndGet(T obj)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.incrementAndGet.

Atomically increments by one the current value of the field of the given object managed by this updater.

private static boolean
isAncestor(ClassLoader first, ClassLoader second)

Returns true if the second classloader can be found in the first classloader's delegation chain.

private static boolean
isSamePackage(Class<?> class1, Class<?> class2)

Returns true if the two classes have the same class loader and package qualifier

public final void
lazySet(T obj, int newValue)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.lazySet.

Eventually sets the field of the given object managed by this updater to the given updated value.

public final void
set(T obj, int newValue)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.set.

Sets the field of the given object managed by this updater to the given updated value.

private final void
throwAccessCheckException(T obj)

Throws access exception if accessCheck failed due to protected access, else ClassCastException.

public final boolean
weakCompareAndSet(T obj, int expect, int update)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.weakCompareAndSet.

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value.

Inherited from java.util.concurrent.atomic.AtomicIntegerFieldUpdater:
accumulateAndGetgetAndAccumulategetAndUpdatenewUpdaterupdateAndGet

Field Detail

cclassback to summary
private final Class<?> cclass

if field is protected, the subclass constructing updater, else the same as tclass

offsetback to summary
private final long offset
tclassback to summary
private final Class<T> tclass

class holding the field

Uback to summary
private static final Unsafe U

Constructor Detail

AtomicIntegerFieldUpdaterImplback to summary
pack-priv AtomicIntegerFieldUpdaterImpl(final Class<T> tclass, final String fieldName, final Class<?> caller)

Method Detail

accessCheckback to summary
private final void accessCheck(T obj)

Checks that target argument is instance of cclass. On failure, throws cause.

addAndGetback to summary
public final int addAndGet(T obj, int delta)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.addAndGet.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.addAndGet.

Atomically adds the given value to the current value of the field of the given object managed by this updater.

compareAndSetback to summary
public final boolean compareAndSet(T obj, int expect, int update)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.compareAndSet.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.compareAndSet.

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value. This method is guaranteed to be atomic with respect to other calls to compareAndSet and set, but not necessarily with respect to other changes in the field.

decrementAndGetback to summary
public final int decrementAndGet(T obj)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.decrementAndGet.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.decrementAndGet.

Atomically decrements by one the current value of the field of the given object managed by this updater.

getback to summary
public final int get(T obj)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.get.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.get.

Returns the current value held in the field of the given object managed by this updater.

getAndAddback to summary
public final int getAndAdd(T obj, int delta)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndAdd.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndAdd.

Atomically adds the given value to the current value of the field of the given object managed by this updater.

getAndDecrementback to summary
public final int getAndDecrement(T obj)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndDecrement.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndDecrement.

Atomically decrements by one the current value of the field of the given object managed by this updater.

getAndIncrementback to summary
public final int getAndIncrement(T obj)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndIncrement.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndIncrement.

Atomically increments by one the current value of the field of the given object managed by this updater.

getAndSetback to summary
public final int getAndSet(T obj, int newValue)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndSet.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.getAndSet.

Atomically sets the field of the given object managed by this updater to the given value and returns the old value.

incrementAndGetback to summary
public final int incrementAndGet(T obj)

Overrides java.util.concurrent.atomic.AtomicIntegerFieldUpdater.incrementAndGet.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.incrementAndGet.

Atomically increments by one the current value of the field of the given object managed by this updater.

isAncestorback to summary
private static boolean isAncestor(ClassLoader first, ClassLoader second)

Returns true if the second classloader can be found in the first classloader's delegation chain. Equivalent to the inaccessible: first.isAncestor(second).

isSamePackageback to summary
private static boolean isSamePackage(Class<?> class1, Class<?> class2)

Returns true if the two classes have the same class loader and package qualifier

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

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.lazySet.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.lazySet.

Eventually sets the field of the given object managed by this updater to the given updated value.

setback to summary
public final void set(T obj, int newValue)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.set.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.set.

Sets the field of the given object managed by this updater to the given updated value. This operation is guaranteed to act as a volatile store with respect to subsequent invocations of compareAndSet.

throwAccessCheckExceptionback to summary
private final void throwAccessCheckException(T obj)

Throws access exception if accessCheck failed due to protected access, else ClassCastException.

weakCompareAndSetback to summary
public final boolean weakCompareAndSet(T obj, int expect, int update)

Implements abstract java.util.concurrent.atomic.AtomicIntegerFieldUpdater.weakCompareAndSet.

Doc from java.util.concurrent.atomic.AtomicIntegerFieldUpdater.weakCompareAndSet.

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value. This method is guaranteed to be atomic with respect to other calls to compareAndSet and set, but not necessarily with respect to other changes in the field.

This operation may fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.