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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
/** |
10 |
* A {@link CompletionService} that uses a supplied {@link Executor} |
11 |
* to execute tasks. This class arranges that submitted tasks are, |
12 |
* upon completion, placed on a queue accessible using {@code take}. |
13 |
* The class is lightweight enough to be suitable for transient use |
14 |
* when processing groups of tasks. |
15 |
* |
16 |
* <p> |
17 |
* |
18 |
* <b>Usage Examples.</b> |
19 |
* |
20 |
* Suppose you have a set of solvers for a certain problem, each |
21 |
* returning a value of some type {@code Result}, and would like to |
22 |
* run them concurrently, processing the results of each of them that |
23 |
* return a non-null value, in some method {@code use(Result r)}. You |
24 |
* could write this as: |
25 |
* |
26 |
* <pre> {@code |
27 |
* void solve(Executor e, |
28 |
* Collection<Callable<Result>> solvers) |
29 |
* throws InterruptedException, ExecutionException { |
30 |
* CompletionService<Result> ecs |
31 |
* = new ExecutorCompletionService<>(e); |
32 |
* for (Callable<Result> s : solvers) |
33 |
* ecs.submit(s); |
34 |
* int n = solvers.size(); |
35 |
* for (int i = 0; i < n; ++i) { |
36 |
* Result r = ecs.take().get(); |
37 |
* if (r != null) |
38 |
* use(r); |
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> {@code |
47 |
* void solve(Executor e, |
48 |
* Collection<Callable<Result>> solvers) |
49 |
* throws InterruptedException { |
50 |
* CompletionService<Result> ecs |
51 |
* = new ExecutorCompletionService<>(e); |
52 |
* int n = solvers.size(); |
53 |
* List<Future<Result>> futures = new ArrayList<>(n); |
54 |
* Result result = null; |
55 |
* try { |
56 |
* for (Callable<Result> s : solvers) |
57 |
* futures.add(ecs.submit(s)); |
58 |
* for (int i = 0; i < n; ++i) { |
59 |
* try { |
60 |
* Result r = ecs.take().get(); |
61 |
* if (r != null) { |
62 |
* result = r; |
63 |
* break; |
64 |
* } |
65 |
* } catch (ExecutionException ignore) {} |
66 |
* } |
67 |
* } |
68 |
* finally { |
69 |
* for (Future<Result> f : futures) |
70 |
* f.cancel(true); |
71 |
* } |
72 |
* |
73 |
* if (result != null) |
74 |
* use(result); |
75 |
* }}</pre> |
76 |
* |
77 |
* @since 1.5 |
78 |
*/ |
79 |
public class ExecutorCompletionService<V> implements CompletionService<V> { |
80 |
private final Executor executor; |
81 |
private final AbstractExecutorService aes; |
82 |
private final BlockingQueue<Future<V>> completionQueue; |
83 |
|
84 |
/** |
85 |
* FutureTask extension to enqueue upon completion. |
86 |
*/ |
87 |
private static class QueueingFuture<V> extends FutureTask<Void> { |
88 |
QueueingFuture(RunnableFuture<V> task, |
89 |
BlockingQueue<Future<V>> completionQueue) { |
90 |
super(task, null); |
91 |
this.task = task; |
92 |
this.completionQueue = completionQueue; |
93 |
} |
94 |
private final Future<V> task; |
95 |
private final BlockingQueue<Future<V>> completionQueue; |
96 |
protected void done() { completionQueue.add(task); } |
97 |
} |
98 |
|
99 |
private RunnableFuture<V> newTaskFor(Callable<V> task) { |
100 |
if (aes == null) |
101 |
return new FutureTask<V>(task); |
102 |
else |
103 |
return aes.newTaskFor(task); |
104 |
} |
105 |
|
106 |
private RunnableFuture<V> newTaskFor(Runnable task, V result) { |
107 |
if (aes == null) |
108 |
return new FutureTask<V>(task, result); |
109 |
else |
110 |
return aes.newTaskFor(task, result); |
111 |
} |
112 |
|
113 |
/** |
114 |
* Creates an ExecutorCompletionService using the supplied |
115 |
* executor for base task execution and a |
116 |
* {@link LinkedBlockingQueue} as a completion queue. |
117 |
* |
118 |
* @param executor the executor to use |
119 |
* @throws NullPointerException if executor is {@code null} |
120 |
*/ |
121 |
public ExecutorCompletionService(Executor executor) { |
122 |
if (executor == null) |
123 |
throw new NullPointerException(); |
124 |
this.executor = executor; |
125 |
this.aes = (executor instanceof AbstractExecutorService) ? |
126 |
(AbstractExecutorService) executor : null; |
127 |
this.completionQueue = new LinkedBlockingQueue<Future<V>>(); |
128 |
} |
129 |
|
130 |
/** |
131 |
* Creates an ExecutorCompletionService using the supplied |
132 |
* executor for base task execution and the supplied queue as its |
133 |
* completion queue. |
134 |
* |
135 |
* @param executor the executor to use |
136 |
* @param completionQueue the queue to use as the completion queue |
137 |
* normally one dedicated for use by this service. This |
138 |
* queue is treated as unbounded -- failed attempted |
139 |
* {@code Queue.add} operations for completed tasks cause |
140 |
* them not to be retrievable. |
141 |
* @throws NullPointerException if executor or completionQueue are {@code null} |
142 |
*/ |
143 |
public ExecutorCompletionService(Executor executor, |
144 |
BlockingQueue<Future<V>> completionQueue) { |
145 |
if (executor == null || completionQueue == null) |
146 |
throw new NullPointerException(); |
147 |
this.executor = executor; |
148 |
this.aes = (executor instanceof AbstractExecutorService) ? |
149 |
(AbstractExecutorService) executor : null; |
150 |
this.completionQueue = completionQueue; |
151 |
} |
152 |
|
153 |
public Future<V> submit(Callable<V> task) { |
154 |
if (task == null) throw new NullPointerException(); |
155 |
RunnableFuture<V> f = newTaskFor(task); |
156 |
executor.execute(new QueueingFuture<V>(f, completionQueue)); |
157 |
return f; |
158 |
} |
159 |
|
160 |
public Future<V> submit(Runnable task, V result) { |
161 |
if (task == null) throw new NullPointerException(); |
162 |
RunnableFuture<V> f = newTaskFor(task, result); |
163 |
executor.execute(new QueueingFuture<V>(f, completionQueue)); |
164 |
return f; |
165 |
} |
166 |
|
167 |
public Future<V> take() throws InterruptedException { |
168 |
return completionQueue.take(); |
169 |
} |
170 |
|
171 |
public Future<V> poll() { |
172 |
return completionQueue.poll(); |
173 |
} |
174 |
|
175 |
public Future<V> poll(long timeout, TimeUnit unit) |
176 |
throws InterruptedException { |
177 |
return completionQueue.poll(timeout, unit); |
178 |
} |
179 |
|
180 |
} |