rt/emul/compact/src/main/java/java/util/concurrent/atomic/package-info.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:40:35 +0200
branchjdk7-b147
changeset 1334 588d5bf7a560
permissions -rw-r--r--
Set of JDK classes needed to run javac
jtulach@1334
     1
/*
jtulach@1334
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334
     3
 *
jtulach@1334
     4
 * This code is free software; you can redistribute it and/or modify it
jtulach@1334
     5
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1334
     6
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1334
     7
 * particular file as subject to the "Classpath" exception as provided
jtulach@1334
     8
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334
     9
 *
jtulach@1334
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1334
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1334
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1334
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1334
    14
 * accompanied this code).
jtulach@1334
    15
 *
jtulach@1334
    16
 * You should have received a copy of the GNU General Public License version
jtulach@1334
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1334
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334
    19
 *
jtulach@1334
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1334
    21
 * or visit www.oracle.com if you need additional information or have any
jtulach@1334
    22
 * questions.
jtulach@1334
    23
 */
jtulach@1334
    24
jtulach@1334
    25
/*
jtulach@1334
    26
 * This file is available under and governed by the GNU General Public
jtulach@1334
    27
 * License version 2 only, as published by the Free Software Foundation.
jtulach@1334
    28
 * However, the following notice accompanied the original version of this
jtulach@1334
    29
 * file:
jtulach@1334
    30
 *
jtulach@1334
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jtulach@1334
    32
 * Expert Group and released to the public domain, as explained at
jtulach@1334
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jtulach@1334
    34
 */
jtulach@1334
    35
jtulach@1334
    36
