9 |
|
|
10 |
|
/** |
11 |
|
* A {@link CompletionService} that uses a supplied {@link Executor} |
12 |
< |
* to execute tasks. An <tt>ExecutorCompletionService</tt> can be |
13 |
< |
* useful as an add-on to solve task coordination problems. |
12 |
> |
* to execute tasks. |
13 |
|
* |
14 |
|
* <p> |
15 |
|
* |
16 |
|
* <b>Usage Examples.</b> |
17 |
< |
* Suppose you have a set of solvers for a certain problem, |
17 |
> |
* Suppose you have a set of solvers for a certain problem (each returning |
18 |
> |
* a value of some type <tt>Result</tt>), |
19 |
|
* and would like to run them concurrently, using the results of each of them |
20 |
|
* that return a non-null value. You could write this as: |
21 |
|
* |
22 |
|
* <pre> |
23 |
< |
* void solve(Executor e, Collection<Callable<Result>> solvers) |
24 |
< |
* throws InterruptedException, ExecutionException { |
25 |
< |
* ExecutorCompletionService<Result> ecs = new |
26 |
< |
* ExecutorCompletionService<Result>(e); |
23 |
> |
* void solve(Executor e, Collection<Callable<Result>> solvers) |
24 |
> |
* throws InterruptedException, ExecutionException { |
25 |
> |
* CompletionService<Result> ecs = new ExecutorCompletionService<Result>(e); |
26 |
|
* for (Callable<Result> s : solvers) |
27 |
|
* ecs.submit(s); |
28 |
|
* int n = solvers.size(); |
35 |
|
* </pre> |
36 |
|
* |
37 |
|
* Suppose instead that you would like to use the first non-null result |
38 |
< |
* of a set of tasks, ignoring any of those that encounter exceptions |
39 |
< |
* and cancelling all of the other tasks when the first one is ready: |
38 |
> |
* of the set of tasks, ignoring any of those that encounter exceptions, |
39 |
> |
* and cancelling all other tasks when the first one is ready: |
40 |
|
* |
41 |
|
* <pre> |
42 |
|
* void solve(Executor e, Collection<Callable<Result>> solvers) |
43 |
< |
* throws InterruptedException { |
44 |
< |
* ExecutorCompletionService<Result> ecs = |
46 |
< |
* new ExecutorCompletionService<Result>(e); |
43 |
> |
* throws InterruptedException { |
44 |
> |
* CompletionService<Result> ecs = new ExecutorCompletionService<Result>(e); |
45 |
|
* int n = solvers.size(); |
46 |
< |
* ArrayList<Future<Result>> futures = |
49 |
< |
* new ArrayList<Future<Result>>(n); |
46 |
> |
* List<Future<Result>> futures = new ArrayList<Future<Result>>(n); |
47 |
|
* Result result = null; |
48 |
|
* try { |
49 |
|
* for (Callable<Result> s : solvers) |