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 |
dl |
1.12 |
<b>Interfaces.</b> {@link java.util.concurrent.Executor} is a simple |
18 |
|
|
standardized interface for defining custom thread-like subsystems, |
19 |
|
|
including thread pools, asynchronous IO, and lightweight task |
20 |
|
|
frameworks. Depending on which concrete Executor class is being used, |
21 |
|
|
tasks may execute in a newly created thread, an existing |
22 |
|
|
task-execution thread, or the thread calling <tt>execute()</tt>, and |
23 |
|
|
may execute sequentially or concurrently. {@link |
24 |
|
|
java.util.concurrent.ExecutorService} provides a more complete |
25 |
|
|
asynchronous task execution framework. An ExecutorService manages |
26 |
dl |
1.14 |
queuing and scheduling of tasks, and allows controlled shutdown. The |
27 |
dl |
1.12 |
{@link java.util.concurrent.ScheduledExecutorService} subinterface |
28 |
|
|
adds support for delayed and periodic task execution. |
29 |
|
|
ExecutorServices provide methods arranging asynchronous execution of |
30 |
|
|
any function expressed as {@link java.util.concurrent.Callable}, the |
31 |
|
|
result-bearing analog of {@link java.lang.Runnable}. A {@link |
32 |
|
|
java.util.concurrent.Future} returns the results of a function, allows |
33 |
dl |
1.16 |
determination of whether execution has completed, and provides a means to |
34 |
dl |
1.12 |
cancel execution. |
35 |
|
|
|
36 |
|
|
<p> |
37 |
|
|
|
38 |
dl |
1.16 |
<b>Implementations.</b> Classes {@link |
39 |
dl |
1.12 |
java.util.concurrent.ThreadPoolExecutor} and {@link |
40 |
dl |
1.16 |
java.util.concurrent.ScheduledThreadPoolExecutor} provide tunable, |
41 |
|
|
flexible thread pools. The {@link java.util.concurrent.Executors} |
42 |
|
|
class provides factory methods for the most common kinds and |
43 |
|
|
configurations of Executors, as well as a few utility methods for |
44 |
|
|
using them. Other utilities based on Executors include the concrete |
45 |
|
|
class {@link java.util.concurrent.FutureTask} providing a common |
46 |
|
|
extensible implementation of Futures, and {@link |
47 |
dl |
1.12 |
java.util.concurrent.ExecutorCompletionService}, that assists in |
48 |
|
|
coordinating the processing of groups of asynchronous tasks. |
49 |
dl |
1.1 |
|
50 |
|
|
<h2>Queues</h2> |
51 |
|
|
|
52 |
dl |
1.6 |
The java.util.concurrent {@link |
53 |
dl |
1.1 |
java.util.concurrent.ConcurrentLinkedQueue} class supplies an |
54 |
dl |
1.6 |
efficient scalable thread-safe non-blocking FIFO queue. Five |
55 |
|
|
implementations in java.util.concurrent support the extended {@link |
56 |
|
|
java.util.concurrent.BlockingQueue} interface, that defines blocking |
57 |
|
|
versions of put and take: {@link |
58 |
dl |
1.1 |
java.util.concurrent.LinkedBlockingQueue}, {@link |
59 |
|
|
java.util.concurrent.ArrayBlockingQueue}, {@link |
60 |
|
|
java.util.concurrent.SynchronousQueue}, {@link |
61 |
|
|
java.util.concurrent.PriorityBlockingQueue}, and {@link |
62 |
dl |
1.2 |
java.util.concurrent.DelayQueue}. The different classes cover the most |
63 |
|
|
common usage contexts for producer-consumer, messaging, parallel |
64 |
dl |
1.17 |
tasking, and related concurrent designs. The {@link |
65 |
|
|
java.util.concurrent.BlockingDeque} interface extends |
66 |
|
|
<tt>BlockingQueue</tt> to support both FIFO and LIFO (stack-based) |
67 |
|
|
operations. Class {@link java.util.concurrent.LinkedBlockingDeque} |
68 |
|
|
provides an implementation. |
69 |
dl |
1.1 |
|
70 |
|
|
|
71 |
|
|
<h2>Timing</h2> |
72 |
|
|
|
73 |
|
|
The {@link java.util.concurrent.TimeUnit} class provides multiple |
74 |
dl |
1.2 |
granularities (including nanoseconds) for specifying and controlling |
75 |
dl |
1.9 |
time-out based operations. Most classes in the package contain |
76 |
|
|
operations based on time-outs in addition to indefinite waits. In all |
77 |
|
|
cases that time-outs are used, the time-out specifies the minimum time |
78 |
|
|
that the method should wait before indicating that it |
79 |
|
|
timed-out. Implementations make a "best effort" to detect |
80 |
|
|
time-outs as soon as possible after they occur. However, an indefinite |
81 |
|
|
amount of time may elapse between a time-out being detected and a |
82 |
|
|
thread actually executing again after that time-out. |
83 |
dholmes |
1.4 |
|
84 |
dl |
1.1 |
<h2>Synchronizers</h2> |
85 |
|
|
|
86 |
dl |
1.13 |
Four classes aid common special-purpose synchronization idioms. |
87 |
dl |
1.10 |
{@link java.util.concurrent.Semaphore} is a classic concurrency tool. |
88 |
dl |
1.16 |
{@link java.util.concurrent.CountDownLatch} is a very simple yet very |
89 |
dl |
1.11 |
common utility for blocking until a given number of signals, events, |
90 |
|
|
or conditions hold. A {@link java.util.concurrent.CyclicBarrier} is a |
91 |
dl |
1.13 |
resettable multiway synchronization point useful in some styles of |
92 |
dl |
1.11 |
parallel programming. An {@link java.util.concurrent.Exchanger} allows |
93 |
dl |
1.13 |
two threads to exchange objects at a rendezvous point, and is useful |
94 |
|
|
in several pipeline designs. |
95 |
dl |
1.1 |
|
96 |
|
|
<h2>Concurrent Collections</h2> |
97 |
|
|
|
98 |
dl |
1.17 |
Besides Queues, this package supplies Collection implementations |
99 |
|
|
designed for use in multithreaded contexts: |
100 |
|
|
{@link java.util.concurrent.ConcurrentHashMap}, |
101 |
|
|
{@link java.util.concurrent.ConcurrentSkipListMap}, |
102 |
|
|
{@link java.util.concurrent.ConcurrentSkipListSet}, |
103 |
|
|
{@link java.util.concurrent.CopyOnWriteArrayList}, and |
104 |
|
|
{@link java.util.concurrent.CopyOnWriteArraySet}. |
105 |
|
|
When many threads are expected to access a given collection, |
106 |
|
|
a <tt>ConcurrentHashMap</tt> is normally preferable to |
107 |
|
|
a synchronized <tt>HashMap</tt>, and a |
108 |
|
|
<tt>ConcurrentSkipListMap</tt> is normally preferable |
109 |
|
|
to a synchronized <tt>TreeMap</tt>. A |
110 |
|
|
<tt>CopyOnWriteArrayList</tt> is preferable to |
111 |
|
|
a synchronized <tt>ArrayList</tt> when the expected number of reads |
112 |
|
|
and traversals greatly outnumber the number of updates to a list. |
113 |
dl |
1.1 |
|
114 |
dl |
1.6 |
<p>The "Concurrent" prefix used with some classes in this package is a |
115 |
|
|
shorthand indicating several differences from similar "synchronized" |
116 |
dl |
1.1 |
classes. For example <tt>java.util.Hashtable</tt> and |
117 |
|
|
<tt>Collections.synchronizedMap(new HashMap())</tt> are |
118 |
dl |
1.6 |
synchronized. But {@link java.util.concurrent.ConcurrentHashMap} is |
119 |
|
|
"concurrent". A concurrent collection is thread-safe, but not |
120 |
|
|
governed by a single exclusion lock. In the particular case of |
121 |
|
|
ConcurrentHashMap, it safely permits any number of concurrent reads as |
122 |
dl |
1.9 |
well as a tunable number of concurrent writes. "Synchronized" classes |
123 |
|
|
can be useful when you need to prevent all access to a collection via |
124 |
|
|
a single lock, at the expense of poorer scalability. In other cases in |
125 |
|
|
which multiple threads are expected to access a common collection, |
126 |
|
|
"concurrent" versions are normally preferable. And unsynchronized |
127 |
|
|
collections are preferable when either collections are unshared, or |
128 |
|
|
are accessible only when holding other locks. |
129 |
dl |
1.1 |
|
130 |
|
|
<p> Most concurrent Collection implementations (including most Queues) |
131 |
|
|
also differ from the usual java.util conventions in that their Iterators |
132 |
|
|
provide <em>weakly consistent</em> rather than fast-fail traversal. A |
133 |
|
|
weakly consistent iterator is thread-safe, but does not necessarily |
134 |
|
|
freeze the collection while iterating, so it may (or may not) reflect |
135 |
|
|
any updates since the iterator was created. |
136 |
|
|
|
137 |
dl |
1.15 |
@since 1.5 |
138 |
|
|
|
139 |
dl |
1.1 |
</body> </html> |