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 35import java.util.ArrayList; 36import java.util.Collection; 37import java.util.Comparator; 38import java.util.List; 39import java.util.Set; 40import java.util.concurrent.Callable; 41import java.util.concurrent.CompletionService; 42import java.util.concurrent.ExecutionException; 43import java.util.concurrent.Executor; 44import java.util.concurrent.ExecutorCompletionService; 45import java.util.concurrent.Future; 46 47import junit.framework.Test; 48import junit.framework.TestSuite; 49 50public class ExecutorCompletionService9Test extends JSR166TestCase { 51 public static void main(String[] args) { 52 main(suite(), args); 53 } 54 public static Test suite() { 55 return new TestSuite(ExecutorCompletionService9Test.class); 56 } 57 58 void solveAll(Executor e, 59 Collection<Callable<Integer>> solvers) 60 throws InterruptedException, ExecutionException { 61 CompletionService<Integer> cs 62 = new ExecutorCompletionService<>(e); 63 solvers.forEach(cs::submit); 64 for (int i = solvers.size(); i > 0; i--) { 65 Integer r = cs.take().get(); 66 if (r != null) 67 use(r); 68 } 69 } 70 71 void solveAny(Executor e, 72 Collection<Callable<Integer>> solvers) 73 throws InterruptedException { 74 CompletionService<Integer> cs 75 = new ExecutorCompletionService<>(e); 76 int n = solvers.size(); 77 List<Future<Integer>> futures = new ArrayList<>(n); 78 Integer result = null; 79 try { 80 solvers.forEach(solver -> futures.add(cs.submit(solver))); 81 for (int i = n; i > 0; i--) { 82 try { 83 Integer r = cs.take().get(); 84 if (r != null) { 85 result = r; 86 break; 87 } 88 } catch (ExecutionException ignore) {} 89 } 90 } finally { 91 futures.forEach(future -> future.cancel(true)); 92 } 93 94 if (result != null) 95 use(result); 96 } 97 98 ArrayList<Integer> results; 99 100 void use(Integer x) { 101 if (results == null) results = new ArrayList<Integer>(); 102 results.add(x); 103 } 104 105 /** 106 * The first "solvers" sample code in the class javadoc works. 107 */ 108 public void testSolveAll() 109 throws InterruptedException, ExecutionException { 110 results = null; 111 Set<Callable<Integer>> solvers = Set.of( 112 () -> null, 113 () -> 1, 114 () -> 2, 115 () -> 3, 116 () -> null); 117 solveAll(cachedThreadPool, solvers); 118 results.sort(Comparator.naturalOrder()); 119 assertEquals(List.of(1, 2, 3), results); 120 } 121 122 /** 123 * The second "solvers" sample code in the class javadoc works. 124 */ 125 public void testSolveAny() 126 throws InterruptedException { 127 results = null; 128 Set<Callable<Integer>> solvers = Set.of( 129 () -> { throw new ArithmeticException(); }, 130 () -> null, 131 () -> 1, 132 () -> 2); 133 solveAny(cachedThreadPool, solvers); 134 assertEquals(1, results.size()); 135 Integer elt = results.get(0); 136 assertTrue(elt.equals(1) || elt.equals(2)); 137 } 138 139} 140