1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.8 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
jsr166 |
1.15 |
import java.util.concurrent.*; // for javadoc (till 6280605 is fixed) |
9 |
dl |
1.1 |
|
10 |
|
|
/** |
11 |
jozart |
1.2 |
* A {@link CompletionService} that uses a supplied {@link Executor} |
12 |
dl |
1.7 |
* 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 |
dl |
1.5 |
* |
17 |
|
|
* <p> |
18 |
|
|
* |
19 |
|
|
* <b>Usage Examples.</b> |
20 |
dl |
1.9 |
* |
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 |
dl |
1.5 |
* |
27 |
|
|
* <pre> |
28 |
jsr166 |
1.15 |
* void solve(Executor e, |
29 |
|
|
* Collection<Callable<Result>> solvers) |
30 |
|
|
* throws InterruptedException, ExecutionException { |
31 |
|
|
* CompletionService<Result> ecs |
32 |
|
|
* = new ExecutorCompletionService<Result>(e); |
33 |
|
|
* for (Callable<Result> s : solvers) |
34 |
|
|
* ecs.submit(s); |
35 |
|
|
* int n = solvers.size(); |
36 |
|
|
* for (int i = 0; i < n; ++i) { |
37 |
|
|
* Result r = ecs.take().get(); |
38 |
|
|
* if (r != null) |
39 |
|
|
* use(r); |
40 |
|
|
* } |
41 |
|
|
* } |
42 |
dl |
1.5 |
* </pre> |
43 |
|
|
* |
44 |
|
|
* Suppose instead that you would like to use the first non-null result |
45 |
dl |
1.9 |
* of the set of tasks, ignoring any that encounter exceptions, |
46 |
dl |
1.6 |
* and cancelling all other tasks when the first one is ready: |
47 |
dl |
1.5 |
* |
48 |
|
|
* <pre> |
49 |
jsr166 |
1.15 |
* void solve(Executor e, |
50 |
|
|
* Collection<Callable<Result>> solvers) |
51 |
|
|
* throws InterruptedException { |
52 |
|
|
* CompletionService<Result> ecs |
53 |
|
|
* = new ExecutorCompletionService<Result>(e); |
54 |
|
|
* int n = solvers.size(); |
55 |
|
|
* List<Future<Result>> futures |
56 |
|
|
* = new ArrayList<Future<Result>>(n); |
57 |
|
|
* Result result = null; |
58 |
|
|
* try { |
59 |
|
|
* for (Callable<Result> s : solvers) |
60 |
|
|
* futures.add(ecs.submit(s)); |
61 |
|
|
* for (int i = 0; i < n; ++i) { |
62 |
|
|
* try { |
63 |
|
|
* Result r = ecs.take().get(); |
64 |
|
|
* if (r != null) { |
65 |
|
|
* result = r; |
66 |
|
|
* break; |
67 |
|
|
* } |
68 |
|
|
* } catch (ExecutionException ignore) {} |
69 |
|
|
* } |
70 |
|
|
* } |
71 |
|
|
* finally { |
72 |
|
|
* for (Future<Result> f : futures) |
73 |
|
|
* f.cancel(true); |
74 |
|
|
* } |
75 |
|
|
* |
76 |
|
|
* if (result != null) |
77 |
|
|
* use(result); |
78 |
|
|
* } |
79 |
dl |
1.5 |
* </pre> |
80 |
dl |
1.1 |
*/ |
81 |
|
|
public class ExecutorCompletionService<V> implements CompletionService<V> { |
82 |
jozart |
1.2 |
private final Executor executor; |
83 |
dl |
1.13 |
private final AbstractExecutorService aes; |
84 |
dl |
1.3 |
private final BlockingQueue<Future<V>> completionQueue; |
85 |
dl |
1.1 |
|
86 |
|
|
/** |
87 |
|
|
* FutureTask extension to enqueue upon completion |
88 |
|
|
*/ |
89 |
dl |
1.13 |
private class QueueingFuture extends FutureTask<Void> { |
90 |
|
|
QueueingFuture(RunnableFuture<V> task) { |
91 |
|
|
super(task, null); |
92 |
|
|
this.task = task; |
93 |
|
|
} |
94 |
|
|
protected void done() { completionQueue.add(task); } |
95 |
|
|
private final Future<V> task; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
private RunnableFuture<V> newTaskFor(Callable<V> task) { |
99 |
|
|
if (aes == null) |
100 |
|
|
return new FutureTask<V>(task); |
101 |
|
|
else |
102 |
|
|
return aes.newTaskFor(task); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
private RunnableFuture<V> newTaskFor(Runnable task, V result) { |
106 |
|
|
if (aes == null) |
107 |
|
|
return new FutureTask<V>(task, result); |
108 |
|
|
else |
109 |
|
|
return aes.newTaskFor(task, result); |
110 |
dl |
1.1 |
} |
111 |
|
|
|
112 |
|
|
/** |
113 |
|
|
* Creates an ExecutorCompletionService using the supplied |
114 |
dl |
1.3 |
* executor for base task execution and a |
115 |
|
|
* {@link LinkedBlockingQueue} as a completion queue. |
116 |
jsr166 |
1.15 |
* |
117 |
dl |
1.5 |
* @param executor the executor to use |
118 |
dl |
1.10 |
* @throws NullPointerException if executor is <tt>null</tt> |
119 |
dl |
1.1 |
*/ |
120 |
jozart |
1.2 |
public ExecutorCompletionService(Executor executor) { |
121 |
jsr166 |
1.12 |
if (executor == null) |
122 |
dl |
1.1 |
throw new NullPointerException(); |
123 |
|
|
this.executor = executor; |
124 |
dl |
1.13 |
this.aes = (executor instanceof AbstractExecutorService) ? |
125 |
|
|
(AbstractExecutorService) executor : null; |
126 |
dl |
1.3 |
this.completionQueue = new LinkedBlockingQueue<Future<V>>(); |
127 |
dl |
1.1 |
} |
128 |
|
|
|
129 |
|
|
/** |
130 |
dl |
1.3 |
* Creates an ExecutorCompletionService using the supplied |
131 |
|
|
* executor for base task execution and the supplied queue as its |
132 |
|
|
* completion queue. |
133 |
jsr166 |
1.15 |
* |
134 |
dl |
1.5 |
* @param executor the executor to use |
135 |
dl |
1.7 |
* @param completionQueue the queue to use as the completion queue |
136 |
dl |
1.3 |
* normally one dedicated for use by this service |
137 |
dl |
1.10 |
* @throws NullPointerException if executor or completionQueue are <tt>null</tt> |
138 |
dl |
1.1 |
*/ |
139 |
dl |
1.3 |
public ExecutorCompletionService(Executor executor, |
140 |
|
|
BlockingQueue<Future<V>> completionQueue) { |
141 |
jsr166 |
1.12 |
if (executor == null || completionQueue == null) |
142 |
dl |
1.3 |
throw new NullPointerException(); |
143 |
|
|
this.executor = executor; |
144 |
dl |
1.13 |
this.aes = (executor instanceof AbstractExecutorService) ? |
145 |
|
|
(AbstractExecutorService) executor : null; |
146 |
dl |
1.3 |
this.completionQueue = completionQueue; |
147 |
dl |
1.1 |
} |
148 |
|
|
|
149 |
|
|
public Future<V> submit(Callable<V> task) { |
150 |
dl |
1.7 |
if (task == null) throw new NullPointerException(); |
151 |
dl |
1.13 |
RunnableFuture<V> f = newTaskFor(task); |
152 |
|
|
executor.execute(new QueueingFuture(f)); |
153 |
dl |
1.1 |
return f; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
public Future<V> submit(Runnable task, V result) { |
157 |
dl |
1.7 |
if (task == null) throw new NullPointerException(); |
158 |
dl |
1.13 |
RunnableFuture<V> f = newTaskFor(task, result); |
159 |
|
|
executor.execute(new QueueingFuture(f)); |
160 |
dl |
1.1 |
return f; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
public Future<V> take() throws InterruptedException { |
164 |
dl |
1.3 |
return completionQueue.take(); |
165 |
dl |
1.1 |
} |
166 |
|
|
|
167 |
|
|
public Future<V> poll() { |
168 |
dl |
1.3 |
return completionQueue.poll(); |
169 |
dl |
1.1 |
} |
170 |
|
|
|
171 |
|
|
public Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException { |
172 |
dl |
1.3 |
return completionQueue.poll(timeout, unit); |
173 |
dl |
1.1 |
} |
174 |
dl |
1.5 |
|
175 |
dl |
1.1 |
} |