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

public abstract Class AtomicLongFieldUpdater<T>

extends Object
Class Inheritance
Known Direct Subclasses
java.util.concurrent.atomic.AtomicLongFieldUpdater.CASUpdater
Type Parameters
<T>
The type of the object holding the updatable field
Imports
java.lang.reflect.Field, .Modifier, java.security.AccessController, .PrivilegedActionException, .PrivilegedExceptionAction, java.util.function.LongBinaryOperator, .LongUnaryOperator, jdk.internal.misc.Unsafe, jdk.internal.reflect.CallerSensitive, .Reflection, java.lang.invoke.VarHandle

A reflection-based utility that enables atomic updates to designated volatile long fields of designated classes. 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

Constructor Summary

AccessConstructor and Description
protected
AtomicLongFieldUpdater()

Protected do-nothing constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public final long

Returns:

the updated value
accumulateAndGet
(T
An object whose field to get and set
obj
,
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 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 long

Returns:

the updated value
addAndGet
(T
An object whose field to get and set
obj
,
long
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
,
long
the expected value
expect
,
long
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 long

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 long

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 long

Returns:

the previous value
getAndAccumulate
(T
An object whose field to get and set
obj
,
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 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 long

Returns:

the previous value
getAndAdd
(T
An object whose field to get and set
obj
,
long
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 long

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 long

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 long

Returns:

the previous value
getAndSet
(T
An object whose field to get and set
obj
,
long
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 long

Returns:

the previous value
getAndUpdate
(T
An object whose field to get and set
obj
,
LongUnaryOperator
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 long

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.

pack-priv static boolean
isAncestor(ClassLoader first, ClassLoader second)

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

pack-priv static boolean
isSamePackage(Class<?> class1, Class<?> class2)

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

public abstract void
lazySet(T
An object whose field to set
obj
,
long
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
>
AtomicLongFieldUpdater<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
,
long
the new value
newValue
)

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

public final long

Returns:

the updated value
updateAndGet
(T
An object whose field to get and set
obj
,
LongUnaryOperator
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
,
long
the expected value
expect
,
long
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

AtomicLongFieldUpdaterback to summary
protected AtomicLongFieldUpdater()

Protected do-nothing constructor for use by subclasses.

Method Detail

accumulateAndGetback to summary
public final long accumulateAndGet(T obj, long x, LongBinaryOperator 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: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(T obj, long 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:long

the value to add

Returns:long

the updated value

compareAndSetback to summary
public abstract boolean compareAndSet(T obj, long expect, long 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:long

the expected value

update:long

the new value

Returns:boolean

true if successful

decrementAndGetback to summary
public long 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:long

the updated value

getback to summary
public abstract long 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:long

the current value

getAndAccumulateback to summary
public final long getAndAccumulate(T obj, long x, LongBinaryOperator 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: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 long getAndAdd(T obj, long 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:long

the value to add

Returns:long

the previous value

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

the previous value

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

the previous value

getAndSetback to summary
public long getAndSet(T obj, long 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:long

the new value

Returns:long

the previous value

getAndUpdateback to summary
public final long getAndUpdate(T obj, LongUnaryOperator 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:LongUnaryOperator

a side-effect-free function

Returns:long

the previous value

Since
1.8
incrementAndGetback to summary
public long 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:long

the updated value

isAncestorback to summary
pack-priv 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
pack-priv 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 abstract void lazySet(T obj, long 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:long

the new value

Since
1.6
newUpdaterback to summary
public static <U> AtomicLongFieldUpdater<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:AtomicLongFieldUpdater<U>

the updater

Annotations
@CallerSensitive
Exceptions
IllegalArgumentException:
if the field is not a volatile long 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, long 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:long

the new value

updateAndGetback to summary
public final long updateAndGet(T obj, LongUnaryOperator 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:LongUnaryOperator

a side-effect-free function

Returns:long

the updated value

Since
1.8
weakCompareAndSetback to summary
public abstract boolean weakCompareAndSet(T obj, long expect, long 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:long

the expected value

update:long

the new value

Returns:boolean

true if successful

java.util.concurrent.atomic back to summary

private final Class AtomicLongFieldUpdater.CASUpdater<T>

extends AtomicLongFieldUpdater<T>
Class Inheritance

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
CASUpdater(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 long
addAndGet(T
An object whose field to get and set
obj
,
long
the value to add
delta
)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.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
An object whose field to conditionally set
obj
,
long
the expected value
expect
,
long
the new value
update
)

Implements abstract java.util.concurrent.atomic.AtomicLongFieldUpdater.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 long
decrementAndGet(T
An object whose field to get and set
obj
)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.decrementAndGet.

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

public final long
get(T
An object whose field to get
obj
)

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

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

public final long
getAndAdd(T
An object whose field to get and set
obj
,
long
the value to add
delta
)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.getAndAdd.

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

public final long
getAndDecrement(T
An object whose field to get and set
obj
)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.getAndDecrement.

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

public final long
getAndIncrement(T
An object whose field to get and set
obj
)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.getAndIncrement.

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

public final long
getAndSet(T
An object whose field to get and set
obj
,
long
the new value
newValue
)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.getAndSet.

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

public final long
incrementAndGet(T
An object whose field to get and set
obj
)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.incrementAndGet.

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

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

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

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

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

Implements abstract java.util.concurrent.atomic.AtomicLongFieldUpdater.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
An object whose field to conditionally set
obj
,
long
the expected value
expect
,
long
the new value
update
)

Implements abstract java.util.concurrent.atomic.AtomicLongFieldUpdater.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.AtomicLongFieldUpdater:
accumulateAndGetgetAndAccumulategetAndUpdateisAncestorisSamePackagenewUpdaterupdateAndGet

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

CASUpdaterback to summary
pack-priv CASUpdater(final Class<T> tclass, final String fieldName, final Class<?> caller)
Annotations
@SuppressWarnings:removal

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

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.addAndGet.

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

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

the value to add

Returns:long

the updated value

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

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

Doc from java.util.concurrent.atomic.AtomicLongFieldUpdater.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.

Parameters
obj:T

An object whose field to conditionally set

expect:long

the expected value

update:long

the new value

Returns:boolean

true if successful

decrementAndGetback to summary
public final long decrementAndGet(T obj)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.decrementAndGet.

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

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

the updated value

getback to summary
public final long get(T obj)

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

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

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

the current value

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

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.getAndAdd.

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

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

the value to add

Returns:long

the previous value

getAndDecrementback to summary
public final long getAndDecrement(T obj)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.getAndDecrement.

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

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

the previous value

getAndIncrementback to summary
public final long getAndIncrement(T obj)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.getAndIncrement.

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

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

the previous value

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

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.getAndSet.

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

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

the new value

Returns:long

the previous value

incrementAndGetback to summary
public final long incrementAndGet(T obj)

Overrides java.util.concurrent.atomic.AtomicLongFieldUpdater.incrementAndGet.

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

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

the updated value

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

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

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

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

the new value

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

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

Doc from java.util.concurrent.atomic.AtomicLongFieldUpdater.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.

Parameters
obj:T

An object whose field to set

newValue:long

the new value

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, long expect, long update)

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

Doc from java.util.concurrent.atomic.AtomicLongFieldUpdater.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.

Parameters
obj:T

An object whose field to conditionally set

expect:long

the expected value

update:long

the new value

Returns:boolean

true if successful