java.util.concurrent.atomic (Java SE 9 & JDK 9 ) (original) (raw)

A small toolkit of classes that support lock-free thread-safe programming on single variables. Instances of Atomic classes maintain values that are accessed and updated using methods otherwise available for fields using associated atomic VarHandle operations.

Instances of classesAtomicBoolean,AtomicInteger,AtomicLong, andAtomicReference each provide access and updates to a single variable of the corresponding type. Each class also provides appropriate utility methods for that type. For example, classes AtomicLong andAtomicInteger provide atomic increment methods. One application is to generate sequence numbers, as in:

class Sequencer { private final AtomicLong sequenceNumber = new AtomicLong(0); public long next() { return sequenceNumber.getAndIncrement(); } }

Arbitrary transformations of the contained value are provided both by low-level read-modify-write operations such as compareAndSet and by higher-level methods such as getAndUpdate.

These classes are not general purpose replacements for java.lang.Integer and related classes. They do not define methods such as equals, hashCode and compareTo. Because atomic variables are expected to be mutated, they are poor choices for hash table keys.

TheAtomicIntegerArray,AtomicLongArray, andAtomicReferenceArray classes further extend atomic operation support to arrays of these types. These classes are also notable in providing volatile access semantics for their array elements.

In addition to classes representing single values and arrays, this package contains Updater classes that can be used to obtain compareAndSet and related operations on any selectedvolatile field of any selected class. These classes predate the introduction of VarHandle, and are of more limited use.AtomicReferenceFieldUpdater,AtomicIntegerFieldUpdater, andAtomicLongFieldUpdater are reflection-based utilities that provide access to the associated field types. These are mainly of use in atomic data structures in which several volatile fields of the same node (for example, the links of a tree node) are independently subject to atomic updates. These classes enable greater flexibility in how and when to use atomic updates, at the expense of more awkward reflection-based setup, less convenient usage, and weaker guarantees.

The AtomicMarkableReference class associates a single boolean with a reference. For example, this bit might be used inside a data structure to mean that the object being referenced has logically been deleted. The AtomicStampedReference class associates an integer value with a reference. This may be used for example, to represent version numbers corresponding to series of updates.

Since:

1.5

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2017, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.