author | dl |
Fri, 13 Oct 2017 18:12:54 -0700 | |
changeset 47341 | ed1fd45b6eb5 |
parent 47216 | 71c04702a3d5 |
child 47727 | 53020d8cdf5b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6267833 |
|
27 |
* @summary Tests for invokeAny, invokeAll |
|
28 |
* @author Martin Buchholz |
|
29 |
*/ |
|
30 |
||
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
31 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
32 |
import static java.util.concurrent.TimeUnit.SECONDS; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
33 |
|
34347 | 34 |
import java.util.List; |
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
35 |
import java.util.stream.Collectors; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
36 |
import java.util.stream.IntStream; |
34347 | 37 |
import java.util.concurrent.Callable; |
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
38 |
import java.util.concurrent.CyclicBarrier; |
34347 | 39 |
import java.util.concurrent.ExecutorService; |
40 |
import java.util.concurrent.Executors; |
|
41 |
import java.util.concurrent.Future; |
|
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
42 |
import java.util.concurrent.ThreadLocalRandom; |
34347 | 43 |
import java.util.concurrent.atomic.AtomicLong; |
2 | 44 |
|
45 |
public class Invoke { |
|
46 |
static volatile int passed = 0, failed = 0; |
|
47 |
||
48 |
static void fail(String msg) { |
|
49 |
failed++; |
|
50 |
new AssertionError(msg).printStackTrace(); |
|
51 |
} |
|
52 |
||
53 |
static void pass() { |
|
54 |
passed++; |
|
55 |
} |
|
56 |
||
57 |
static void unexpected(Throwable t) { |
|
58 |
failed++; |
|
59 |
t.printStackTrace(); |
|
60 |
} |
|
61 |
||
62 |
static void check(boolean condition, String msg) { |
|
63 |
if (condition) pass(); else fail(msg); |
|
64 |
} |
|
65 |
||
66 |
static void check(boolean condition) { |
|
67 |
check(condition, "Assertion failure"); |
|
68 |
} |
|
69 |
||
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
70 |
static long secondsElapsedSince(long startTime) { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
71 |
return NANOSECONDS.toSeconds(System.nanoTime() - startTime); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
72 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
73 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
74 |
static void awaitInterrupt(long timeoutSeconds) { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
75 |
long startTime = System.nanoTime(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
76 |
try { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
77 |
Thread.sleep(SECONDS.toMillis(timeoutSeconds)); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
78 |
fail("timed out waiting for interrupt"); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
79 |
} catch (InterruptedException expected) { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
80 |
check(secondsElapsedSince(startTime) < timeoutSeconds); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
81 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
82 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
83 |
|
2 | 84 |
public static void main(String[] args) { |
85 |
try { |
|
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
86 |
testInvokeAll(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
87 |
testInvokeAny(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
88 |
testInvokeAny_cancellationInterrupt(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
89 |
} catch (Throwable t) { unexpected(t); } |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
90 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
91 |
if (failed > 0) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
92 |
throw new Error( |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
93 |
String.format("Passed = %d, failed = %d", passed, failed)); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
94 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
95 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
96 |
static final long timeoutSeconds = 10L; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
97 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
98 |
static void testInvokeAll() throws Throwable { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
99 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
100 |
final int nThreads = rnd.nextInt(2, 7); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
101 |
final boolean timed = rnd.nextBoolean(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
102 |
final ExecutorService pool = Executors.newFixedThreadPool(nThreads); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
103 |
final AtomicLong count = new AtomicLong(0); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
104 |
class Task implements Callable<Long> { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
105 |
public Long call() throws Exception { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
106 |
return count.incrementAndGet(); |
2 | 107 |
} |
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
108 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
109 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
110 |
try { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
111 |
final List<Task> tasks = |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
112 |
IntStream.range(0, nThreads) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
113 |
.mapToObj(i -> new Task()) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
114 |
.collect(Collectors.toList()); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
115 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
116 |
List<Future<Long>> futures; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
117 |
if (timed) { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
118 |
long startTime = System.nanoTime(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
119 |
futures = pool.invokeAll(tasks, timeoutSeconds, SECONDS); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
120 |
check(secondsElapsedSince(startTime) < timeoutSeconds); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
121 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
122 |
else |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
123 |
futures = pool.invokeAll(tasks); |
2 | 124 |
check(futures.size() == tasks.size()); |
125 |
check(count.get() == tasks.size()); |
|
126 |
||
127 |
long gauss = 0; |
|
128 |
for (Future<Long> future : futures) gauss += future.get(); |
|
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
129 |
check(gauss == (tasks.size()+1)*tasks.size()/2); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
130 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
131 |
pool.shutdown(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
132 |
check(pool.awaitTermination(10L, SECONDS)); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
133 |
} finally { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
134 |
pool.shutdownNow(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
135 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
136 |
} |
2 | 137 |
|
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
138 |
static void testInvokeAny() throws Throwable { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
139 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
140 |
final boolean timed = rnd.nextBoolean(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
141 |
final ExecutorService pool = Executors.newSingleThreadExecutor(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
142 |
final AtomicLong count = new AtomicLong(0); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
143 |
class Task implements Callable<Long> { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
144 |
public Long call() throws Exception { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
145 |
long x = count.incrementAndGet(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
146 |
check(x <= 2); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
147 |
if (x == 2) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
148 |
// wait for main thread to interrupt us |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
149 |
awaitInterrupt(timeoutSeconds); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
150 |
return x; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
151 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
152 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
153 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
154 |
try { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
155 |
final List<Task> tasks = |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
156 |
IntStream.range(0, rnd.nextInt(1, 7)) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
157 |
.mapToObj(i -> new Task()) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
158 |
.collect(Collectors.toList()); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
159 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
160 |
long val; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
161 |
if (timed) { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
162 |
long startTime = System.nanoTime(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
163 |
val = pool.invokeAny(tasks, timeoutSeconds, SECONDS); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
164 |
check(secondsElapsedSince(startTime) < timeoutSeconds); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
165 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
166 |
else |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
167 |
val = pool.invokeAny(tasks); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
168 |
check(val == 1); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
169 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
170 |
// inherent race between main thread interrupt and |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
171 |
// start of second task |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
172 |
check(count.get() == 1 || count.get() == 2); |
2 | 173 |
|
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
174 |
pool.shutdown(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
175 |
check(pool.awaitTermination(timeoutSeconds, SECONDS)); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
176 |
} finally { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
177 |
pool.shutdownNow(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
178 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
179 |
} |
2 | 180 |
|
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
181 |
/** |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
182 |
* Every remaining running task is sent an interrupt for cancellation. |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
183 |
*/ |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
184 |
static void testInvokeAny_cancellationInterrupt() throws Throwable { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
185 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
186 |
final int nThreads = rnd.nextInt(2, 7); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
187 |
final boolean timed = rnd.nextBoolean(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
188 |
final ExecutorService pool = Executors.newFixedThreadPool(nThreads); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
189 |
final AtomicLong count = new AtomicLong(0); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
190 |
final AtomicLong interruptedCount = new AtomicLong(0); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
191 |
final CyclicBarrier allStarted = new CyclicBarrier(nThreads); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
192 |
class Task implements Callable<Long> { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
193 |
public Long call() throws Exception { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
194 |
allStarted.await(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
195 |
long x = count.incrementAndGet(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
196 |
if (x > 1) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
197 |
// main thread will interrupt us |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
198 |
awaitInterrupt(timeoutSeconds); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
199 |
return x; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
200 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
201 |
} |
2 | 202 |
|
47341
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
203 |
try { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
204 |
final List<Task> tasks = |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
205 |
IntStream.range(0, nThreads) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
206 |
.mapToObj(i -> new Task()) |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
207 |
.collect(Collectors.toList()); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
208 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
209 |
long val; |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
210 |
if (timed) { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
211 |
long startTime = System.nanoTime(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
212 |
val = pool.invokeAny(tasks, timeoutSeconds, SECONDS); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
213 |
check(secondsElapsedSince(startTime) < timeoutSeconds); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
214 |
} |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
215 |
else |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
216 |
val = pool.invokeAny(tasks); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
217 |
check(val == 1); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
218 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
219 |
pool.shutdown(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
220 |
check(pool.awaitTermination(timeoutSeconds, SECONDS)); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
221 |
|
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
222 |
// Check after shutdown to avoid race |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
223 |
check(count.get() == nThreads); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
224 |
} finally { |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
225 |
pool.shutdownNow(); |
ed1fd45b6eb5
8188853: java/util/concurrent/ExecutorService/Invoke.java Assertion failure
dl
parents:
47216
diff
changeset
|
226 |
} |
2 | 227 |
} |
228 |
} |