1/*
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 * Utility classes commonly useful in concurrent programming.  This
9 * package includes a few small standardized extensible frameworks, as
10 * well as some classes that provide useful functionality and are
11 * otherwise tedious or difficult to implement.  Here are brief
12 * descriptions of the main components.  See also the
13 * {@link java.util.concurrent.locks} and
14 * {@link java.util.concurrent.atomic} packages.
15 *
16 * <h2>Executors</h2>
17 *
18 * <b>Interfaces.</b>
19 *
20 * {@link java.util.concurrent.Executor} is a simple standardized
21 * interface for defining custom thread-like subsystems, including
22 * thread pools, asynchronous IO, and lightweight task frameworks.
23 * Depending on which concrete Executor class is being used, tasks may
24 * execute in a newly created thread, an existing task-execution thread,
25 * or the thread calling {@link java.util.concurrent.Executor#execute
26 * execute}, and may execute sequentially or concurrently.
27 *
28 * {@link java.util.concurrent.ExecutorService} provides a more
29 * complete asynchronous task execution framework.  An
30 * ExecutorService manages queuing and scheduling of tasks,
31 * and allows controlled shutdown.
32 *
33 * The {@link java.util.concurrent.ScheduledExecutorService}
34 * subinterface and associated interfaces add support for
35 * delayed and periodic task execution.  ExecutorServices
36 * provide methods arranging asynchronous execution of any
37 * function expressed as {@link java.util.concurrent.Callable},
38 * the result-bearing analog of {@link java.lang.Runnable}.
39 *
40 * A {@link java.util.concurrent.Future} returns the results of
41 * a function, allows determination of whether execution has
42 * completed, and provides a means to cancel execution.
43 *
44 * A {@link java.util.concurrent.RunnableFuture} is a {@code Future}
45 * that possesses a {@code run} method that upon execution,
46 * sets its results.
47 *
48 * <p>
49 *
50 * <b>Implementations.</b>
51 *
52 * Classes {@link java.util.concurrent.ThreadPoolExecutor} and
53 * {@link java.util.concurrent.ScheduledThreadPoolExecutor}
54 * provide tunable, flexible thread pools.
55 *
56 * The {@link java.util.concurrent.Executors} class provides
57 * factory methods for the most common kinds and configurations
58 * of Executors, as well as a few utility methods for using
59 * them.  Other utilities based on {@code Executors} include the
60 * concrete class {@link java.util.concurrent.FutureTask}
61 * providing a common extensible implementation of Futures, and
62 * {@link java.util.concurrent.ExecutorCompletionService}, that
63 * assists in coordinating the processing of groups of
64 * asynchronous tasks.
65 *
66 * <h2>Queues</h2>
67 *
68 * The {@link java.util.concurrent.ConcurrentLinkedQueue} class
69 * supplies an efficient scalable thread-safe non-blocking FIFO
70 * queue.
71 *
72 * <p>Five implementations in {@code java.util.concurrent} support
73 * the extended {@link java.util.concurrent.BlockingQueue}
74 * interface, that defines blocking versions of put and take:
75 * {@link java.util.concurrent.LinkedBlockingQueue},
76 * {@link java.util.concurrent.ArrayBlockingQueue},
77 * {@link java.util.concurrent.SynchronousQueue},
78 * {@link java.util.concurrent.PriorityBlockingQueue}, and
79 * {@link java.util.concurrent.DelayQueue}.
80 * The different classes cover the most common usage contexts
81 * for producer-consumer, messaging, parallel tasking, and
82 * related concurrent designs.
83 *
84 * <p>The {@link java.util.concurrent.BlockingDeque} interface
85 * extends {@code BlockingQueue} to support both FIFO and LIFO
86 * (stack-based) operations.
87 * Class {@link java.util.concurrent.LinkedBlockingDeque}
88 * provides an implementation.
89 *
90 * <h2>Timing</h2>
91 *
92 * The {@link java.util.concurrent.TimeUnit} class provides
93 * multiple granularities (including nanoseconds) for
94 * specifying and controlling time-out based operations.  Most
95 * classes in the package contain operations based on time-outs
96 * in addition to indefinite waits.  In all cases that
97 * time-outs are used, the time-out specifies the minimum time
98 * that the method should wait before indicating that it
99 * timed-out.  Implementations make a &quot;best effort&quot;
100 * to detect time-outs as soon as possible after they occur.
101 * However, an indefinite amount of time may elapse between a
102 * time-out being detected and a thread actually executing
103 * again after that time-out.  All methods that accept timeout
104 * parameters treat values less than or equal to zero to mean
105 * not to wait at all.  To wait "forever", you can use a value
106 * of {@code Long.MAX_VALUE}.
107 *
108 * <h2>Synchronizers</h2>
109 *
110 * Five classes aid common special-purpose synchronization idioms.
111 * <ul>
112 *
113 * <li>{@link java.util.concurrent.Semaphore} is a classic concurrency tool.
114 *
115 * <li>{@link java.util.concurrent.CountDownLatch} is a very simple yet
116 * very common utility for blocking until a given number of signals,
117 * events, or conditions hold.
118 *
119 * <li>A {@link java.util.concurrent.CyclicBarrier} is a resettable
120 * multiway synchronization point useful in some styles of parallel
121 * programming.
122 *
123 * <li>An {@link java.util.concurrent.Exchanger} allows two threads to
124 * exchange objects at a rendezvous point, and is useful in several
125 * pipeline designs.
126 *
127 * </ul>
128 *
129 * <h2>Concurrent Collections</h2>
130 *
131 * Besides Queues, this package supplies Collection implementations
132 * designed for use in multithreaded contexts:
133 * {@link java.util.concurrent.ConcurrentHashMap},
134 * {@link java.util.concurrent.ConcurrentSkipListMap},
135 * {@link java.util.concurrent.ConcurrentSkipListSet},
136 * {@link java.util.concurrent.CopyOnWriteArrayList}, and
137 * {@link java.util.concurrent.CopyOnWriteArraySet}.
138 * When many threads are expected to access a given collection, a
139 * {@code ConcurrentHashMap} is normally preferable to a synchronized
140 * {@code HashMap}, and a {@code ConcurrentSkipListMap} is normally
141 * preferable to a synchronized {@code TreeMap}.
142 * A {@code CopyOnWriteArrayList} is preferable to a synchronized
143 * {@code ArrayList} when the expected number of reads and traversals
144 * greatly outnumber the number of updates to a list.
145
146 * <p>The "Concurrent" prefix used with some classes in this package
147 * is a shorthand indicating several differences from similar
148 * "synchronized" classes.  For example {@code java.util.Hashtable} and
149 * {@code Collections.synchronizedMap(new HashMap())} are
150 * synchronized.  But {@link
151 * java.util.concurrent.ConcurrentHashMap} is "concurrent".  A
152 * concurrent collection is thread-safe, but not governed by a
153 * single exclusion lock.  In the particular case of
154 * ConcurrentHashMap, it safely permits any number of
155 * concurrent reads as well as a tunable number of concurrent
156 * writes.  "Synchronized" classes can be useful when you need
157 * to prevent all access to a collection via a single lock, at
158 * the expense of poorer scalability.  In other cases in which
159 * multiple threads are expected to access a common collection,
160 * "concurrent" versions are normally preferable.  And
161 * unsynchronized collections are preferable when either
162 * collections are unshared, or are accessible only when
163 * holding other locks.
164 *
165 * <p>Most concurrent Collection implementations (including most
166 * Queues) also differ from the usual java.util conventions in that
167 * their Iterators provide <em>weakly consistent</em> rather than
168 * fast-fail traversal.  A weakly consistent iterator is thread-safe,
169 * but does not necessarily freeze the collection while iterating, so
170 * it may (or may not) reflect any updates since the iterator was
171 * created.
172 *
173 * <h2><a name="MemoryVisibility">Memory Consistency Properties</a></h2>
174 *
175 * <a href="http://java.sun.com/docs/books/jls/third_edition/html/memory.html">
176 * Chapter 17 of the Java Language Specification</a> defines the
177 * <i>happens-before</i> relation on memory operations such as reads and
178 * writes of shared variables.  The results of a write by one thread are
179 * guaranteed to be visible to a read by another thread only if the write
180 * operation <i>happens-before</i> the read operation.  The
181 * {@code synchronized} and {@code volatile} constructs, as well as the
182 * {@code Thread.start()} and {@code Thread.join()} methods, can form
183 * <i>happens-before</i> relationships.  In particular:
184 *
185 * <ul>
186 *   <li>Each action in a thread <i>happens-before</i> every action in that
187 *   thread that comes later in the program's order.
188 *
189 *   <li>An unlock ({@code synchronized} block or method exit) of a
190 *   monitor <i>happens-before</i> every subsequent lock ({@code synchronized}
191 *   block or method entry) of that same monitor.  And because
192 *   the <i>happens-before</i> relation is transitive, all actions
193 *   of a thread prior to unlocking <i>happen-before</i> all actions
194 *   subsequent to any thread locking that monitor.
195 *
196 *   <li>A write to a {@code volatile} field <i>happens-before</i> every
197 *   subsequent read of that same field.  Writes and reads of
198 *   {@code volatile} fields have similar memory consistency effects
199 *   as entering and exiting monitors, but do <em>not</em> entail
200 *   mutual exclusion locking.
201 *
202 *   <li>A call to {@code start} on a thread <i>happens-before</i> any
203 *   action in the started thread.
204 *
205 *   <li>All actions in a thread <i>happen-before</i> any other thread
206 *   successfully returns from a {@code join} on that thread.
207 *
208 * </ul>
209 *
210 *
211 * The methods of all classes in {@code java.util.concurrent} and its
212 * subpackages extend these guarantees to higher-level
213 * synchronization.  In particular:
214 *
215 * <ul>
216 *
217 *   <li>Actions in a thread prior to placing an object into any concurrent
218 *   collection <i>happen-before</i> actions subsequent to the access or
219 *   removal of that element from the collection in another thread.
220 *
221 *   <li>Actions in a thread prior to the submission of a {@code Runnable}
222 *   to an {@code Executor} <i>happen-before</i> its execution begins.
223 *   Similarly for {@code Callables} submitted to an {@code ExecutorService}.
224 *
225 *   <li>Actions taken by the asynchronous computation represented by a
226 *   {@code Future} <i>happen-before</i> actions subsequent to the
227 *   retrieval of the result via {@code Future.get()} in another thread.
228 *
229 *   <li>Actions prior to "releasing" synchronizer methods such as
230 *   {@code Lock.unlock}, {@code Semaphore.release}, and
231 *   {@code CountDownLatch.countDown} <i>happen-before</i> actions
232 *   subsequent to a successful "acquiring" method such as
233 *   {@code Lock.lock}, {@code Semaphore.acquire},
234 *   {@code Condition.await}, and {@code CountDownLatch.await} on the
235 *   same synchronizer object in another thread.
236 *
237 *   <li>For each pair of threads that successfully exchange objects via
238 *   an {@code Exchanger}, actions prior to the {@code exchange()}
239 *   in each thread <i>happen-before</i> those subsequent to the
240 *   corresponding {@code exchange()} in another thread.
241 *
242 *   <li>Actions prior to calling {@code CyclicBarrier.await}
243 *   <i>happen-before</i> actions performed by the barrier action, and
244 *   actions performed by the barrier action <i>happen-before</i> actions
245 *   subsequent to a successful return from the corresponding {@code await}
246 *   in other threads.
247 *
248 * </ul>
249 *
250 * @since 1.5
251 */
252package java.util.concurrent;
253