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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines