blob: 4a4375d4ef2152e239a551584b872162d3603957 [file] [log] [blame]
The Android Open Source Project0eec4642012-04-01 00:00:00 -07001/*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 */
6
7/**
8 * A small toolkit of classes that support lock-free thread-safe
9 * programming on single variables. In essence, the classes in this
10 * package extend the notion of {@code volatile} values, fields, and
11 * array elements to those that also provide an atomic conditional update
12 * operation of the form:
13 *
14 * <pre>
15 * boolean compareAndSet(expectedValue, updateValue);
16 * </pre>
17 *
18 * <p>This method (which varies in argument types across different
19 * classes) atomically sets a variable to the {@code updateValue} if it
20 * currently holds the {@code expectedValue}, reporting {@code true} on
21 * success. The classes in this package also contain methods to get and
22 * unconditionally set values, as well as a weaker conditional atomic
23 * update operation {@code weakCompareAndSet} described below.
24 *
25 * <p>The specifications of these methods enable implementations to
26 * employ efficient machine-level atomic instructions that are available
27 * on contemporary processors. However on some platforms, support may
28 * entail some form of internal locking. Thus the methods are not
29 * strictly guaranteed to be non-blocking --
30 * a thread may block transiently before performing the operation.
31 *
32 * <p>Instances of classes
33 * {@link java.util.concurrent.atomic.AtomicBoolean},
34 * {@link java.util.concurrent.atomic.AtomicInteger},
35 * {@link java.util.concurrent.atomic.AtomicLong}, and
36 * {@link java.util.concurrent.atomic.AtomicReference}
37 * each provide access and updates to a single variable of the
38 * corresponding type. Each class also provides appropriate utility
39 * methods for that type. For example, classes {@code AtomicLong} and
40 * {@code AtomicInteger} provide atomic increment methods. One
41 * application is to generate sequence numbers, as in:
42 *
43 * <pre>
44 * class Sequencer {
45 * private final AtomicLong sequenceNumber
46 * = new AtomicLong(0);
47 * public long next() {
48 * return sequenceNumber.getAndIncrement();
49 * }
50 * }
51 * </pre>
52 *
53 * <p>The memory effects for accesses and updates of atomics generally
54 * follow the rules for volatiles, as stated in
55 * <a href="http://java.sun.com/docs/books/jls/"> The Java Language
56 * Specification, Third Edition (17.4 Memory Model)</a>:
57 *
58 * <ul>
59 *
60 * <li> {@code get} has the memory effects of reading a
61 * {@code volatile} variable.
62 *
63 * <li> {@code set} has the memory effects of writing (assigning) a
64 * {@code volatile} variable.
65 *
66 * <li> {@code lazySet} has the memory effects of writing (assigning)
67 * a {@code volatile} variable except that it permits reorderings with
68 * subsequent (but not previous) memory actions that do not themselves
69 * impose reordering constraints with ordinary non-{@code volatile}
70 * writes. Among other usage contexts, {@code lazySet} may apply when
71 * nulling out, for the sake of garbage collection, a reference that is
72 * never accessed again.
73 *
74 * <li>{@code weakCompareAndSet} atomically reads and conditionally
75 * writes a variable but does <em>not</em>
76 * create any happens-before orderings, so provides no guarantees
77 * with respect to previous or subsequent reads and writes of any
78 * variables other than the target of the {@code weakCompareAndSet}.
79 *
80 * <li> {@code compareAndSet}
81 * and all other read-and-update operations such as {@code getAndIncrement}
82 * have the memory effects of both reading and
83 * writing {@code volatile} variables.
84 * </ul>
85 *
86 * <p>In addition to classes representing single values, this package
87 * contains <em>Updater</em> classes that can be used to obtain
88 * {@code compareAndSet} operations on any selected {@code volatile}
89 * field of any selected class.
90 *
91 * {@link java.util.concurrent.atomic.AtomicReferenceFieldUpdater},
92 * {@link java.util.concurrent.atomic.AtomicIntegerFieldUpdater}, and
93 * {@link java.util.concurrent.atomic.AtomicLongFieldUpdater} are
94 * reflection-based utilities that provide access to the associated
95 * field types. These are mainly of use in atomic data structures in
96 * which several {@code volatile} fields of the same node (for
97 * example, the links of a tree node) are independently subject to
98 * atomic updates. These classes enable greater flexibility in how
99 * and when to use atomic updates, at the expense of more awkward
100 * reflection-based setup, less convenient usage, and weaker
101 * guarantees.
102 *
103 * <p>The
104 * {@link java.util.concurrent.atomic.AtomicIntegerArray},
105 * {@link java.util.concurrent.atomic.AtomicLongArray}, and
106 * {@link java.util.concurrent.atomic.AtomicReferenceArray} classes
107 * further extend atomic operation support to arrays of these types.
108 * These classes are also notable in providing {@code volatile} access
109 * semantics for their array elements, which is not supported for
110 * ordinary arrays.
111 *
112 * <a name="Spurious">
113 * <p>The atomic classes also support method {@code weakCompareAndSet},
114 * which has limited applicability. On some platforms, the weak version
115 * may be more efficient than {@code compareAndSet} in the normal case,
116 * but differs in that any given invocation of the
117 * {@code weakCompareAndSet} method may return {@code false}
118 * <em>spuriously</em> (that is, for no apparent reason)</a>. A
119 * {@code false} return means only that the operation may be retried if
120 * desired, relying on the guarantee that repeated invocation when the
121 * variable holds {@code expectedValue} and no other thread is also
122 * attempting to set the variable will eventually succeed. (Such
123 * spurious failures may for example be due to memory contention effects
124 * that are unrelated to whether the expected and current values are
125 * equal.) Additionally {@code weakCompareAndSet} does not provide
126 * ordering guarantees that are usually needed for synchronization
127 * control. However, the method may be useful for updating counters and
128 * statistics when such updates are unrelated to the other
129 * happens-before orderings of a program. When a thread sees an update
130 * to an atomic variable caused by a {@code weakCompareAndSet}, it does
131 * not necessarily see updates to any <em>other</em> variables that
132 * occurred before the {@code weakCompareAndSet}. This may be
133 * acceptable when, for example, updating performance statistics, but
134 * rarely otherwise.
135 *
136 * <p>The {@link java.util.concurrent.atomic.AtomicMarkableReference}
137 * class associates a single boolean with a reference. For example, this
138 * bit might be used inside a data structure to mean that the object
139 * being referenced has logically been deleted.
140 *
141 * The {@link java.util.concurrent.atomic.AtomicStampedReference}
142 * class associates an integer value with a reference. This may be
143 * used for example, to represent version numbers corresponding to
144 * series of updates.
145 *
146 * <p>Atomic classes are designed primarily as building blocks for
147 * implementing non-blocking data structures and related infrastructure
148 * classes. The {@code compareAndSet} method is not a general
149 * replacement for locking. It applies only when critical updates for an
150 * object are confined to a <em>single</em> variable.
151 *
152 * <p>Atomic classes are not general purpose replacements for
153 * {@code java.lang.Integer} and related classes. They do <em>not</em>
154 * define methods such as {@code hashCode} and
155 * {@code compareTo}. (Because atomic variables are expected to be
156 * mutated, they are poor choices for hash table keys.) Additionally,
157 * classes are provided only for those types that are commonly useful in
158 * intended applications. For example, there is no atomic class for
159 * representing {@code byte}. In those infrequent cases where you would
160 * like to do so, you can use an {@code AtomicInteger} to hold
161 * {@code byte} values, and cast appropriately.
162 *
163 * You can also hold floats using
164 * {@link java.lang.Float#floatToIntBits} and
165 * {@link java.lang.Float#intBitsToFloat} conversions, and doubles using
166 * {@link java.lang.Double#doubleToLongBits} and
167 * {@link java.lang.Double#longBitsToDouble} conversions.
168 *
169 * @since 1.5
170 */
171package java.util.concurrent.atomic;