ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorCompletionService9Test.java
Revision: 1.9
Committed: Wed Jan 27 01:57:24 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +1 -1 lines
Log Message:
use diamond <> pervasively

File Contents

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