ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorCompletionService.java
Revision: 1.11
Committed: Mon May 2 08:40:27 2005 UTC (19 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.10: +1 -1 lines
Log Message:
catch( -> catch (

File Contents

# Content
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 package java.util.concurrent;
8
9
10 /**
11 * A {@link CompletionService} that uses a supplied {@link Executor}
12 * to execute tasks. This class arranges that submitted tasks are,
13 * upon completion, placed on a queue accessible using <tt>take</tt>.
14 * The class is lightweight enough to be suitable for transient use
15 * when processing groups of tasks.
16 *
17 * <p>
18 *
19 * <b>Usage Examples.</b>
20 *
21 * Suppose you have a set of solvers for a certain problem, each
22 * returning a value of some type <tt>Result</tt>, and would like to
23 * run them concurrently, processing the results of each of them that
24 * return a non-null value, in some method <tt>use(Result r)</tt>. You
25 * could write this as:
26 *
27 * <pre>
28 * void solve(Executor e, Collection&lt;Callable&lt;Result&gt;&gt; solvers)
29 * throws InterruptedException, ExecutionException {
30 * CompletionService&lt;Result&gt; ecs = new ExecutorCompletionService&lt;Result&gt;(e);
31 * for (Callable&lt;Result&gt; s : solvers)
32 * ecs.submit(s);
33 * int n = solvers.size();
34 * for (int i = 0; i &lt; n; ++i) {
35 * Result r = ecs.take().get();
36 * if (r != null)
37 * use(r);
38 * }
39 * }
40 * </pre>
41 *
42 * Suppose instead that you would like to use the first non-null result
43 * of the set of tasks, ignoring any that encounter exceptions,
44 * and cancelling all other tasks when the first one is ready:
45 *
46 * <pre>
47 * void solve(Executor e, Collection&lt;Callable&lt;Result&gt;&gt; solvers)
48 * throws InterruptedException {
49 * CompletionService&lt;Result&gt; ecs = new ExecutorCompletionService&lt;Result&gt;(e);
50 * int n = solvers.size();
51 * List&lt;Future&lt;Result&gt;&gt; futures = new ArrayList&lt;Future&lt;Result&gt;&gt;(n);
52 * Result result = null;
53 * try {
54 * for (Callable&lt;Result&gt; s : solvers)
55 * futures.add(ecs.submit(s));
56 * for (int i = 0; i &lt; n; ++i) {
57 * try {
58 * Result r = ecs.take().get();
59 * if (r != null) {
60 * result = r;
61 * break;
62 * }
63 * } catch (ExecutionException ignore) {}
64 * }
65 * }
66 * finally {
67 * for (Future&lt;Result&gt; f : futures)
68 * f.cancel(true);
69 * }
70 *
71 * if (result != null)
72 * use(result);
73 * }
74 * </pre>
75 */
76 public class ExecutorCompletionService<V> implements CompletionService<V> {
77 private final Executor executor;
78 private final BlockingQueue<Future<V>> completionQueue;
79
80 /**
81 * FutureTask extension to enqueue upon completion
82 */
83 private class QueueingFuture extends FutureTask<V> {
84 QueueingFuture(Callable<V> c) { super(c); }
85 QueueingFuture(Runnable t, V r) { super(t, r); }
86 protected void done() { completionQueue.add(this); }
87 }
88
89 /**
90 * Creates an ExecutorCompletionService using the supplied
91 * executor for base task execution and a
92 * {@link LinkedBlockingQueue} as a completion queue.
93 * @param executor the executor to use
94 * @throws NullPointerException if executor is <tt>null</tt>
95 */
96 public ExecutorCompletionService(Executor executor) {
97 if (executor == null)
98 throw new NullPointerException();
99 this.executor = executor;
100 this.completionQueue = new LinkedBlockingQueue<Future<V>>();
101 }
102
103 /**
104 * Creates an ExecutorCompletionService using the supplied
105 * executor for base task execution and the supplied queue as its
106 * completion queue.
107 * @param executor the executor to use
108 * @param completionQueue the queue to use as the completion queue
109 * normally one dedicated for use by this service
110 * @throws NullPointerException if executor or completionQueue are <tt>null</tt>
111 */
112 public ExecutorCompletionService(Executor executor,
113 BlockingQueue<Future<V>> completionQueue) {
114 if (executor == null || completionQueue == null)
115 throw new NullPointerException();
116 this.executor = executor;
117 this.completionQueue = completionQueue;
118 }
119
120 public Future<V> submit(Callable<V> task) {
121 if (task == null) throw new NullPointerException();
122 QueueingFuture f = new QueueingFuture(task);
123 executor.execute(f);
124 return f;
125 }
126
127 public Future<V> submit(Runnable task, V result) {
128 if (task == null) throw new NullPointerException();
129 QueueingFuture f = new QueueingFuture(task, result);
130 executor.execute(f);
131 return f;
132 }
133
134 public Future<V> take() throws InterruptedException {
135 return completionQueue.take();
136 }
137
138 public Future<V> poll() {
139 return completionQueue.poll();
140 }
141
142 public Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException {
143 return completionQueue.poll(timeout, unit);
144 }
145
146 }
147
148