1 |
/* |
2 |
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
* members of JCP JSR-166 Expert Group and released to the public |
4 |
* domain, as explained at |
5 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
*/ |
7 |
|
8 |
import java.util.ArrayList; |
9 |
import java.util.Collection; |
10 |
import java.util.List; |
11 |
import java.util.Set; |
12 |
import java.util.HashSet; |
13 |
import java.util.concurrent.Callable; |
14 |
import java.util.concurrent.CompletionService; |
15 |
import java.util.concurrent.ExecutionException; |
16 |
import java.util.concurrent.ExecutorCompletionService; |
17 |
import java.util.concurrent.Executor; |
18 |
import java.util.concurrent.ExecutorService; |
19 |
import java.util.concurrent.ForkJoinPool; |
20 |
import java.util.concurrent.Future; |
21 |
|
22 |
import junit.framework.Test; |
23 |
import junit.framework.TestSuite; |
24 |
|
25 |
public class ExecutorCompletionService9Test extends JSR166TestCase { |
26 |
public static void main(String[] args) { |
27 |
main(suite(), args); |
28 |
} |
29 |
public static Test suite() { |
30 |
return new TestSuite(ExecutorCompletionService9Test.class); |
31 |
} |
32 |
|
33 |
void solveAll(Executor e, |
34 |
Collection<Callable<Integer>> solvers) |
35 |
throws InterruptedException, ExecutionException { |
36 |
CompletionService<Integer> cs |
37 |
= new ExecutorCompletionService<>(e); |
38 |
solvers.forEach((solver) -> cs.submit(solver)); |
39 |
for (int i = solvers.size(); i > 0; i--) { |
40 |
Integer r = cs.take().get(); |
41 |
if (r != null) |
42 |
use(r); |
43 |
} |
44 |
} |
45 |
|
46 |
void solveAny(Executor e, |
47 |
Collection<Callable<Integer>> solvers) |
48 |
throws InterruptedException { |
49 |
CompletionService<Integer> cs |
50 |
= new ExecutorCompletionService<>(e); |
51 |
int n = solvers.size(); |
52 |
List<Future<Integer>> futures = new ArrayList<>(n); |
53 |
Integer result = null; |
54 |
try { |
55 |
solvers.forEach((solver) -> futures.add(cs.submit(solver))); |
56 |
for (int i = n; i > 0; i--) { |
57 |
try { |
58 |
Integer r = cs.take().get(); |
59 |
if (r != null) { |
60 |
result = r; |
61 |
break; |
62 |
} |
63 |
} catch (ExecutionException ignore) {} |
64 |
} |
65 |
} finally { |
66 |
futures.forEach((future) -> future.cancel(true)); |
67 |
} |
68 |
|
69 |
if (result != null) |
70 |
use(result); |
71 |
} |
72 |
|
73 |
HashSet<Integer> results; |
74 |
|
75 |
void use(Integer x) { |
76 |
if (results == null) results = new HashSet<Integer>(); |
77 |
results.add(x); |
78 |
} |
79 |
|
80 |
/** |
81 |
* The first "solvers" sample code in the class javadoc works. |
82 |
*/ |
83 |
public void testSolveAll() |
84 |
throws InterruptedException, ExecutionException { |
85 |
Set<Callable<Integer>> solvers = Set.of( |
86 |
() -> null, |
87 |
() -> 1, |
88 |
() -> 2, |
89 |
() -> 3, |
90 |
() -> null); |
91 |
solveAll(ForkJoinPool.commonPool(), solvers); |
92 |
assertEquals(Set.of(1, 2, 3), results); |
93 |
} |
94 |
|
95 |
/** |
96 |
* The second "solvers" sample code in the class javadoc works. |
97 |
*/ |
98 |
public void testSolveAny() |
99 |
throws InterruptedException { |
100 |
Set<Callable<Integer>> solvers = Set.of( |
101 |
() -> { throw new ArithmeticException(); }, |
102 |
() -> null, |
103 |
() -> 1, |
104 |
() -> 2); |
105 |
solveAny(ForkJoinPool.commonPool(), solvers); |
106 |
assertEquals(1, results.size()); |
107 |
Integer elt = results.iterator().next(); |
108 |
assertTrue(elt.equals(1) || elt.equals(2)); |
109 |
} |
110 |
|
111 |
} |