/**
jtulach@1334
    37
 * A small toolkit of classes that support lock-free thread-safe
jtulach@1334
    38
 * programming on single variables.  In essence, the classes in this
jtulach@1334
    39
 * package extend the notion of {@code volatile} values, fields, and
jtulach@1334
    40
 * array elements to those that also provide an atomic conditional update
jtulach@1334
    41
 * operation of the form:
jtulach@1334
    42
 *
jtulach@1334
    43
 * <pre>
jtulach@1334
    44
 *   boolean compareAndSet(expectedValue, updateValue);
jtulach@1334
    45
 * </pre>
jtulach@1334
    46
 *
jtulach@1334
    47
 * <p>This method (which varies in argument types across different
jtulach@1334
    48
 * classes) atomically sets a variable to the {@code updateValue} if it
jtulach@1334
    49
 * currently holds the {@code expectedValue}, reporting {@code true} on
jtulach@1334
    50
 * success.  The classes in this package also contain methods to get and
jtulach@1334
    51
 * unconditionally set values, as well as a weaker conditional atomic
jtulach@1334
    52
 * update operation {@code weakCompareAndSet} described below.
jtulach@1334
    53
 *
jtulach@1334
    54
 * <p>The specifications of these methods enable implementations to
jtulach@1334
    55
 * employ efficient machine-level atomic instructions that are available
jtulach@1334
    56
 * on contemporary processors.  However on some platforms, support may
jtulach@1334
    57
 * entail some form of internal locking.  Thus the methods are not
jtulach@1334
    58
 * strictly guaranteed to be non-blocking --
jtulach@1334
    59
 * a thread may block transiently before performing the operation.
jtulach@1334
    60
 *
jtulach@1334
    61
 * <p>Instances of classes
jtulach@1334
    62
 * {@link java.util.concurrent.atomic.AtomicBoolean},
jtulach@1334
    63
 * {@link java.util.concurrent.atomic.AtomicInteger},
jtulach@1334
    64
 * {@link java.util.concurrent.atomic.AtomicLong}, and
jtulach@1334
    65
 * {@link java.util.concurrent.atomic.AtomicReference}
jtulach@1334
    66
 * each provide access and updates to a single variable of the
jtulach@1334
    67
 * corresponding type.  Each class also provides appropriate utility
jtulach@1334
    68
 * methods for that type.  For example, classes {@code AtomicLong} and
jtulach@1334
    69
 * {@code AtomicInteger} provide atomic increment methods.  One
jtulach@1334
    70
 * application is to generate sequence numbers, as in:
jtulach@1334
    71
 *
jtulach@1334
    72
 * <pre>
jtulach@1334
    73
 * class Sequencer {
jtulach@1334
    74
 *   private final AtomicLong sequenceNumber
jtulach@1334
    75
 *     = new AtomicLong(0);
jtulach@1334
    76
 *   public long next() {
jtulach@1334
    77
 *     return sequenceNumber.getAndIncrement();
jtulach@1334
    78
 *   }
jtulach@1334
    79
 * }
jtulach@1334
    80
 * </pre>
jtulach@1334
    81
 *
jtulach@1334
    82
 * <p>The memory effects for accesses and updates of atomics generally
jtulach@1334
    83
 * follow the rules for volatiles, as stated in section 17.4 of
jtulach@1334
    84
 * <cite>The Java&trade; Language Specification</cite>.
jtulach@1334
    85
 *
jtulach@1334
    86
 * <ul>
jtulach@1334
    87
 *
jtulach@1334
    88
 *   <li> {@code get} has the memory effects of reading a
jtulach@1334
    89
 * {@code volatile} variable.
jtulach@1334
    90
 *
jtulach@1334
    91
 *   <li> {@code set} has the memory effects of writing (assigning) a
jtulach@1334
    92
 * {@code volatile} variable.
jtulach@1334
    93
 *
jtulach@1334
    94
 *   <li> {@code lazySet} has the memory effects of writing (assigning)
jtulach@1334
    95
 *   a {@code volatile} variable except that it permits reorderings with
jtulach@1334
    96
 *   subsequent (but not previous) memory actions that do not themselves
jtulach@1334
    97
 *   impose reordering constraints with ordinary non-{@code volatile}
jtulach@1334
    98
 *   writes.  Among other usage contexts, {@code lazySet} may apply when
jtulach@1334
    99
 *   nulling out, for the sake of garbage collection, a reference that is
jtulach@1334
   100
 *   never accessed again.
jtulach@1334
   101
 *
jtulach@1334
   102
 *   <li>{@code weakCompareAndSet} atomically reads and conditionally
jtulach@1334
   103
 *   writes a variable but does <em>not</em>
jtulach@1334
   104
 *   create any happens-before orderings, so provides no guarantees
jtulach@1334
   105
 *   with respect to previous or subsequent reads and writes of any
jtulach@1334
   106
 *   variables other than the target of the {@code weakCompareAndSet}.
jtulach@1334
   107
 *
jtulach@1334
   108
 *   <li> {@code compareAndSet}
jtulach@1334
   109
 *   and all other read-and-update operations such as {@code getAndIncrement}
jtulach@1334
   110
 *   have the memory effects of both reading and
jtulach@1334
   111
 *   writing {@code volatile} variables.
jtulach@1334
   112
 * </ul>
jtulach@1334
   113
 *
jtulach@1334
   114
 * <p>In addition to classes representing single values, this package
jtulach@1334
   115
 * contains <em>Updater</em> classes that can be used to obtain
jtulach@1334
   116
 * {@code compareAndSet} operations on any selected {@code volatile}
jtulach@1334
   117
 * field of any selected class.
jtulach@1334
   118
 *
jtulach@1334
   119
 * {@link java.util.concurrent.atomic.AtomicReferenceFieldUpdater},
jtulach@1334
   120
 * {@link java.util.concurrent.atomic.AtomicIntegerFieldUpdater}, and
jtulach@1334
   121
 * {@link java.util.concurrent.atomic.AtomicLongFieldUpdater} are
jtulach@1334
   122
 * reflection-based utilities that provide access to the associated
jtulach@1334
   123
 * field types.  These are mainly of use in atomic data structures in
jtulach@1334
   124
 * which several {@code volatile} fields of the same node (for
jtulach@1334
   125
 * example, the links of a tree node) are independently subject to
jtulach@1334
   126
 * atomic updates.  These classes enable greater flexibility in how
jtulach@1334
   127
 * and when to use atomic updates, at the expense of more awkward
jtulach@1334
   128
 * reflection-based setup, less convenient usage, and weaker
jtulach@1334
   129
 * guarantees.
jtulach@1334
   130
 *
jtulach@1334
   131
 * <p>The
jtulach@1334
   132
 * {@link java.util.concurrent.atomic.AtomicIntegerArray},
jtulach@1334
   133
 * {@link java.util.concurrent.atomic.AtomicLongArray}, and
jtulach@1334
   134
 * {@link java.util.concurrent.atomic.AtomicReferenceArray} classes
jtulach@1334
   135
 * further extend atomic operation support to arrays of these types.
jtulach@1334
   136
 * These classes are also notable in providing {@code volatile} access
jtulach@1334
   137
 * semantics for their array elements, which is not supported for
jtulach@1334
   138
 * ordinary arrays.
jtulach@1334
   139
 *
jtulach@1334
   140
 * <a name="Spurious">
jtulach@1334
   141
 * <p>The atomic classes also support method {@code weakCompareAndSet},
jtulach@1334
   142
 * which has limited applicability.  On some platforms, the weak version
jtulach@1334
   143
 * may be more efficient than {@code compareAndSet} in the normal case,
jtulach@1334
   144
 * but differs in that any given invocation of the
jtulach@1334
   145
 * {@code weakCompareAndSet} method may return {@code false}
jtulach@1334
   146
 * <em>spuriously</em> (that is, for no apparent reason)</a>.  A
jtulach@1334
   147
 * {@code false} return means only that the operation may be retried if
jtulach@1334
   148
 * desired, relying on the guarantee that repeated invocation when the
jtulach@1334
   149
 * variable holds {@code expectedValue} and no other thread is also
jtulach@1334
   150
 * attempting to set the variable will eventually succeed.  (Such
jtulach@1334
   151
 * spurious failures may for example be due to memory contention effects
jtulach@1334
   152
 * that are unrelated to whether the expected and current values are
jtulach@1334
   153
 * equal.)  Additionally {@code weakCompareAndSet} does not provide
jtulach@1334
   154
 * ordering guarantees that are usually needed for synchronization
jtulach@1334
   155
 * control.  However, the method may be useful for updating counters and
jtulach@1334
   156
 * statistics when such updates are unrelated to the other
jtulach@1334
   157
 * happens-before orderings of a program.  When a thread sees an update
jtulach@1334
   158
 * to an atomic variable caused by a {@code weakCompareAndSet}, it does
jtulach@1334
   159
 * not necessarily see updates to any <em>other</em> variables that
jtulach@1334
   160
 * occurred before the {@code weakCompareAndSet}.  This may be
jtulach@1334
   161
 * acceptable when, for example, updating performance statistics, but
jtulach@1334
   162
 * rarely otherwise.
jtulach@1334
   163
 *
jtulach@1334
   164
 * <p>The {@link java.util.concurrent.atomic.AtomicMarkableReference}
jtulach@1334
   165
 * class associates a single boolean with a reference.  For example, this
jtulach@1334
   166
 * bit might be used inside a data structure to mean that the object
jtulach@1334
   167
 * being referenced has logically been deleted.
jtulach@1334
   168
 *
jtulach@1334
   169
 * The {@link java.util.concurrent.atomic.AtomicStampedReference}
jtulach@1334
   170
 * class associates an integer value with a reference.  This may be
jtulach@1334
   171
 * used for example, to represent version numbers corresponding to
jtulach@1334
   172
 * series of updates.
jtulach@1334
   173
 *
jtulach@1334
   174
 * <p>Atomic classes are designed primarily as building blocks for
jtulach@1334
   175
 * implementing non-blocking data structures and related infrastructure
jtulach@1334
   176
 * classes.  The {@code compareAndSet} method is not a general
jtulach@1334
   177
 * replacement for locking.  It applies only when critical updates for an
jtulach@1334
   178
 * object are confined to a <em>single</em> variable.
jtulach@1334
   179
 *
jtulach@1334
   180
 * <p>Atomic classes are not general purpose replacements for
jtulach@1334
   181
 * {@code java.lang.Integer} and related classes.  They do <em>not</em>
jtulach@1334
   182
 * define methods such as {@code hashCode} and
jtulach@1334
   183
 * {@code compareTo}.  (Because atomic variables are expected to be
jtulach@1334
   184
 * mutated, they are poor choices for hash table keys.)  Additionally,
jtulach@1334
   185
 * classes are provided only for those types that are commonly useful in
jtulach@1334
   186
 * intended applications.  For example, there is no atomic class for
jtulach@1334
   187
 * representing {@code byte}.  In those infrequent cases where you would
jtulach@1334
   188
 * like to do so, you can use an {@code AtomicInteger} to hold
jtulach@1334
   189
 * {@code byte} values, and cast appropriately.
jtulach@1334
   190
 *
jtulach@1334
   191
 * You can also hold floats using
jtulach@1334
   192
 * {@link java.lang.Float#floatToIntBits} and
jtulach@1334
   193
 * {@link java.lang.Float#intBitsToFloat} conversions, and doubles using
jtulach@1334
   194
 * {@link java.lang.Double#doubleToLongBits} and
jtulach@1334
   195
 * {@link java.lang.Double#longBitsToDouble} conversions.
jtulach@1334
   196
 *
jtulach@1334
   197
 * @since 1.5
jtulach@1334
   198
 */
jtulach@1334
   199
package java.util.concurrent.atomic;