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