1 |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> |
2 |
<html> <head> |
3 |
<title>Concurrency Utilities</title> |
4 |
</head> |
5 |
|
6 |
<body> |
7 |
|
8 |
<p> Package java.util.concurrent contains utility classes commonly |
9 |
useful in concurrent programming. Like package java.util, it includes |
10 |
a few small standardized extensible frameworks, as well as some |
11 |
classes that provide useful functionality and are otherwise tedious or |
12 |
difficult to implement. Here are brief descriptions of the main |
13 |
components. See also the <tt>locks</tt> and <tt>atomic</tt> packages. |
14 |
|
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 |
{@link java.util.concurrent.ThreadPoolExecutor}, a highly tunable and |
34 |
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 |
be used in conjunction with a {@link java.util.concurrent.FutureTask} |
38 |
to asynchronously |
39 |
start a potentially long-running computation and query the FutureTask |
40 |
to determine if its execution has completed, or cancel it. |
41 |
|
42 |
<p> The {@link java.util.concurrent.Executors} class provides factory |
43 |
methods for the most common kinds and configurations of Executors, as |
44 |
well as a few utility methods for using them. |
45 |
|
46 |
<h2>Queues</h2> |
47 |
|
48 |
The java.util.concurrent {@link |
49 |
java.util.concurrent.ConcurrentLinkedQueue} class supplies an |
50 |
efficient scalable thread-safe non-blocking FIFO queue. |
51 |
|
52 |
<p> Five implementations in java.util.concurrent support the extended |
53 |
{@link java.util.concurrent.BlockingQueue} interface, that defines |
54 |
blocking versions of put and take: {@link |
55 |
java.util.concurrent.LinkedBlockingQueue}, {@link |
56 |
java.util.concurrent.ArrayBlockingQueue}, {@link |
57 |
java.util.concurrent.SynchronousQueue}, {@link |
58 |
java.util.concurrent.PriorityBlockingQueue}, and {@link |
59 |
java.util.concurrent.DelayQueue}. The different classes cover the most |
60 |
common usage contexts for producer-consumer, messaging, parallel |
61 |
tasking, and related concurrent designs. |
62 |
|
63 |
|
64 |
<h2>Timing</h2> |
65 |
|
66 |
The {@link java.util.concurrent.TimeUnit} class provides multiple |
67 |
granularities (including nanoseconds) for specifying and controlling |
68 |
time-out based operations. Nearly all other classes in the package |
69 |
contain operations based on time-outs in addition to indefinite waits. |
70 |
|
71 |
<h2>Synchronizers</h2> |
72 |
|
73 |
Five classes aid common special-purpose synchronization idioms. |
74 |
{@link java.util.concurrent.Semaphore} and {@link |
75 |
java.util.concurrent.FairSemaphore} are classic concurrency tools. |
76 |
{@link java.util.concurrent.CountDownLatch} is very simple yet very |
77 |
common utility for blocking until a single signal, event, or condition |
78 |
holds. A {@link java.util.concurrent.CyclicBarrier} is a resettable multiway |
79 |
synchronization point common in some styles of parallel |
80 |
programming. An {@link java.util.concurrent.Exchanger} allows two |
81 |
threads to exchange objects at a rendezvous point. |
82 |
|
83 |
<h2>Concurrent Collections</h2> |
84 |
|
85 |
Besides Queues, this package supplies a few Collection implementations |
86 |
designed for use in multithreaded contexts: {@link |
87 |
java.util.concurrent.ConcurrentHashMap}, {@link |
88 |
java.util.concurrent.CopyOnWriteArrayList}, and {@link |
89 |
java.util.concurrent.CopyOnWriteArraySet}. |
90 |
|
91 |
<p>The "Concurrent" prefix for classes is a shorthand |
92 |
indicating several differences from similar "synchronized" |
93 |
classes. For example <tt>java.util.Hashtable</tt> and |
94 |
<tt>Collections.synchronizedMap(new HashMap())</tt> are |
95 |
synchronized. But {@link |
96 |
java.util.concurrent.ConcurrentHashMap} is "concurrent". |
97 |
A concurrent collection (among other kinds of classes) is |
98 |
thread-safe, but not governed by a single exclusion lock. So, in the |
99 |
particular case of ConcurrentHashMap, it safely permits any number of |
100 |
concurrent reads as well as a tunable number of concurrent writes. |
101 |
There may still be a role for "synchronized" classes in some |
102 |
multithreaded programs -- they can sometimes be useful when you need |
103 |
to prevent ALL access to a collection via a single lock, at the |
104 |
expense of much poor scalability. In all other cases, "concurrent" |
105 |
versions are normally preferable. |
106 |
|
107 |
<p> Most concurrent Collection implementations (including most Queues) |
108 |
also differ from the usual java.util conventions in that their Iterators |
109 |
provide <em>weakly consistent</em> rather than fast-fail traversal. A |
110 |
weakly consistent iterator is thread-safe, but does not necessarily |
111 |
freeze the collection while iterating, so it may (or may not) reflect |
112 |
any updates since the iterator was created. |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
<hr> |
118 |
<address><A HREF="http://gee.cs.oswego.edu/dl">Doug Lea</A></address> |
119 |
<!-- hhmts start --> Last modified: Tue Aug 5 20:39:44 EDT 2003 <!-- hhmts end --> |
120 |
</body> </html> |