ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorCompletionService9Test.java
(Generate patch)

Comparing jsr166/src/test/tck/ExecutorCompletionService9Test.java (file contents):
Revision 1.1 by jsr166, Sun May 22 01:09:21 2016 UTC vs.
Revision 1.7 by jsr166, Sun Nov 6 22:42:10 2016 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 import static java.util.concurrent.TimeUnit.MILLISECONDS;
9
8   import java.util.ArrayList;
9   import java.util.Collection;
10 + import java.util.Comparator;
11   import java.util.List;
12   import java.util.Set;
13   import java.util.HashSet;
14   import java.util.concurrent.Callable;
15   import java.util.concurrent.CompletionService;
16   import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorCompletionService;
17   import java.util.concurrent.Executor;
18 < import java.util.concurrent.ExecutorService;
21 < import java.util.concurrent.ForkJoinPool;
18 > import java.util.concurrent.ExecutorCompletionService;
19   import java.util.concurrent.Future;
20  
21   import junit.framework.Test;
# Line 37 | Line 34 | public class ExecutorCompletionService9T
34          throws InterruptedException, ExecutionException {
35          CompletionService<Integer> cs
36              = new ExecutorCompletionService<>(e);
37 <        solvers.forEach((solver) -> cs.submit(solver));
37 >        solvers.forEach(cs::submit);
38          for (int i = solvers.size(); i > 0; i--) {
39              Integer r = cs.take().get();
40              if (r != null)
# Line 54 | Line 51 | public class ExecutorCompletionService9T
51          List<Future<Integer>> futures = new ArrayList<>(n);
52          Integer result = null;
53          try {
54 <            solvers.forEach((solver) -> futures.add(cs.submit(solver)));
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();
# Line 65 | Line 62 | public class ExecutorCompletionService9T
62                  } catch (ExecutionException ignore) {}
63              }
64          } finally {
65 <            futures.forEach((future) -> future.cancel(true));
65 >            futures.forEach(future -> future.cancel(true));
66          }
67  
68          if (result != null)
69              use(result);
70      }
71  
72 <    HashSet<Integer> results;
72 >    ArrayList<Integer> results;
73  
74      void use(Integer x) {
75 <        if (results == null) results = new HashSet<Integer>();
75 >        if (results == null) results = new ArrayList<Integer>();
76          results.add(x);
77      }
78  
# Line 84 | Line 81 | public class ExecutorCompletionService9T
81       */
82      public void testSolveAll()
83          throws InterruptedException, ExecutionException {
84 +        results = null;
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);
91 >        solveAll(cachedThreadPool, solvers);
92 >        results.sort(Comparator.naturalOrder());
93 >        assertEquals(List.of(1, 2, 3), results);
94      }
95 <    
95 >
96      /**
97       * The second "solvers" sample code in the class javadoc works.
98       */
99      public void testSolveAny()
100          throws InterruptedException {
101 +        results = null;
102          Set<Callable<Integer>> solvers = Set.of(
103              () -> { throw new ArithmeticException(); },
104              () -> null,
105              () -> 1,
106              () -> 2);
107 <        solveAny(ForkJoinPool.commonPool(), solvers);
107 >        solveAny(cachedThreadPool, solvers);
108          assertEquals(1, results.size());
109 <        Integer elt = results.iterator().next();
109 >        Integer elt = results.get(0);
110          assertTrue(elt.equals(1) || elt.equals(2));
111      }
112  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines