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 <tt>take</tt>. |
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 |
|
* |
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 <tt>Result</tt>, and would like to |
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 <tt>use(Result r)</tt>. You |
23 |
> |
* return a non-null value, in some method {@code use(Result r)}. You |
24 |
|
* could write this as: |
25 |
|
* |
26 |
< |
* <pre> |
27 |
< |
* void solve(Executor e, |
28 |
< |
* Collection<Callable<Result>> solvers) |
26 |
> |
* <pre> {@code |
27 |
> |
* void solve(Executor e, |
28 |
> |
* Collection<Callable<Result>> solvers) |
29 |
|
* throws InterruptedException, ExecutionException { |
30 |
< |
* CompletionService<Result> ecs |
31 |
< |
* = new ExecutorCompletionService<Result>(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 |
< |
* } |
41 |
< |
* </pre> |
30 |
> |
* CompletionService<Result> ecs |
31 |
> |
* = new ExecutorCompletionService<Result>(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> |
47 |
< |
* void solve(Executor e, |
48 |
< |
* Collection<Callable<Result>> solvers) |
46 |
> |
* <pre> {@code |
47 |
> |
* void solve(Executor e, |
48 |
> |
* Collection<Callable<Result>> solvers) |
49 |
|
* throws InterruptedException { |
50 |
< |
* CompletionService<Result> ecs |
51 |
< |
* = new ExecutorCompletionService<Result>(e); |
52 |
< |
* int n = solvers.size(); |
53 |
< |
* List<Future<Result>> futures |
54 |
< |
* = new ArrayList<Future<Result>>(n); |
55 |
< |
* Result result = null; |
56 |
< |
* try { |
57 |
< |
* for (Callable<Result> s : solvers) |
58 |
< |
* futures.add(ecs.submit(s)); |
59 |
< |
* for (int i = 0; i < n; ++i) { |
60 |
< |
* try { |
61 |
< |
* Result r = ecs.take().get(); |
62 |
< |
* if (r != null) { |
63 |
< |
* result = r; |
64 |
< |
* break; |
65 |
< |
* } |
66 |
< |
* } catch (ExecutionException ignore) {} |
67 |
< |
* } |
68 |
< |
* } |
69 |
< |
* finally { |
70 |
< |
* for (Future<Result> f : futures) |
71 |
< |
* f.cancel(true); |
72 |
< |
* } |
73 |
< |
* |
74 |
< |
* if (result != null) |
75 |
< |
* use(result); |
76 |
< |
* } |
78 |
< |
* </pre> |
50 |
> |
* CompletionService<Result> ecs |
51 |
> |
* = new ExecutorCompletionService<Result>(e); |
52 |
> |
* int n = solvers.size(); |
53 |
> |
* List<Future<Result>> futures |
54 |
> |
* = new ArrayList<Future<Result>>(n); |
55 |
> |
* Result result = null; |
56 |
> |
* try { |
57 |
> |
* for (Callable<Result> s : solvers) |
58 |
> |
* futures.add(ecs.submit(s)); |
59 |
> |
* for (int i = 0; i < n; ++i) { |
60 |
> |
* try { |
61 |
> |
* Result r = ecs.take().get(); |
62 |
> |
* if (r != null) { |
63 |
> |
* result = r; |
64 |
> |
* break; |
65 |
> |
* } |
66 |
> |
* } catch (ExecutionException ignore) {} |
67 |
> |
* } |
68 |
> |
* } |
69 |
> |
* finally { |
70 |
> |
* for (Future<Result> f : futures) |
71 |
> |
* f.cancel(true); |
72 |
> |
* } |
73 |
> |
* |
74 |
> |
* if (result != null) |
75 |
> |
* use(result); |
76 |
> |
* }}</pre> |
77 |
|
*/ |
78 |
|
public class ExecutorCompletionService<V> implements CompletionService<V> { |
79 |
|
private final Executor executor; |
112 |
|
* {@link LinkedBlockingQueue} as a completion queue. |
113 |
|
* |
114 |
|
* @param executor the executor to use |
115 |
< |
* @throws NullPointerException if executor is <tt>null</tt> |
115 |
> |
* @throws NullPointerException if executor is {@code null} |
116 |
|
*/ |
117 |
|
public ExecutorCompletionService(Executor executor) { |
118 |
|
if (executor == null) |
130 |
|
* |
131 |
|
* @param executor the executor to use |
132 |
|
* @param completionQueue the queue to use as the completion queue |
133 |
< |
* normally one dedicated for use by this service. This queue is |
134 |
< |
* treated as unbounded -- failed attempted <tt>Queue.add</tt> |
135 |
< |
* operations for completed taskes cause them not to be |
136 |
< |
* retrievable. |
137 |
< |
* @throws NullPointerException if executor or completionQueue are <tt>null</tt> |
133 |
> |
* normally one dedicated for use by this service. This |
134 |
> |
* queue is treated as unbounded -- failed attempted |
135 |
> |
* {@code Queue.add} operations for completed taskes cause |
136 |
> |
* them not to be retrievable. |
137 |
> |
* @throws NullPointerException if executor or completionQueue are {@code null} |
138 |
|
*/ |
139 |
|
public ExecutorCompletionService(Executor executor, |
140 |
|
BlockingQueue<Future<V>> completionQueue) { |