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