1 |
jsr166 |
1.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 |
jsr166 |
1.3 |
import java.util.concurrent.Executor; |
17 |
jsr166 |
1.1 |
import java.util.concurrent.ExecutorCompletionService; |
18 |
|
|
import java.util.concurrent.ExecutorService; |
19 |
|
|
import java.util.concurrent.Future; |
20 |
|
|
|
21 |
|
|
import junit.framework.Test; |
22 |
|
|
import junit.framework.TestSuite; |
23 |
|
|
|
24 |
|
|
public class ExecutorCompletionService9Test extends JSR166TestCase { |
25 |
|
|
public static void main(String[] args) { |
26 |
|
|
main(suite(), args); |
27 |
|
|
} |
28 |
|
|
public static Test suite() { |
29 |
|
|
return new TestSuite(ExecutorCompletionService9Test.class); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
void solveAll(Executor e, |
33 |
|
|
Collection<Callable<Integer>> solvers) |
34 |
|
|
throws InterruptedException, ExecutionException { |
35 |
|
|
CompletionService<Integer> cs |
36 |
|
|
= new ExecutorCompletionService<>(e); |
37 |
jsr166 |
1.4 |
solvers.forEach(cs::submit); |
38 |
jsr166 |
1.1 |
for (int i = solvers.size(); i > 0; i--) { |
39 |
|
|
Integer r = cs.take().get(); |
40 |
|
|
if (r != null) |
41 |
|
|
use(r); |
42 |
|
|
} |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
void solveAny(Executor e, |
46 |
|
|
Collection<Callable<Integer>> solvers) |
47 |
|
|
throws InterruptedException { |
48 |
|
|
CompletionService<Integer> cs |
49 |
|
|
= new ExecutorCompletionService<>(e); |
50 |
|
|
int n = solvers.size(); |
51 |
|
|
List<Future<Integer>> futures = new ArrayList<>(n); |
52 |
|
|
Integer result = null; |
53 |
|
|
try { |
54 |
|
|
solvers.forEach((solver) -> futures.add(cs.submit(solver))); |
55 |
|
|
for (int i = n; i > 0; i--) { |
56 |
|
|
try { |
57 |
|
|
Integer r = cs.take().get(); |
58 |
|
|
if (r != null) { |
59 |
|
|
result = r; |
60 |
|
|
break; |
61 |
|
|
} |
62 |
|
|
} catch (ExecutionException ignore) {} |
63 |
|
|
} |
64 |
|
|
} finally { |
65 |
|
|
futures.forEach((future) -> future.cancel(true)); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
if (result != null) |
69 |
|
|
use(result); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
HashSet<Integer> results; |
73 |
|
|
|
74 |
|
|
void use(Integer x) { |
75 |
|
|
if (results == null) results = new HashSet<Integer>(); |
76 |
|
|
results.add(x); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
/** |
80 |
|
|
* The first "solvers" sample code in the class javadoc works. |
81 |
|
|
*/ |
82 |
|
|
public void testSolveAll() |
83 |
|
|
throws InterruptedException, ExecutionException { |
84 |
|
|
Set<Callable<Integer>> solvers = Set.of( |
85 |
|
|
() -> null, |
86 |
|
|
() -> 1, |
87 |
|
|
() -> 2, |
88 |
|
|
() -> 3, |
89 |
|
|
() -> null); |
90 |
jsr166 |
1.3 |
solveAll(cachedThreadPool, solvers); |
91 |
jsr166 |
1.1 |
assertEquals(Set.of(1, 2, 3), results); |
92 |
|
|
} |
93 |
jsr166 |
1.2 |
|
94 |
jsr166 |
1.1 |
/** |
95 |
|
|
* The second "solvers" sample code in the class javadoc works. |
96 |
|
|
*/ |
97 |
|
|
public void testSolveAny() |
98 |
|
|
throws InterruptedException { |
99 |
|
|
Set<Callable<Integer>> solvers = Set.of( |
100 |
|
|
() -> { throw new ArithmeticException(); }, |
101 |
|
|
() -> null, |
102 |
|
|
() -> 1, |
103 |
|
|
() -> 2); |
104 |
jsr166 |
1.3 |
solveAny(cachedThreadPool, solvers); |
105 |
jsr166 |
1.1 |
assertEquals(1, results.size()); |
106 |
|
|
Integer elt = results.iterator().next(); |
107 |
|
|
assertTrue(elt.equals(1) || elt.equals(2)); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
} |