rt/emul/compact/src/main/java/java/util/concurrent/package-info.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:46:31 +0100
branchjdk7-b147
changeset 1890 212417b74b72
permissions -rw-r--r--
Bringing in all concurrent package from JDK7-b147
jaroslav@1890
     1
/*
jaroslav@1890
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890
     3
 *
jaroslav@1890
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1890
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1890
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1890
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890
     9
 *
jaroslav@1890
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1890
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890
    14
 * accompanied this code).
jaroslav@1890
    15
 *
jaroslav@1890
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1890
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890
    19
 *
jaroslav@1890
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1890
    22
 * questions.
jaroslav@1890
    23
 */
jaroslav@1890
    24
jaroslav@1890
    25
/*
jaroslav@1890
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1890
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890
    28
 * However, the following notice accompanied the original version of this
jaroslav@1890
    29
 * file:
jaroslav@1890
    30
 *
jaroslav@1890
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1890
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890
    34
 */
jaroslav@1890
    35
jaroslav@1890
    36
/**
jaroslav@1890
    37
 * Utility classes commonly useful in concurrent programming.  This
jaroslav@1890
    38
 * package includes a few small standardized extensible frameworks, as
jaroslav@1890
    39
 * well as some classes that provide useful functionality and are
jaroslav@1890
    40
 * otherwise tedious or difficult to implement.  Here are brief
jaroslav@1890
    41
 * descriptions of the main components.  See also the
jaroslav@1890
    42
 * {@link java.util.concurrent.locks} and
jaroslav@1890
    43
 * {@link java.util.concurrent.atomic} packages.
jaroslav@1890
    44
 *
jaroslav@1890
    45
 * <h2>Executors</h2>
jaroslav@1890
    46
 *
jaroslav@1890
    47
 * <b>Interfaces.</b>
jaroslav@1890
    48
 *
jaroslav@1890
    49
 * {@link java.util.concurrent.Executor} is a simple standardized
jaroslav@1890
    50
 * interface for defining custom thread-like subsystems, including
jaroslav@1890
    51
 * thread pools, asynchronous IO, and lightweight task frameworks.
jaroslav@1890
    52
 * Depending on which concrete Executor class is being used, tasks may
jaroslav@1890
    53
 * execute in a newly created thread, an existing task-execution thread,
jaroslav@1890
    54
 * or the thread calling {@link java.util.concurrent.Executor#execute
jaroslav@1890
    55
 * execute}, and may execute sequentially or concurrently.
jaroslav@1890
    56
 *
jaroslav@1890
    57
 * {@link java.util.concurrent.ExecutorService} provides a more
jaroslav@1890
    58
 * complete asynchronous task execution framework.  An
jaroslav@1890
    59
 * ExecutorService manages queuing and scheduling of tasks,
jaroslav@1890
    60
 * and allows controlled shutdown.
jaroslav@1890
    61
 *
jaroslav@1890
    62
 * The {@link java.util.concurrent.ScheduledExecutorService}
jaroslav@1890
    63
 * subinterface and associated interfaces add support for
jaroslav@1890
    64
 * delayed and periodic task execution.  ExecutorServices
jaroslav@1890
    65
 * provide methods arranging asynchronous execution of any
jaroslav@1890
    66
 * function expressed as {@link java.util.concurrent.Callable},
jaroslav@1890
    67
 * the result-bearing analog of {@link java.lang.Runnable}.
jaroslav@1890
    68
 *
jaroslav@1890
    69
 * A {@link java.util.concurrent.Future} returns the results of
jaroslav@1890
    70
 * a function, allows determination of whether execution has
jaroslav@1890
    71
 * completed, and provides a means to cancel execution.
jaroslav@1890
    72
 *
jaroslav@1890
    73
 * A {@link java.util.concurrent.RunnableFuture} is a {@code Future}
jaroslav@1890
    74
 * that possesses a {@code run} method that upon execution,
jaroslav@1890
    75
 * sets its results.
jaroslav@1890
    76
 *
jaroslav@1890
    77
 * <p>
jaroslav@1890
    78
 *
jaroslav@1890
    79
 * <b>Implementations.</b>
jaroslav@1890
    80
 *
jaroslav@1890
    81
 * Classes {@link java.util.concurrent.ThreadPoolExecutor} and
jaroslav@1890
    82
 * {@link java.util.concurrent.ScheduledThreadPoolExecutor}
jaroslav@1890
    83
 * provide tunable, flexible thread pools.
jaroslav@1890
    84
 *
jaroslav@1890
    85
 * The {@link java.util.concurrent.Executors} class provides
jaroslav@1890
    86
 * factory methods for the most common kinds and configurations
jaroslav@1890
    87
 * of Executors, as well as a few utility methods for using
jaroslav@1890
    88
 * them.  Other utilities based on {@code Executors} include the
jaroslav@1890
    89
 * concrete class {@link java.util.concurrent.FutureTask}
jaroslav@1890
    90
 * providing a common extensible implementation of Futures, and
jaroslav@1890
    91
 * {@link java.util.concurrent.ExecutorCompletionService}, that
jaroslav@1890
    92
 * assists in coordinating the processing of groups of
jaroslav@1890
    93
 * asynchronous tasks.
jaroslav@1890
    94
 *
jaroslav@1890
    95
 * <p>Class {@link java.util.concurrent.ForkJoinPool} provides an
jaroslav@1890
    96
 * Executor primarily designed for processing instances of {@link
jaroslav@1890
    97
 * java.util.concurrent.ForkJoinTask} and its subclasses.  These
jaroslav@1890
    98
 * classes employ a work-stealing scheduler that attains high
jaroslav@1890
    99
 * throughput for tasks conforming to restrictions that often hold in
jaroslav@1890
   100
 * computation-intensive parallel processing.
jaroslav@1890
   101
 *
jaroslav@1890
   102
 * <h2>Queues</h2>
jaroslav@1890
   103
 *
jaroslav@1890
   104
 * The {@link java.util.concurrent.ConcurrentLinkedQueue} class
jaroslav@1890
   105
 * supplies an efficient scalable thread-safe non-blocking FIFO
jaroslav@1890
   106
 * queue.
jaroslav@1890
   107
 *
jaroslav@1890
   108
 * <p>Five implementations in {@code java.util.concurrent} support
jaroslav@1890
   109
 * the extended {@link java.util.concurrent.BlockingQueue}
jaroslav@1890
   110
 * interface, that defines blocking versions of put and take:
jaroslav@1890
   111
 * {@link java.util.concurrent.LinkedBlockingQueue},
jaroslav@1890
   112
 * {@link java.util.concurrent.ArrayBlockingQueue},
jaroslav@1890
   113
 * {@link java.util.concurrent.SynchronousQueue},
jaroslav@1890
   114
 * {@link java.util.concurrent.PriorityBlockingQueue}, and
jaroslav@1890
   115
 * {@link java.util.concurrent.DelayQueue}.
jaroslav@1890
   116
 * The different classes cover the most common usage contexts
jaroslav@1890
   117
 * for producer-consumer, messaging, parallel tasking, and
jaroslav@1890
   118
 * related concurrent designs.
jaroslav@1890
   119
 *
jaroslav@1890
   120
 * <p> Extended interface {@link java.util.concurrent.TransferQueue},
jaroslav@1890
   121
 * and implementation {@link java.util.concurrent.LinkedTransferQueue}
jaroslav@1890
   122
 * introduce a synchronous {@code transfer} method (along with related
jaroslav@1890
   123
 * features) in which a producer may optionally block awaiting its
jaroslav@1890
   124
 * consumer.
jaroslav@1890
   125
 *
jaroslav@1890
   126
 * <p>The {@link java.util.concurrent.BlockingDeque} interface
jaroslav@1890
   127
 * extends {@code BlockingQueue} to support both FIFO and LIFO
jaroslav@1890
   128
 * (stack-based) operations.
jaroslav@1890
   129
 * Class {@link java.util.concurrent.LinkedBlockingDeque}
jaroslav@1890
   130
 * provides an implementation.
jaroslav@1890
   131
 *
jaroslav@1890
   132
 * <h2>Timing</h2>
jaroslav@1890
   133
 *
jaroslav@1890
   134
 * The {@link java.util.concurrent.TimeUnit} class provides
jaroslav@1890
   135
 * multiple granularities (including nanoseconds) for
jaroslav@1890
   136
 * specifying and controlling time-out based operations.  Most
jaroslav@1890
   137
 * classes in the package contain operations based on time-outs
jaroslav@1890
   138
 * in addition to indefinite waits.  In all cases that
jaroslav@1890
   139
 * time-outs are used, the time-out specifies the minimum time
jaroslav@1890
   140
 * that the method should wait before indicating that it
jaroslav@1890
   141
 * timed-out.  Implementations make a &quot;best effort&quot;
jaroslav@1890
   142
 * to detect time-outs as soon as possible after they occur.
jaroslav@1890
   143
 * However, an indefinite amount of time may elapse between a
jaroslav@1890
   144
 * time-out being detected and a thread actually executing
jaroslav@1890
   145
 * again after that time-out.  All methods that accept timeout
jaroslav@1890
   146
 * parameters treat values less than or equal to zero to mean
jaroslav@1890
   147
 * not to wait at all.  To wait "forever", you can use a value
jaroslav@1890
   148
 * of {@code Long.MAX_VALUE}.
jaroslav@1890
   149
 *
jaroslav@1890
   150
 * <h2>Synchronizers</h2>
jaroslav@1890
   151
 *
jaroslav@1890
   152
 * Five classes aid common special-purpose synchronization idioms.
jaroslav@1890
   153
 * <ul>
jaroslav@1890
   154
 *
jaroslav@1890
   155
 * <li>{@link java.util.concurrent.Semaphore} is a classic concurrency tool.
jaroslav@1890
   156
 *
jaroslav@1890
   157
 * <li>{@link java.util.concurrent.CountDownLatch} is a very simple yet
jaroslav@1890
   158
 * very common utility for blocking until a given number of signals,
jaroslav@1890
   159
 * events, or conditions hold.
jaroslav@1890
   160
 *
jaroslav@1890
   161
 * <li>A {@link java.util.concurrent.CyclicBarrier} is a resettable
jaroslav@1890
   162
 * multiway synchronization point useful in some styles of parallel
jaroslav@1890
   163
 * programming.
jaroslav@1890
   164
 *
jaroslav@1890
   165
 * <li>A {@link java.util.concurrent.Phaser} provides
jaroslav@1890
   166
 * a more flexible form of barrier that may be used to control phased
jaroslav@1890
   167
 * computation among multiple threads.
jaroslav@1890
   168
 *
jaroslav@1890
   169
 * <li>An {@link java.util.concurrent.Exchanger} allows two threads to
jaroslav@1890
   170
 * exchange objects at a rendezvous point, and is useful in several
jaroslav@1890
   171
 * pipeline designs.
jaroslav@1890
   172
 *
jaroslav@1890
   173
 * </ul>
jaroslav@1890
   174
 *
jaroslav@1890
   175
 * <h2>Concurrent Collections</h2>
jaroslav@1890
   176
 *
jaroslav@1890
   177
 * Besides Queues, this package supplies Collection implementations
jaroslav@1890
   178
 * designed for use in multithreaded contexts:
jaroslav@1890
   179
 * {@link java.util.concurrent.ConcurrentHashMap},
jaroslav@1890
   180
 * {@link java.util.concurrent.ConcurrentSkipListMap},
jaroslav@1890
   181
 * {@link java.util.concurrent.ConcurrentSkipListSet},
jaroslav@1890
   182
 * {@link java.util.concurrent.CopyOnWriteArrayList}, and
jaroslav@1890
   183
 * {@link java.util.concurrent.CopyOnWriteArraySet}.
jaroslav@1890
   184
 * When many threads are expected to access a given collection, a
jaroslav@1890
   185
 * {@code ConcurrentHashMap} is normally preferable to a synchronized
jaroslav@1890
   186
 * {@code HashMap}, and a {@code ConcurrentSkipListMap} is normally
jaroslav@1890
   187
 * preferable to a synchronized {@code TreeMap}.
jaroslav@1890
   188
 * A {@code CopyOnWriteArrayList} is preferable to a synchronized
jaroslav@1890
   189
 * {@code ArrayList} when the expected number of reads and traversals
jaroslav@1890
   190
 * greatly outnumber the number of updates to a list.
jaroslav@1890
   191
jaroslav@1890
   192
 * <p>The "Concurrent" prefix used with some classes in this package
jaroslav@1890
   193
 * is a shorthand indicating several differences from similar
jaroslav@1890
   194
 * "synchronized" classes.  For example {@code java.util.Hashtable} and
jaroslav@1890
   195
 * {@code Collections.synchronizedMap(new HashMap())} are
jaroslav@1890
   196
 * synchronized.  But {@link
jaroslav@1890
   197
 * java.util.concurrent.ConcurrentHashMap} is "concurrent".  A
jaroslav@1890
   198
 * concurrent collection is thread-safe, but not governed by a
jaroslav@1890
   199
 * single exclusion lock.  In the particular case of
jaroslav@1890
   200
 * ConcurrentHashMap, it safely permits any number of
jaroslav@1890
   201
 * concurrent reads as well as a tunable number of concurrent
jaroslav@1890
   202
 * writes.  "Synchronized" classes can be useful when you need
jaroslav@1890
   203
 * to prevent all access to a collection via a single lock, at
jaroslav@1890
   204
 * the expense of poorer scalability.  In other cases in which
jaroslav@1890
   205
 * multiple threads are expected to access a common collection,
jaroslav@1890
   206
 * "concurrent" versions are normally preferable.  And
jaroslav@1890
   207
 * unsynchronized collections are preferable when either
jaroslav@1890
   208
 * collections are unshared, or are accessible only when
jaroslav@1890
   209
 * holding other locks.
jaroslav@1890
   210
 *
jaroslav@1890
   211
 * <p>Most concurrent Collection implementations (including most
jaroslav@1890
   212
 * Queues) also differ from the usual java.util conventions in that
jaroslav@1890
   213
 * their Iterators provide <em>weakly consistent</em> rather than
jaroslav@1890
   214
 * fast-fail traversal.  A weakly consistent iterator is thread-safe,
jaroslav@1890
   215
 * but does not necessarily freeze the collection while iterating, so
jaroslav@1890
   216
 * it may (or may not) reflect any updates since the iterator was
jaroslav@1890
   217
 * created.
jaroslav@1890
   218
 *
jaroslav@1890
   219
 * <h2><a name="MemoryVisibility">Memory Consistency Properties</a></h2>
jaroslav@1890
   220
 *
jaroslav@1890
   221
 * Chapter 17 of
jaroslav@1890
   222
 * <cite>The Java&trade; Language Specification</cite>
jaroslav@1890
   223
 * defines the
jaroslav@1890
   224
 * <i>happens-before</i> relation on memory operations such as reads and
jaroslav@1890
   225
 * writes of shared variables.  The results of a write by one thread are
jaroslav@1890
   226
 * guaranteed to be visible to a read by another thread only if the write
jaroslav@1890
   227
 * operation <i>happens-before</i> the read operation.  The
jaroslav@1890
   228
 * {@code synchronized} and {@code volatile} constructs, as well as the
jaroslav@1890
   229
 * {@code Thread.start()} and {@code Thread.join()} methods, can form
jaroslav@1890
   230
 * <i>happens-before</i> relationships.  In particular:
jaroslav@1890
   231
 *
jaroslav@1890
   232
 * <ul>
jaroslav@1890
   233
 *   <li>Each action in a thread <i>happens-before</i> every action in that
jaroslav@1890
   234
 *   thread that comes later in the program's order.
jaroslav@1890
   235
 *
jaroslav@1890
   236
 *   <li>An unlock ({@code synchronized} block or method exit) of a
jaroslav@1890
   237
 *   monitor <i>happens-before</i> every subsequent lock ({@code synchronized}
jaroslav@1890
   238
 *   block or method entry) of that same monitor.  And because
jaroslav@1890
   239
 *   the <i>happens-before</i> relation is transitive, all actions
jaroslav@1890
   240
 *   of a thread prior to unlocking <i>happen-before</i> all actions
jaroslav@1890
   241
 *   subsequent to any thread locking that monitor.
jaroslav@1890
   242
 *
jaroslav@1890
   243
 *   <li>A write to a {@code volatile} field <i>happens-before</i> every
jaroslav@1890
   244
 *   subsequent read of that same field.  Writes and reads of
jaroslav@1890
   245
 *   {@code volatile} fields have similar memory consistency effects
jaroslav@1890
   246
 *   as entering and exiting monitors, but do <em>not</em> entail
jaroslav@1890
   247
 *   mutual exclusion locking.
jaroslav@1890
   248
 *
jaroslav@1890
   249
 *   <li>A call to {@code start} on a thread <i>happens-before</i> any
jaroslav@1890
   250
 *   action in the started thread.
jaroslav@1890
   251
 *
jaroslav@1890
   252
 *   <li>All actions in a thread <i>happen-before</i> any other thread
jaroslav@1890
   253
 *   successfully returns from a {@code join} on that thread.
jaroslav@1890
   254
 *
jaroslav@1890
   255
 * </ul>
jaroslav@1890
   256
 *
jaroslav@1890
   257
 *
jaroslav@1890
   258
 * The methods of all classes in {@code java.util.concurrent} and its
jaroslav@1890
   259
 * subpackages extend these guarantees to higher-level
jaroslav@1890
   260
 * synchronization.  In particular:
jaroslav@1890
   261
 *
jaroslav@1890
   262
 * <ul>
jaroslav@1890
   263
 *
jaroslav@1890
   264
 *   <li>Actions in a thread prior to placing an object into any concurrent
jaroslav@1890
   265
 *   collection <i>happen-before</i> actions subsequent to the access or
jaroslav@1890
   266
 *   removal of that element from the collection in another thread.
jaroslav@1890
   267
 *
jaroslav@1890
   268
 *   <li>Actions in a thread prior to the submission of a {@code Runnable}
jaroslav@1890
   269
 *   to an {@code Executor} <i>happen-before</i> its execution begins.
jaroslav@1890
   270
 *   Similarly for {@code Callables} submitted to an {@code ExecutorService}.
jaroslav@1890
   271
 *
jaroslav@1890
   272
 *   <li>Actions taken by the asynchronous computation represented by a
jaroslav@1890
   273
 *   {@code Future} <i>happen-before</i> actions subsequent to the
jaroslav@1890
   274
 *   retrieval of the result via {@code Future.get()} in another thread.
jaroslav@1890
   275
 *
jaroslav@1890
   276
 *   <li>Actions prior to "releasing" synchronizer methods such as
jaroslav@1890
   277
 *   {@code Lock.unlock}, {@code Semaphore.release}, and
jaroslav@1890
   278
 *   {@code CountDownLatch.countDown} <i>happen-before</i> actions
jaroslav@1890
   279
 *   subsequent to a successful "acquiring" method such as
jaroslav@1890
   280
 *   {@code Lock.lock}, {@code Semaphore.acquire},
jaroslav@1890
   281
 *   {@code Condition.await}, and {@code CountDownLatch.await} on the
jaroslav@1890
   282
 *   same synchronizer object in another thread.
jaroslav@1890
   283
 *
jaroslav@1890
   284
 *   <li>For each pair of threads that successfully exchange objects via
jaroslav@1890
   285
 *   an {@code Exchanger}, actions prior to the {@code exchange()}
jaroslav@1890
   286
 *   in each thread <i>happen-before</i> those subsequent to the
jaroslav@1890
   287
 *   corresponding {@code exchange()} in another thread.
jaroslav@1890
   288
 *
jaroslav@1890
   289
 *   <li>Actions prior to calling {@code CyclicBarrier.await} and
jaroslav@1890
   290
 *   {@code Phaser.awaitAdvance} (as well as its variants)
jaroslav@1890
   291
 *   <i>happen-before</i> actions performed by the barrier action, and
jaroslav@1890
   292
 *   actions performed by the barrier action <i>happen-before</i> actions
jaroslav@1890
   293
 *   subsequent to a successful return from the corresponding {@code await}
jaroslav@1890
   294
 *   in other threads.
jaroslav@1890
   295
 *
jaroslav@1890
   296
 * </ul>
jaroslav@1890
   297
 *
jaroslav@1890
   298
 * @since 1.5
jaroslav@1890
   299
 */
jaroslav@1890
   300
package java.util.concurrent;