1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  */
22 
23 /*
24  * This file is available under and governed by the GNU General Public
25  * License version 2 only, as published by the Free Software Foundation.
26  * However, the following notice accompanied the original version of this
27  * file:
28  *
29  * Written by Doug Lea and Martin Buchholz with assistance from
30  * members of JCP JSR-166 Expert Group and released to the public
31  * domain, as explained at
32  * http://creativecommons.org/publicdomain/zero/1.0/
33  */
34 
35 package test.java.util.concurrent.tck;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Comparator;
39 import java.util.List;
40 import java.util.Set;
41 import java.util.HashSet;
42 import java.util.concurrent.Callable;
43 import java.util.concurrent.CompletionService;
44 import java.util.concurrent.ExecutionException;
45 import java.util.concurrent.Executor;
46 import java.util.concurrent.ExecutorCompletionService;
47 import java.util.concurrent.Future;
48 
49 import junit.framework.Test;
50 import junit.framework.TestSuite;
51 
52 public class ExecutorCompletionService9Test extends JSR166TestCase {
main(String[] args)53     public static void main(String[] args) {
54         main(suite(), args);
55     }
suite()56     public static Test suite() {
57         return new TestSuite(ExecutorCompletionService9Test.class);
58     }
59 
solveAll(Executor e, Collection<Callable<Integer>> solvers)60     void solveAll(Executor e,
61                   Collection<Callable<Integer>> solvers)
62         throws InterruptedException, ExecutionException {
63         CompletionService<Integer> cs
64             = new ExecutorCompletionService<>(e);
65         solvers.forEach(cs::submit);
66         for (int i = solvers.size(); i > 0; i--) {
67             Integer r = cs.take().get();
68             if (r != null)
69                 use(r);
70         }
71     }
72 
solveAny(Executor e, Collection<Callable<Integer>> solvers)73     void solveAny(Executor e,
74                   Collection<Callable<Integer>> solvers)
75         throws InterruptedException {
76         CompletionService<Integer> cs
77             = new ExecutorCompletionService<>(e);
78         int n = solvers.size();
79         List<Future<Integer>> futures = new ArrayList<>(n);
80         Integer result = null;
81         try {
82             solvers.forEach(solver -> futures.add(cs.submit(solver)));
83             for (int i = n; i > 0; i--) {
84                 try {
85                     Integer r = cs.take().get();
86                     if (r != null) {
87                         result = r;
88                         break;
89                     }
90                 } catch (ExecutionException ignore) {}
91             }
92         } finally {
93             futures.forEach(future -> future.cancel(true));
94         }
95 
96         if (result != null)
97             use(result);
98     }
99 
100     ArrayList<Integer> results;
101 
use(Integer x)102     void use(Integer x) {
103         if (results == null) results = new ArrayList<Integer>();
104         results.add(x);
105     }
106 
107     /**
108      * The first "solvers" sample code in the class javadoc works.
109      */
testSolveAll()110     public void testSolveAll()
111         throws InterruptedException, ExecutionException {
112         results = null;
113         Set<Callable<Integer>> solvers = Set.of(
114             () -> null,
115             () -> 1,
116             () -> 2,
117             () -> 3,
118             () -> null);
119         solveAll(cachedThreadPool, solvers);
120         results.sort(Comparator.naturalOrder());
121         assertEquals(List.of(1, 2, 3), results);
122     }
123 
124     /**
125      * The second "solvers" sample code in the class javadoc works.
126      */
testSolveAny()127     public void testSolveAny()
128         throws InterruptedException {
129         results = null;
130         Set<Callable<Integer>> solvers = Set.of(
131             () -> { throw new ArithmeticException(); },
132             () -> null,
133             () -> 1,
134             () -> 2);
135         solveAny(cachedThreadPool, solvers);
136         assertEquals(1, results.size());
137         Integer elt = results.get(0);
138         assertTrue(elt.equals(1) || elt.equals(2));
139     }
140 
141 }
142