1 |
dl |
1.1 |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |
2 |
|
|
<html> <head> |
3 |
|
|
<title>Concurrency Utilities</title> |
4 |
|
|
</head> |
5 |
|
|
|
6 |
|
|
<body> |
7 |
|
|
|
8 |
dl |
1.3 |
<p> 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 <tt>locks</tt> and |
13 |
|
|
<tt>atomic</tt> packages. |
14 |
dl |
1.1 |
|
15 |
|
|
<h2>Executors</h2> |
16 |
|
|
|
17 |
|
|
{@link java.util.concurrent.Executor} is a simple standardized |
18 |
|
|
interface for defining custom thread-like subsystems, including thread |
19 |
|
|
pools, asynch-IO, and lightweight task frameworks. Depending on which |
20 |
|
|
concrete Executor class is being used, tasks may execute in a newly |
21 |
|
|
created thread, an existing task-execution thread, or the thread |
22 |
|
|
calling <tt>execute()</tt>, and may execute sequentially or |
23 |
|
|
concurrently. Executors also standardize ways of calling threads that |
24 |
|
|
compute functions returning results, via a {@link |
25 |
|
|
java.util.concurrent.Future}. This is supported in part by defining |
26 |
|
|
interface {@link java.util.concurrent.Callable}, the argument/result |
27 |
|
|
analog of Runnable. |
28 |
|
|
|
29 |
|
|
<p> {@link java.util.concurrent.ExecutorService} provides a more |
30 |
|
|
complete framework for executing Runnables. An ExecutorService |
31 |
|
|
manages queueing and scheduling of tasks, and allows controlled |
32 |
|
|
shutdown. The two primary implementations of ExecutorService are |
33 |
dl |
1.5 |
{@link java.util.concurrent.ThreadPoolExecutor}, a tunable and |
34 |
dl |
1.1 |
flexible thread pool and {@link |
35 |
|
|
java.util.concurrent.ScheduledExecutor}, which adds support for |
36 |
|
|
delayed and periodic task execution. These, and other Executors can |
37 |
dl |
1.5 |
be used in conjunction with a {@link |
38 |
|
|
java.util.concurrent.CancellableTask} or {@link |
39 |
|
|
java.util.concurrent.FutureTask} to asynchronously start a potentially |
40 |
|
|
long-running computation and query the FutureTask to determine if its |
41 |
|
|
execution has completed, or cancel it. |
42 |
dl |
1.1 |
|
43 |
|
|
<p> The {@link java.util.concurrent.Executors} class provides factory |
44 |
dl |
1.2 |
methods for the most common kinds and configurations of Executors, as |
45 |
|
|
well as a few utility methods for using them. |
46 |
dl |
1.1 |
|
47 |
|
|
<h2>Queues</h2> |
48 |
|
|
|
49 |
|
|
The java.util.concurrent {@link |
50 |
|
|
java.util.concurrent.ConcurrentLinkedQueue} class supplies an |
51 |
dl |
1.2 |
efficient scalable thread-safe non-blocking FIFO queue. |
52 |
dl |
1.1 |
|
53 |
|
|
<p> Five implementations in java.util.concurrent support the extended |
54 |
|
|
{@link java.util.concurrent.BlockingQueue} interface, that defines |
55 |
|
|
blocking versions of put and take: {@link |
56 |
|
|
java.util.concurrent.LinkedBlockingQueue}, {@link |
57 |
|
|
java.util.concurrent.ArrayBlockingQueue}, {@link |
58 |
|
|
java.util.concurrent.SynchronousQueue}, {@link |
59 |
|
|
java.util.concurrent.PriorityBlockingQueue}, and {@link |
60 |
dl |
1.2 |
java.util.concurrent.DelayQueue}. The different classes cover the most |
61 |
|
|
common usage contexts for producer-consumer, messaging, parallel |
62 |
|
|
tasking, and related concurrent designs. |
63 |
dl |
1.1 |
|
64 |
|
|
|
65 |
|
|
<h2>Timing</h2> |
66 |
|
|
|
67 |
|
|
The {@link java.util.concurrent.TimeUnit} class provides multiple |
68 |
dl |
1.2 |
granularities (including nanoseconds) for specifying and controlling |
69 |
|
|
time-out based operations. Nearly all other classes in the package |
70 |
|
|
contain operations based on time-outs in addition to indefinite waits. |
71 |
dl |
1.1 |
|
72 |
dholmes |
1.4 |
<p>In all cases that time-outs are used, the time-out specifies the |
73 |
|
|
minimum time that the method should wait before indicating that it |
74 |
|
|
timed-out. The virtual machine should make a "best effort" |
75 |
|
|
to detect time-outs as soon as possible after they occur. Regardless |
76 |
|
|
of the efforts of the virtual machine, the normal scheduling |
77 |
|
|
mechanisms, and the need to re-acquire locks in many cases, can lead |
78 |
|
|
to an indefinite amount of time elapsing between a time-out being |
79 |
|
|
detected and a thread actually executing again after that time-out. |
80 |
|
|
|
81 |
dl |
1.1 |
<h2>Synchronizers</h2> |
82 |
|
|
|
83 |
|
|
Five classes aid common special-purpose synchronization idioms. |
84 |
|
|
{@link java.util.concurrent.Semaphore} and {@link |
85 |
|
|
java.util.concurrent.FairSemaphore} are classic concurrency tools. |
86 |
|
|
{@link java.util.concurrent.CountDownLatch} is very simple yet very |
87 |
|
|
common utility for blocking until a single signal, event, or condition |
88 |
|
|
holds. A {@link java.util.concurrent.CyclicBarrier} is a resettable multiway |
89 |
|
|
synchronization point common in some styles of parallel |
90 |
|
|
programming. An {@link java.util.concurrent.Exchanger} allows two |
91 |
|
|
threads to exchange objects at a rendezvous point. |
92 |
|
|
|
93 |
|
|
<h2>Concurrent Collections</h2> |
94 |
|
|
|
95 |
|
|
Besides Queues, this package supplies a few Collection implementations |
96 |
|
|
designed for use in multithreaded contexts: {@link |
97 |
|
|
java.util.concurrent.ConcurrentHashMap}, {@link |
98 |
|
|
java.util.concurrent.CopyOnWriteArrayList}, and {@link |
99 |
|
|
java.util.concurrent.CopyOnWriteArraySet}. |
100 |
|
|
|
101 |
|
|
<p>The "Concurrent" prefix for classes is a shorthand |
102 |
|
|
indicating several differences from similar "synchronized" |
103 |
|
|
classes. For example <tt>java.util.Hashtable</tt> and |
104 |
|
|
<tt>Collections.synchronizedMap(new HashMap())</tt> are |
105 |
|
|
synchronized. But {@link |
106 |
|
|
java.util.concurrent.ConcurrentHashMap} is "concurrent". |
107 |
|
|
A concurrent collection (among other kinds of classes) is |
108 |
|
|
thread-safe, but not governed by a single exclusion lock. So, in the |
109 |
|
|
particular case of ConcurrentHashMap, it safely permits any number of |
110 |
|
|
concurrent reads as well as a tunable number of concurrent writes. |
111 |
|
|
There may still be a role for "synchronized" classes in some |
112 |
|
|
multithreaded programs -- they can sometimes be useful when you need |
113 |
|
|
to prevent ALL access to a collection via a single lock, at the |
114 |
|
|
expense of much poor scalability. In all other cases, "concurrent" |
115 |
|
|
versions are normally preferable. |
116 |
|
|
|
117 |
|
|
<p> Most concurrent Collection implementations (including most Queues) |
118 |
|
|
also differ from the usual java.util conventions in that their Iterators |
119 |
|
|
provide <em>weakly consistent</em> rather than fast-fail traversal. A |
120 |
|
|
weakly consistent iterator is thread-safe, but does not necessarily |
121 |
|
|
freeze the collection while iterating, so it may (or may not) reflect |
122 |
|
|
any updates since the iterator was created. |
123 |
|
|
|
124 |
|
|
</body> </html> |