author | dl |
Wed, 28 Nov 2018 15:25:14 -0800 | |
changeset 52730 | 345266000aba |
parent 48541 | 946e34c2dec9 |
child 58138 | 1e4270f875ee |
permissions | -rw-r--r-- |
35394 | 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 with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
|
33 |
* Pat Fisher, Mike Judd. |
|
34 |
*/ |
|
35 |
||
36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
37 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
35394 | 38 |
import static java.util.concurrent.TimeUnit.SECONDS; |
39 |
||
40 |
import java.util.ArrayList; |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
41 |
import java.util.Collection; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
42 |
import java.util.Collections; |
35394 | 43 |
import java.util.HashSet; |
44 |
import java.util.List; |
|
45 |
import java.util.concurrent.BlockingQueue; |
|
46 |
import java.util.concurrent.Callable; |
|
47 |
import java.util.concurrent.CancellationException; |
|
48 |
import java.util.concurrent.CountDownLatch; |
|
49 |
import java.util.concurrent.ExecutionException; |
|
50 |
import java.util.concurrent.ExecutorService; |
|
51 |
import java.util.concurrent.Future; |
|
52 |
import java.util.concurrent.RejectedExecutionException; |
|
53 |
import java.util.concurrent.ScheduledFuture; |
|
54 |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
|
55 |
import java.util.concurrent.ThreadFactory; |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
56 |
import java.util.concurrent.ThreadLocalRandom; |
35394 | 57 |
import java.util.concurrent.ThreadPoolExecutor; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
58 |
import java.util.concurrent.atomic.AtomicBoolean; |
35394 | 59 |
import java.util.concurrent.atomic.AtomicInteger; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
60 |
import java.util.concurrent.atomic.AtomicLong; |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
61 |
import java.util.stream.Stream; |
35394 | 62 |
|
63 |
import junit.framework.Test; |
|
64 |
import junit.framework.TestSuite; |
|
65 |
||
66 |
public class ScheduledExecutorTest extends JSR166TestCase { |
|
67 |
public static void main(String[] args) { |
|
68 |
main(suite(), args); |
|
69 |
} |
|
70 |
public static Test suite() { |
|
71 |
return new TestSuite(ScheduledExecutorTest.class); |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* execute successfully executes a runnable |
|
76 |
*/ |
|
77 |
public void testExecute() throws InterruptedException { |
|
78 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
79 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
80 |
final CountDownLatch done = new CountDownLatch(1); |
|
81 |
final Runnable task = new CheckedRunnable() { |
|
82 |
public void realRun() { done.countDown(); }}; |
|
83 |
p.execute(task); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
84 |
await(done); |
35394 | 85 |
} |
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* delayed schedule of callable successfully executes after delay |
|
90 |
*/ |
|
91 |
public void testSchedule1() throws Exception { |
|
92 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
93 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
94 |
final long startTime = System.nanoTime(); |
|
95 |
final CountDownLatch done = new CountDownLatch(1); |
|
96 |
Callable task = new CheckedCallable<Boolean>() { |
|
97 |
public Boolean realCall() { |
|
98 |
done.countDown(); |
|
99 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
100 |
return Boolean.TRUE; |
|
101 |
}}; |
|
102 |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
|
103 |
assertSame(Boolean.TRUE, f.get()); |
|
104 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
105 |
assertEquals(0L, done.getCount()); |
35394 | 106 |
} |
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* delayed schedule of runnable successfully executes after delay |
|
111 |
*/ |
|
112 |
public void testSchedule3() throws Exception { |
|
113 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
114 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
115 |
final long startTime = System.nanoTime(); |
|
116 |
final CountDownLatch done = new CountDownLatch(1); |
|
117 |
Runnable task = new CheckedRunnable() { |
|
118 |
public void realRun() { |
|
119 |
done.countDown(); |
|
120 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
121 |
}}; |
|
122 |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
|
123 |
await(done); |
|
124 |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
|
125 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
* scheduleAtFixedRate executes runnable after given initial delay |
|
131 |
*/ |
|
132 |
public void testSchedule4() throws Exception { |
|
133 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
134 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
135 |
final long startTime = System.nanoTime(); |
|
136 |
final CountDownLatch done = new CountDownLatch(1); |
|
137 |
Runnable task = new CheckedRunnable() { |
|
138 |
public void realRun() { |
|
139 |
done.countDown(); |
|
140 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
141 |
}}; |
|
142 |
ScheduledFuture f = |
|
143 |
p.scheduleAtFixedRate(task, timeoutMillis(), |
|
144 |
LONG_DELAY_MS, MILLISECONDS); |
|
145 |
await(done); |
|
146 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
147 |
f.cancel(true); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* scheduleWithFixedDelay executes runnable after given initial delay |
|
153 |
*/ |
|
154 |
public void testSchedule5() throws Exception { |
|
155 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
156 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
157 |
final long startTime = System.nanoTime(); |
|
158 |
final CountDownLatch done = new CountDownLatch(1); |
|
159 |
Runnable task = new CheckedRunnable() { |
|
160 |
public void realRun() { |
|
161 |
done.countDown(); |
|
162 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
163 |
}}; |
|
164 |
ScheduledFuture f = |
|
165 |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
|
166 |
LONG_DELAY_MS, MILLISECONDS); |
|
167 |
await(done); |
|
168 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
169 |
f.cancel(true); |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
static class RunnableCounter implements Runnable { |
|
174 |
AtomicInteger count = new AtomicInteger(0); |
|
175 |
public void run() { count.getAndIncrement(); } |
|
176 |
} |
|
177 |
||
178 |
/** |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
179 |
* scheduleAtFixedRate executes series of tasks at given rate. |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
180 |
* Eventually, it must hold that: |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
181 |
* cycles - 1 <= elapsedMillis/delay < cycles |
35394 | 182 |
*/ |
183 |
public void testFixedRateSequence() throws InterruptedException { |
|
184 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
185 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
186 |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
187 |
final long startTime = System.nanoTime(); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
188 |
final int cycles = 8; |
35394 | 189 |
final CountDownLatch done = new CountDownLatch(cycles); |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
190 |
final Runnable task = new CheckedRunnable() { |
35394 | 191 |
public void realRun() { done.countDown(); }}; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
192 |
final ScheduledFuture periodicTask = |
35394 | 193 |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
194 |
final int totalDelayMillis = (cycles - 1) * delay; |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
195 |
await(done, totalDelayMillis + LONG_DELAY_MS); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
196 |
periodicTask.cancel(true); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
197 |
final long elapsedMillis = millisElapsedSince(startTime); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
198 |
assertTrue(elapsedMillis >= totalDelayMillis); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
199 |
if (elapsedMillis <= cycles * delay) |
35394 | 200 |
return; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
201 |
// else retry with longer delay |
35394 | 202 |
} |
203 |
fail("unexpected execution rate"); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
/** |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
208 |
* scheduleWithFixedDelay executes series of tasks with given period. |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
209 |
* Eventually, it must hold that each task starts at least delay and at |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
210 |
* most 2 * delay after the termination of the previous task. |
35394 | 211 |
*/ |
212 |
public void testFixedDelaySequence() throws InterruptedException { |
|
213 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
214 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
215 |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
|
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
216 |
final long startTime = System.nanoTime(); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
217 |
final AtomicLong previous = new AtomicLong(startTime); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
218 |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
219 |
final int cycles = 8; |
35394 | 220 |
final CountDownLatch done = new CountDownLatch(cycles); |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
221 |
final int d = delay; |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
222 |
final Runnable task = new CheckedRunnable() { |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
223 |
public void realRun() { |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
224 |
long now = System.nanoTime(); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
225 |
long elapsedMillis |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
226 |
= NANOSECONDS.toMillis(now - previous.get()); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
227 |
if (done.getCount() == cycles) { // first execution |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
228 |
if (elapsedMillis >= d) |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
229 |
tryLongerDelay.set(true); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
230 |
} else { |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
231 |
assertTrue(elapsedMillis >= d); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
232 |
if (elapsedMillis >= 2 * d) |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
233 |
tryLongerDelay.set(true); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
234 |
} |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
235 |
previous.set(now); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
236 |
done.countDown(); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
237 |
}}; |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
238 |
final ScheduledFuture periodicTask = |
35394 | 239 |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
240 |
final int totalDelayMillis = (cycles - 1) * delay; |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
241 |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
242 |
periodicTask.cancel(true); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
243 |
final long elapsedMillis = millisElapsedSince(startTime); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
244 |
assertTrue(elapsedMillis >= totalDelayMillis); |
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
245 |
if (!tryLongerDelay.get()) |
35394 | 246 |
return; |
36231
52e0ee9847fb
8150319: ScheduledExecutorTest:testFixedDelaySequence timeout with slow VMs
dl
parents:
35394
diff
changeset
|
247 |
// else retry with longer delay |
35394 | 248 |
} |
249 |
fail("unexpected execution rate"); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
254 |
* Submitting null tasks throws NullPointerException |
35394 | 255 |
*/ |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
256 |
public void testNullTaskSubmission() { |
35394 | 257 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
258 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
259 |
assertNullTaskSubmissionThrowsNullPointerException(p); |
35394 | 260 |
} |
261 |
} |
|
262 |
||
263 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
264 |
* Submitted tasks are rejected when shutdown |
35394 | 265 |
*/ |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
266 |
public void testSubmittedTasksRejectedWhenShutdown() throws InterruptedException { |
35394 | 267 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
268 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
269 |
final CountDownLatch threadsStarted = new CountDownLatch(p.getCorePoolSize()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
270 |
final CountDownLatch done = new CountDownLatch(1); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
271 |
final Runnable r = () -> { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
272 |
threadsStarted.countDown(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
273 |
for (;;) { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
274 |
try { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
275 |
done.await(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
276 |
return; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
277 |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
278 |
}}; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
279 |
final Callable<Boolean> c = () -> { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
280 |
threadsStarted.countDown(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
281 |
for (;;) { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
282 |
try { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
283 |
done.await(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
284 |
return Boolean.TRUE; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
285 |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
286 |
}}; |
35394 | 287 |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
288 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
289 |
for (int i = p.getCorePoolSize(); i--> 0; ) { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
290 |
switch (rnd.nextInt(4)) { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
291 |
case 0: p.execute(r); break; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
292 |
case 1: assertFalse(p.submit(r).isDone()); break; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
293 |
case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
294 |
case 3: assertFalse(p.submit(c).isDone()); break; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
295 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
296 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
297 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
298 |
// ScheduledThreadPoolExecutor has an unbounded queue, so never saturated. |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
299 |
await(threadsStarted); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
300 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
301 |
if (rnd.nextBoolean()) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
302 |
p.shutdownNow(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
303 |
else |
35394 | 304 |
p.shutdown(); |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
305 |
// Pool is shutdown, but not yet terminated |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
306 |
assertTaskSubmissionsAreRejected(p); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
307 |
assertFalse(p.isTerminated()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
308 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
309 |
done.countDown(); // release blocking tasks |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
310 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
311 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
312 |
assertTaskSubmissionsAreRejected(p); |
35394 | 313 |
} |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
314 |
assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount()); |
35394 | 315 |
} |
316 |
||
317 |
/** |
|
318 |
* getActiveCount increases but doesn't overestimate, when a |
|
319 |
* thread becomes active |
|
320 |
*/ |
|
321 |
public void testGetActiveCount() throws InterruptedException { |
|
322 |
final CountDownLatch done = new CountDownLatch(1); |
|
323 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
|
324 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
325 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
326 |
assertEquals(0, p.getActiveCount()); |
|
327 |
p.execute(new CheckedRunnable() { |
|
328 |
public void realRun() throws InterruptedException { |
|
329 |
threadStarted.countDown(); |
|
330 |
assertEquals(1, p.getActiveCount()); |
|
331 |
await(done); |
|
332 |
}}); |
|
333 |
await(threadStarted); |
|
334 |
assertEquals(1, p.getActiveCount()); |
|
335 |
} |
|
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* getCompletedTaskCount increases, but doesn't overestimate, |
|
340 |
* when tasks complete |
|
341 |
*/ |
|
342 |
public void testGetCompletedTaskCount() throws InterruptedException { |
|
343 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
|
344 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
345 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
346 |
final CountDownLatch threadProceed = new CountDownLatch(1); |
|
347 |
final CountDownLatch threadDone = new CountDownLatch(1); |
|
348 |
assertEquals(0, p.getCompletedTaskCount()); |
|
349 |
p.execute(new CheckedRunnable() { |
|
350 |
public void realRun() throws InterruptedException { |
|
351 |
threadStarted.countDown(); |
|
352 |
assertEquals(0, p.getCompletedTaskCount()); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
353 |
await(threadProceed); |
35394 | 354 |
threadDone.countDown(); |
355 |
}}); |
|
356 |
await(threadStarted); |
|
357 |
assertEquals(0, p.getCompletedTaskCount()); |
|
358 |
threadProceed.countDown(); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
359 |
await(threadDone); |
35394 | 360 |
long startTime = System.nanoTime(); |
361 |
while (p.getCompletedTaskCount() != 1) { |
|
362 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
|
363 |
fail("timed out"); |
|
364 |
Thread.yield(); |
|
365 |
} |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
/** |
|
370 |
* getCorePoolSize returns size given in constructor if not otherwise set |
|
371 |
*/ |
|
372 |
public void testGetCorePoolSize() throws InterruptedException { |
|
373 |
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
374 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
375 |
assertEquals(1, p.getCorePoolSize()); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* getLargestPoolSize increases, but doesn't overestimate, when |
|
381 |
* multiple threads active |
|
382 |
*/ |
|
383 |
public void testGetLargestPoolSize() throws InterruptedException { |
|
384 |
final int THREADS = 3; |
|
385 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
|
386 |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
|
387 |
final CountDownLatch done = new CountDownLatch(1); |
|
388 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
389 |
assertEquals(0, p.getLargestPoolSize()); |
|
390 |
for (int i = 0; i < THREADS; i++) |
|
391 |
p.execute(new CheckedRunnable() { |
|
392 |
public void realRun() throws InterruptedException { |
|
393 |
threadsStarted.countDown(); |
|
394 |
await(done); |
|
395 |
assertEquals(THREADS, p.getLargestPoolSize()); |
|
396 |
}}); |
|
397 |
await(threadsStarted); |
|
398 |
assertEquals(THREADS, p.getLargestPoolSize()); |
|
399 |
} |
|
400 |
assertEquals(THREADS, p.getLargestPoolSize()); |
|
401 |
} |
|
402 |
||
403 |
/** |
|
404 |
* getPoolSize increases, but doesn't overestimate, when threads |
|
405 |
* become active |
|
406 |
*/ |
|
407 |
public void testGetPoolSize() throws InterruptedException { |
|
408 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
409 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
410 |
final CountDownLatch done = new CountDownLatch(1); |
|
411 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
412 |
assertEquals(0, p.getPoolSize()); |
|
413 |
p.execute(new CheckedRunnable() { |
|
414 |
public void realRun() throws InterruptedException { |
|
415 |
threadStarted.countDown(); |
|
416 |
assertEquals(1, p.getPoolSize()); |
|
417 |
await(done); |
|
418 |
}}); |
|
419 |
await(threadStarted); |
|
420 |
assertEquals(1, p.getPoolSize()); |
|
421 |
} |
|
422 |
} |
|
423 |
||
424 |
/** |
|
425 |
* getTaskCount increases, but doesn't overestimate, when tasks |
|
426 |
* submitted |
|
427 |
*/ |
|
428 |
public void testGetTaskCount() throws InterruptedException { |
|
429 |
final int TASKS = 3; |
|
430 |
final CountDownLatch done = new CountDownLatch(1); |
|
431 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
432 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
433 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
434 |
assertEquals(0, p.getTaskCount()); |
|
435 |
assertEquals(0, p.getCompletedTaskCount()); |
|
436 |
p.execute(new CheckedRunnable() { |
|
437 |
public void realRun() throws InterruptedException { |
|
438 |
threadStarted.countDown(); |
|
439 |
await(done); |
|
440 |
}}); |
|
441 |
await(threadStarted); |
|
442 |
assertEquals(1, p.getTaskCount()); |
|
443 |
assertEquals(0, p.getCompletedTaskCount()); |
|
444 |
for (int i = 0; i < TASKS; i++) { |
|
445 |
assertEquals(1 + i, p.getTaskCount()); |
|
446 |
p.execute(new CheckedRunnable() { |
|
447 |
public void realRun() throws InterruptedException { |
|
448 |
threadStarted.countDown(); |
|
449 |
assertEquals(1 + TASKS, p.getTaskCount()); |
|
450 |
await(done); |
|
451 |
}}); |
|
452 |
} |
|
453 |
assertEquals(1 + TASKS, p.getTaskCount()); |
|
454 |
assertEquals(0, p.getCompletedTaskCount()); |
|
455 |
} |
|
456 |
assertEquals(1 + TASKS, p.getTaskCount()); |
|
457 |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
|
458 |
} |
|
459 |
||
460 |
/** |
|
461 |
* getThreadFactory returns factory in constructor if not set |
|
462 |
*/ |
|
463 |
public void testGetThreadFactory() throws InterruptedException { |
|
464 |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
|
465 |
final ScheduledThreadPoolExecutor p = |
|
466 |
new ScheduledThreadPoolExecutor(1, threadFactory); |
|
467 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
468 |
assertSame(threadFactory, p.getThreadFactory()); |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
|
474 |
*/ |
|
475 |
public void testSetThreadFactory() throws InterruptedException { |
|
476 |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
|
477 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
478 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
479 |
p.setThreadFactory(threadFactory); |
|
480 |
assertSame(threadFactory, p.getThreadFactory()); |
|
481 |
} |
|
482 |
} |
|
483 |
||
484 |
/** |
|
485 |
* setThreadFactory(null) throws NPE |
|
486 |
*/ |
|
487 |
public void testSetThreadFactoryNull() throws InterruptedException { |
|
488 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
489 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
490 |
try { |
|
491 |
p.setThreadFactory(null); |
|
492 |
shouldThrow(); |
|
493 |
} catch (NullPointerException success) {} |
|
494 |
} |
|
495 |
} |
|
496 |
||
497 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
498 |
* The default rejected execution handler is AbortPolicy. |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
499 |
*/ |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
500 |
public void testDefaultRejectedExecutionHandler() { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
501 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
502 |
try (PoolCleaner cleaner = cleaner(p)) { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
503 |
assertTrue(p.getRejectedExecutionHandler() |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
504 |
instanceof ThreadPoolExecutor.AbortPolicy); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
505 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
506 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
507 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
508 |
/** |
35394 | 509 |
* isShutdown is false before shutdown, true after |
510 |
*/ |
|
511 |
public void testIsShutdown() { |
|
512 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
513 |
assertFalse(p.isShutdown()); |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
514 |
try (PoolCleaner cleaner = cleaner(p)) { |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
515 |
try { |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
516 |
p.shutdown(); |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
517 |
assertTrue(p.isShutdown()); |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
518 |
} catch (SecurityException ok) {} |
35394 | 519 |
} |
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* isTerminated is false before termination, true after |
|
524 |
*/ |
|
525 |
public void testIsTerminated() throws InterruptedException { |
|
526 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
527 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
528 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
529 |
final CountDownLatch done = new CountDownLatch(1); |
|
530 |
assertFalse(p.isTerminated()); |
|
531 |
p.execute(new CheckedRunnable() { |
|
532 |
public void realRun() throws InterruptedException { |
|
533 |
assertFalse(p.isTerminated()); |
|
534 |
threadStarted.countDown(); |
|
535 |
await(done); |
|
536 |
}}); |
|
537 |
await(threadStarted); |
|
538 |
assertFalse(p.isTerminating()); |
|
539 |
done.countDown(); |
|
540 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
|
541 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
542 |
assertTrue(p.isTerminated()); |
|
543 |
} |
|
544 |
} |
|
545 |
||
546 |
/** |
|
547 |
* isTerminating is not true when running or when terminated |
|
548 |
*/ |
|
549 |
public void testIsTerminating() throws InterruptedException { |
|
550 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
551 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
552 |
final CountDownLatch done = new CountDownLatch(1); |
|
553 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
554 |
assertFalse(p.isTerminating()); |
|
555 |
p.execute(new CheckedRunnable() { |
|
556 |
public void realRun() throws InterruptedException { |
|
557 |
assertFalse(p.isTerminating()); |
|
558 |
threadStarted.countDown(); |
|
559 |
await(done); |
|
560 |
}}); |
|
561 |
await(threadStarted); |
|
562 |
assertFalse(p.isTerminating()); |
|
563 |
done.countDown(); |
|
564 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
|
565 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
566 |
assertTrue(p.isTerminated()); |
|
567 |
assertFalse(p.isTerminating()); |
|
568 |
} |
|
569 |
} |
|
570 |
||
571 |
/** |
|
572 |
* getQueue returns the work queue, which contains queued tasks |
|
573 |
*/ |
|
574 |
public void testGetQueue() throws InterruptedException { |
|
575 |
final CountDownLatch done = new CountDownLatch(1); |
|
576 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
577 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
578 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
579 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
|
580 |
for (int i = 0; i < tasks.length; i++) { |
|
581 |
Runnable r = new CheckedRunnable() { |
|
582 |
public void realRun() throws InterruptedException { |
|
583 |
threadStarted.countDown(); |
|
584 |
await(done); |
|
585 |
}}; |
|
586 |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
|
587 |
} |
|
588 |
await(threadStarted); |
|
589 |
BlockingQueue<Runnable> q = p.getQueue(); |
|
590 |
assertTrue(q.contains(tasks[tasks.length - 1])); |
|
591 |
assertFalse(q.contains(tasks[0])); |
|
592 |
} |
|
593 |
} |
|
594 |
||
595 |
/** |
|
596 |
* remove(task) removes queued task, and fails to remove active task |
|
597 |
*/ |
|
598 |
public void testRemove() throws InterruptedException { |
|
599 |
final CountDownLatch done = new CountDownLatch(1); |
|
600 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
601 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
602 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
|
603 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
|
604 |
for (int i = 0; i < tasks.length; i++) { |
|
605 |
Runnable r = new CheckedRunnable() { |
|
606 |
public void realRun() throws InterruptedException { |
|
607 |
threadStarted.countDown(); |
|
608 |
await(done); |
|
609 |
}}; |
|
610 |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
|
611 |
} |
|
612 |
await(threadStarted); |
|
613 |
BlockingQueue<Runnable> q = p.getQueue(); |
|
614 |
assertFalse(p.remove((Runnable)tasks[0])); |
|
615 |
assertTrue(q.contains((Runnable)tasks[4])); |
|
616 |
assertTrue(q.contains((Runnable)tasks[3])); |
|
617 |
assertTrue(p.remove((Runnable)tasks[4])); |
|
618 |
assertFalse(p.remove((Runnable)tasks[4])); |
|
619 |
assertFalse(q.contains((Runnable)tasks[4])); |
|
620 |
assertTrue(q.contains((Runnable)tasks[3])); |
|
621 |
assertTrue(p.remove((Runnable)tasks[3])); |
|
622 |
assertFalse(q.contains((Runnable)tasks[3])); |
|
623 |
} |
|
624 |
} |
|
625 |
||
626 |
/** |
|
627 |
* purge eventually removes cancelled tasks from the queue |
|
628 |
*/ |
|
629 |
public void testPurge() throws InterruptedException { |
|
630 |
final ScheduledFuture[] tasks = new ScheduledFuture[5]; |
|
631 |
final Runnable releaser = new Runnable() { public void run() { |
|
632 |
for (ScheduledFuture task : tasks) |
|
633 |
if (task != null) task.cancel(true); }}; |
|
634 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
635 |
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
|
636 |
for (int i = 0; i < tasks.length; i++) |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
48541
diff
changeset
|
637 |
tasks[i] = p.schedule(possiblyInterruptedRunnable(SMALL_DELAY_MS), |
35394 | 638 |
LONG_DELAY_MS, MILLISECONDS); |
639 |
int max = tasks.length; |
|
640 |
if (tasks[4].cancel(true)) --max; |
|
641 |
if (tasks[3].cancel(true)) --max; |
|
642 |
// There must eventually be an interference-free point at |
|
643 |
// which purge will not fail. (At worst, when queue is empty.) |
|
644 |
long startTime = System.nanoTime(); |
|
645 |
do { |
|
646 |
p.purge(); |
|
647 |
long count = p.getTaskCount(); |
|
648 |
if (count == max) |
|
649 |
return; |
|
650 |
} while (millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
651 |
fail("Purge failed to remove cancelled tasks"); |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
/** |
|
656 |
* shutdownNow returns a list containing tasks that were not run, |
|
657 |
* and those tasks are drained from the queue |
|
658 |
*/ |
|
659 |
public void testShutdownNow() throws InterruptedException { |
|
660 |
final int poolSize = 2; |
|
661 |
final int count = 5; |
|
662 |
final AtomicInteger ran = new AtomicInteger(0); |
|
663 |
final ScheduledThreadPoolExecutor p = |
|
664 |
new ScheduledThreadPoolExecutor(poolSize); |
|
665 |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
|
666 |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
|
667 |
threadsStarted.countDown(); |
|
668 |
try { |
|
669 |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
|
670 |
} catch (InterruptedException success) {} |
|
671 |
ran.getAndIncrement(); |
|
672 |
}}; |
|
673 |
for (int i = 0; i < count; i++) |
|
674 |
p.execute(waiter); |
|
675 |
await(threadsStarted); |
|
676 |
assertEquals(poolSize, p.getActiveCount()); |
|
677 |
assertEquals(0, p.getCompletedTaskCount()); |
|
678 |
final List<Runnable> queuedTasks; |
|
679 |
try { |
|
680 |
queuedTasks = p.shutdownNow(); |
|
681 |
} catch (SecurityException ok) { |
|
682 |
return; // Allowed in case test doesn't have privs |
|
683 |
} |
|
684 |
assertTrue(p.isShutdown()); |
|
685 |
assertTrue(p.getQueue().isEmpty()); |
|
686 |
assertEquals(count - poolSize, queuedTasks.size()); |
|
687 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
688 |
assertTrue(p.isTerminated()); |
|
689 |
assertEquals(poolSize, ran.get()); |
|
690 |
assertEquals(poolSize, p.getCompletedTaskCount()); |
|
691 |
} |
|
692 |
||
693 |
/** |
|
694 |
* shutdownNow returns a list containing tasks that were not run, |
|
695 |
* and those tasks are drained from the queue |
|
696 |
*/ |
|
697 |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
|
698 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
699 |
List<ScheduledFuture> tasks = new ArrayList<>(); |
|
700 |
for (int i = 0; i < 3; i++) { |
|
701 |
Runnable r = new NoOpRunnable(); |
|
702 |
tasks.add(p.schedule(r, 9, SECONDS)); |
|
703 |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
|
704 |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
|
705 |
} |
|
706 |
if (testImplementationDetails) |
|
707 |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
|
708 |
final List<Runnable> queuedTasks; |
|
709 |
try { |
|
710 |
queuedTasks = p.shutdownNow(); |
|
711 |
} catch (SecurityException ok) { |
|
712 |
return; // Allowed in case test doesn't have privs |
|
713 |
} |
|
714 |
assertTrue(p.isShutdown()); |
|
715 |
assertTrue(p.getQueue().isEmpty()); |
|
716 |
if (testImplementationDetails) |
|
717 |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
|
718 |
assertEquals(tasks.size(), queuedTasks.size()); |
|
719 |
for (ScheduledFuture task : tasks) { |
|
720 |
assertFalse(task.isDone()); |
|
721 |
assertFalse(task.isCancelled()); |
|
722 |
} |
|
723 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
724 |
assertTrue(p.isTerminated()); |
|
725 |
} |
|
726 |
||
727 |
/** |
|
728 |
* By default, periodic tasks are cancelled at shutdown. |
|
729 |
* By default, delayed tasks keep running after shutdown. |
|
730 |
* Check that changing the default values work: |
|
731 |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
|
732 |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
|
733 |
*/ |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
734 |
@SuppressWarnings("FutureReturnValueIgnored") |
35394 | 735 |
public void testShutdown_cancellation() throws Exception { |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
736 |
final int poolSize = 4; |
35394 | 737 |
final ScheduledThreadPoolExecutor p |
738 |
= new ScheduledThreadPoolExecutor(poolSize); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
739 |
final BlockingQueue<Runnable> q = p.getQueue(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
740 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
741 |
final long delay = rnd.nextInt(2); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
742 |
final int rounds = rnd.nextInt(1, 3); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
743 |
final boolean effectiveDelayedPolicy; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
744 |
final boolean effectivePeriodicPolicy; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
745 |
final boolean effectiveRemovePolicy; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
746 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
747 |
if (rnd.nextBoolean()) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
748 |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
749 |
effectiveDelayedPolicy = rnd.nextBoolean()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
750 |
else |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
751 |
effectiveDelayedPolicy = true; |
35394 | 752 |
assertEquals(effectiveDelayedPolicy, |
753 |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
754 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
755 |
if (rnd.nextBoolean()) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
756 |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
757 |
effectivePeriodicPolicy = rnd.nextBoolean()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
758 |
else |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
759 |
effectivePeriodicPolicy = false; |
35394 | 760 |
assertEquals(effectivePeriodicPolicy, |
761 |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
762 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
763 |
if (rnd.nextBoolean()) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
764 |
p.setRemoveOnCancelPolicy( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
765 |
effectiveRemovePolicy = rnd.nextBoolean()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
766 |
else |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
767 |
effectiveRemovePolicy = false; |
35394 | 768 |
assertEquals(effectiveRemovePolicy, |
769 |
p.getRemoveOnCancelPolicy()); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
770 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
771 |
final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
772 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
773 |
// Strategy: Wedge the pool with one wave of "blocker" tasks, |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
774 |
// then add a second wave that waits in the queue until unblocked. |
35394 | 775 |
final AtomicInteger ran = new AtomicInteger(0); |
776 |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
|
777 |
final CountDownLatch unblock = new CountDownLatch(1); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
778 |
final RuntimeException exception = new RuntimeException(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
779 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
780 |
class Task implements Runnable { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
781 |
public void run() { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
782 |
try { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
783 |
ran.getAndIncrement(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
784 |
poolBlocked.countDown(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
785 |
await(unblock); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
786 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
787 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
788 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
789 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
790 |
class PeriodicTask extends Task { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
791 |
PeriodicTask(int rounds) { this.rounds = rounds; } |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
792 |
int rounds; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
793 |
public void run() { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
794 |
if (--rounds == 0) super.run(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
795 |
// throw exception to surely terminate this periodic task, |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
796 |
// but in a separate execution and in a detectable way. |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
797 |
if (rounds == -1) throw exception; |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
798 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
799 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
800 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
801 |
Runnable task = new Task(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
802 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
803 |
List<Future<?>> immediates = new ArrayList<>(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
804 |
List<Future<?>> delayeds = new ArrayList<>(); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
805 |
List<Future<?>> periodics = new ArrayList<>(); |
35394 | 806 |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
807 |
immediates.add(p.submit(task)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
808 |
delayeds.add(p.schedule(task, delay, MILLISECONDS)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
809 |
periodics.add(p.scheduleAtFixedRate( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
810 |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
811 |
periodics.add(p.scheduleWithFixedDelay( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
812 |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
813 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
814 |
await(poolBlocked); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
815 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
816 |
assertEquals(poolSize, ran.get()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
817 |
assertEquals(poolSize, p.getActiveCount()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
818 |
assertTrue(q.isEmpty()); |
35394 | 819 |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
820 |
// Add second wave of tasks. |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
821 |
immediates.add(p.submit(task)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
822 |
delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
823 |
periodics.add(p.scheduleAtFixedRate( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
824 |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
825 |
periodics.add(p.scheduleWithFixedDelay( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
826 |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
827 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
828 |
assertEquals(poolSize, q.size()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
829 |
assertEquals(poolSize, ran.get()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
830 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
831 |
immediates.forEach( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
832 |
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
833 |
|
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
834 |
Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream) |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
835 |
.forEach(f -> assertFalse(f.isDone())); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
836 |
|
35394 | 837 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
838 |
assertTrue(p.isShutdown()); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
839 |
assertTrue(p.isTerminating()); |
35394 | 840 |
assertFalse(p.isTerminated()); |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
841 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
842 |
if (rnd.nextBoolean()) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
843 |
assertThrows( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
844 |
RejectedExecutionException.class, |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
845 |
() -> p.submit(task), |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
846 |
() -> p.schedule(task, 1, SECONDS), |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
847 |
() -> p.scheduleAtFixedRate( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
848 |
new PeriodicTask(1), 1, 1, SECONDS), |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
849 |
() -> p.scheduleWithFixedDelay( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
850 |
new PeriodicTask(2), 1, 1, SECONDS)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
851 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
852 |
assertTrue(q.contains(immediates.get(1))); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
853 |
assertTrue(!effectiveDelayedPolicy |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
854 |
^ q.contains(delayeds.get(1))); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
855 |
assertTrue(!effectivePeriodicPolicy |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
856 |
^ q.containsAll(periodics.subList(2, 4))); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
857 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
858 |
immediates.forEach(f -> assertFalse(f.isDone())); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
859 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
860 |
assertFalse(delayeds.get(0).isDone()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
861 |
if (effectiveDelayedPolicy) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
862 |
assertFalse(delayeds.get(1).isDone()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
863 |
else |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
864 |
assertTrue(delayeds.get(1).isCancelled()); |
35394 | 865 |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
866 |
if (effectivePeriodicPolicy) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
867 |
periodics.forEach( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
868 |
f -> { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
869 |
assertFalse(f.isDone()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
870 |
if (!periodicTasksContinue) { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
871 |
assertTrue(f.cancel(false)); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
872 |
assertTrue(f.isCancelled()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
873 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
874 |
}); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
875 |
else { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
876 |
periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
877 |
periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); |
35394 | 878 |
} |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
879 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
880 |
unblock.countDown(); // Release all pool threads |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
881 |
|
35394 | 882 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
883 |
assertFalse(p.isTerminating()); |
35394 | 884 |
assertTrue(p.isTerminated()); |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
885 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
886 |
assertTrue(q.isEmpty()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
887 |
|
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
888 |
Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream) |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
889 |
.forEach(f -> assertTrue(f.isDone())); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
890 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
891 |
for (Future<?> f : immediates) assertNull(f.get()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
892 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
893 |
assertNull(delayeds.get(0).get()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
894 |
if (effectiveDelayedPolicy) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
895 |
assertNull(delayeds.get(1).get()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
896 |
else |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
897 |
assertTrue(delayeds.get(1).isCancelled()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
898 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
899 |
if (periodicTasksContinue) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
900 |
periodics.forEach( |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
901 |
f -> { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
902 |
try { f.get(); } |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
903 |
catch (ExecutionException success) { |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
904 |
assertSame(exception, success.getCause()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
905 |
} |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
906 |
catch (Throwable fail) { threadUnexpectedException(fail); } |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
907 |
}); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
908 |
else |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
909 |
periodics.forEach(f -> assertTrue(f.isCancelled())); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
910 |
|
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
911 |
assertEquals(poolSize + 1 |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
912 |
+ (effectiveDelayedPolicy ? 1 : 0) |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
913 |
+ (periodicTasksContinue ? 2 : 0), |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
914 |
ran.get()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
915 |
} |
35394 | 916 |
|
917 |
/** |
|
918 |
* completed submit of callable returns result |
|
919 |
*/ |
|
920 |
public void testSubmitCallable() throws Exception { |
|
921 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
922 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
923 |
Future<String> future = e.submit(new StringTask()); |
|
924 |
String result = future.get(); |
|
925 |
assertSame(TEST_STRING, result); |
|
926 |
} |
|
927 |
} |
|
928 |
||
929 |
/** |
|
930 |
* completed submit of runnable returns successfully |
|
931 |
*/ |
|
932 |
public void testSubmitRunnable() throws Exception { |
|
933 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
934 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
935 |
Future<?> future = e.submit(new NoOpRunnable()); |
|
936 |
future.get(); |
|
937 |
assertTrue(future.isDone()); |
|
938 |
} |
|
939 |
} |
|
940 |
||
941 |
/** |
|
942 |
* completed submit of (runnable, result) returns result |
|
943 |
*/ |
|
944 |
public void testSubmitRunnable2() throws Exception { |
|
945 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
946 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
947 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
|
948 |
String result = future.get(); |
|
949 |
assertSame(TEST_STRING, result); |
|
950 |
} |
|
951 |
} |
|
952 |
||
953 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
954 |
* invokeAny(null) throws NullPointerException |
35394 | 955 |
*/ |
956 |
public void testInvokeAny1() throws Exception { |
|
957 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
958 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
959 |
try { |
|
960 |
e.invokeAny(null); |
|
961 |
shouldThrow(); |
|
962 |
} catch (NullPointerException success) {} |
|
963 |
} |
|
964 |
} |
|
965 |
||
966 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
967 |
* invokeAny(empty collection) throws IllegalArgumentException |
35394 | 968 |
*/ |
969 |
public void testInvokeAny2() throws Exception { |
|
970 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
971 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
972 |
try { |
|
973 |
e.invokeAny(new ArrayList<Callable<String>>()); |
|
974 |
shouldThrow(); |
|
975 |
} catch (IllegalArgumentException success) {} |
|
976 |
} |
|
977 |
} |
|
978 |
||
979 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
980 |
* invokeAny(c) throws NullPointerException if c has null elements |
35394 | 981 |
*/ |
982 |
public void testInvokeAny3() throws Exception { |
|
983 |
CountDownLatch latch = new CountDownLatch(1); |
|
984 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
985 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
986 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 987 |
l.add(latchAwaitingStringTask(latch)); |
988 |
l.add(null); |
|
989 |
try { |
|
990 |
e.invokeAny(l); |
|
991 |
shouldThrow(); |
|
992 |
} catch (NullPointerException success) {} |
|
993 |
latch.countDown(); |
|
994 |
} |
|
995 |
} |
|
996 |
||
997 |
/** |
|
998 |
* invokeAny(c) throws ExecutionException if no task completes |
|
999 |
*/ |
|
1000 |
public void testInvokeAny4() throws Exception { |
|
1001 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1002 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1003 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1004 |
l.add(new NPETask()); |
1005 |
try { |
|
1006 |
e.invokeAny(l); |
|
1007 |
shouldThrow(); |
|
1008 |
} catch (ExecutionException success) { |
|
1009 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
1010 |
} |
|
1011 |
} |
|
1012 |
} |
|
1013 |
||
1014 |
/** |
|
1015 |
* invokeAny(c) returns result of some task |
|
1016 |
*/ |
|
1017 |
public void testInvokeAny5() throws Exception { |
|
1018 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1019 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1020 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1021 |
l.add(new StringTask()); |
1022 |
l.add(new StringTask()); |
|
1023 |
String result = e.invokeAny(l); |
|
1024 |
assertSame(TEST_STRING, result); |
|
1025 |
} |
|
1026 |
} |
|
1027 |
||
1028 |
/** |
|
1029 |
* invokeAll(null) throws NPE |
|
1030 |
*/ |
|
1031 |
public void testInvokeAll1() throws Exception { |
|
1032 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1033 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1034 |
try { |
|
1035 |
e.invokeAll(null); |
|
1036 |
shouldThrow(); |
|
1037 |
} catch (NullPointerException success) {} |
|
1038 |
} |
|
1039 |
} |
|
1040 |
||
1041 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1042 |
* invokeAll(empty collection) returns empty list |
35394 | 1043 |
*/ |
1044 |
public void testInvokeAll2() throws Exception { |
|
1045 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1046 |
final Collection<Callable<String>> emptyCollection |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1047 |
= Collections.emptyList(); |
35394 | 1048 |
try (PoolCleaner cleaner = cleaner(e)) { |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1049 |
List<Future<String>> r = e.invokeAll(emptyCollection); |
35394 | 1050 |
assertTrue(r.isEmpty()); |
1051 |
} |
|
1052 |
} |
|
1053 |
||
1054 |
/** |
|
1055 |
* invokeAll(c) throws NPE if c has null elements |
|
1056 |
*/ |
|
1057 |
public void testInvokeAll3() throws Exception { |
|
1058 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1059 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1060 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1061 |
l.add(new StringTask()); |
1062 |
l.add(null); |
|
1063 |
try { |
|
1064 |
e.invokeAll(l); |
|
1065 |
shouldThrow(); |
|
1066 |
} catch (NullPointerException success) {} |
|
1067 |
} |
|
1068 |
} |
|
1069 |
||
1070 |
/** |
|
1071 |
* get of invokeAll(c) throws exception on failed task |
|
1072 |
*/ |
|
1073 |
public void testInvokeAll4() throws Exception { |
|
1074 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1075 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1076 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1077 |
l.add(new NPETask()); |
1078 |
List<Future<String>> futures = e.invokeAll(l); |
|
1079 |
assertEquals(1, futures.size()); |
|
1080 |
try { |
|
1081 |
futures.get(0).get(); |
|
1082 |
shouldThrow(); |
|
1083 |
} catch (ExecutionException success) { |
|
1084 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
1085 |
} |
|
1086 |
} |
|
1087 |
} |
|
1088 |
||
1089 |
/** |
|
1090 |
* invokeAll(c) returns results of all completed tasks |
|
1091 |
*/ |
|
1092 |
public void testInvokeAll5() throws Exception { |
|
1093 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1094 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1095 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1096 |
l.add(new StringTask()); |
1097 |
l.add(new StringTask()); |
|
1098 |
List<Future<String>> futures = e.invokeAll(l); |
|
1099 |
assertEquals(2, futures.size()); |
|
1100 |
for (Future<String> future : futures) |
|
1101 |
assertSame(TEST_STRING, future.get()); |
|
1102 |
} |
|
1103 |
} |
|
1104 |
||
1105 |
/** |
|
1106 |
* timed invokeAny(null) throws NPE |
|
1107 |
*/ |
|
1108 |
public void testTimedInvokeAny1() throws Exception { |
|
1109 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1110 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1111 |
try { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1112 |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
35394 | 1113 |
shouldThrow(); |
1114 |
} catch (NullPointerException success) {} |
|
1115 |
} |
|
1116 |
} |
|
1117 |
||
1118 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1119 |
* timed invokeAny(,,null) throws NullPointerException |
35394 | 1120 |
*/ |
1121 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
|
1122 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1123 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1124 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1125 |
l.add(new StringTask()); |
1126 |
try { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1127 |
e.invokeAny(l, randomTimeout(), null); |
35394 | 1128 |
shouldThrow(); |
1129 |
} catch (NullPointerException success) {} |
|
1130 |
} |
|
1131 |
} |
|
1132 |
||
1133 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1134 |
* timed invokeAny(empty collection) throws IllegalArgumentException |
35394 | 1135 |
*/ |
1136 |
public void testTimedInvokeAny2() throws Exception { |
|
1137 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1138 |
final Collection<Callable<String>> emptyCollection |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1139 |
= Collections.emptyList(); |
35394 | 1140 |
try (PoolCleaner cleaner = cleaner(e)) { |
1141 |
try { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1142 |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
35394 | 1143 |
shouldThrow(); |
1144 |
} catch (IllegalArgumentException success) {} |
|
1145 |
} |
|
1146 |
} |
|
1147 |
||
1148 |
/** |
|
1149 |
* timed invokeAny(c) throws NPE if c has null elements |
|
1150 |
*/ |
|
1151 |
public void testTimedInvokeAny3() throws Exception { |
|
1152 |
CountDownLatch latch = new CountDownLatch(1); |
|
1153 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1154 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1155 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1156 |
l.add(latchAwaitingStringTask(latch)); |
1157 |
l.add(null); |
|
1158 |
try { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1159 |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
35394 | 1160 |
shouldThrow(); |
1161 |
} catch (NullPointerException success) {} |
|
1162 |
latch.countDown(); |
|
1163 |
} |
|
1164 |
} |
|
1165 |
||
1166 |
/** |
|
1167 |
* timed invokeAny(c) throws ExecutionException if no task completes |
|
1168 |
*/ |
|
1169 |
public void testTimedInvokeAny4() throws Exception { |
|
1170 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1171 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1172 |
long startTime = System.nanoTime(); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1173 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1174 |
l.add(new NPETask()); |
1175 |
try { |
|
1176 |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
|
1177 |
shouldThrow(); |
|
1178 |
} catch (ExecutionException success) { |
|
1179 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
1180 |
} |
|
1181 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
1182 |
} |
|
1183 |
} |
|
1184 |
||
1185 |
/** |
|
1186 |
* timed invokeAny(c) returns result of some task |
|
1187 |
*/ |
|
1188 |
public void testTimedInvokeAny5() throws Exception { |
|
1189 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1190 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1191 |
long startTime = System.nanoTime(); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1192 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1193 |
l.add(new StringTask()); |
1194 |
l.add(new StringTask()); |
|
1195 |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
|
1196 |
assertSame(TEST_STRING, result); |
|
1197 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
1198 |
} |
|
1199 |
} |
|
1200 |
||
1201 |
/** |
|
1202 |
* timed invokeAll(null) throws NPE |
|
1203 |
*/ |
|
1204 |
public void testTimedInvokeAll1() throws Exception { |
|
1205 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1206 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
1207 |
try { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1208 |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
35394 | 1209 |
shouldThrow(); |
1210 |
} catch (NullPointerException success) {} |
|
1211 |
} |
|
1212 |
} |
|
1213 |
||
1214 |
/** |
|
1215 |
* timed invokeAll(,,null) throws NPE |
|
1216 |
*/ |
|
1217 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
|
1218 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1219 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1220 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1221 |
l.add(new StringTask()); |
1222 |
try { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1223 |
e.invokeAll(l, randomTimeout(), null); |
35394 | 1224 |
shouldThrow(); |
1225 |
} catch (NullPointerException success) {} |
|
1226 |
} |
|
1227 |
} |
|
1228 |
||
1229 |
/** |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1230 |
* timed invokeAll(empty collection) returns empty list |
35394 | 1231 |
*/ |
1232 |
public void testTimedInvokeAll2() throws Exception { |
|
1233 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1234 |
final Collection<Callable<String>> emptyCollection |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1235 |
= Collections.emptyList(); |
35394 | 1236 |
try (PoolCleaner cleaner = cleaner(e)) { |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1237 |
List<Future<String>> r = |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1238 |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
35394 | 1239 |
assertTrue(r.isEmpty()); |
1240 |
} |
|
1241 |
} |
|
1242 |
||
1243 |
/** |
|
1244 |
* timed invokeAll(c) throws NPE if c has null elements |
|
1245 |
*/ |
|
1246 |
public void testTimedInvokeAll3() throws Exception { |
|
1247 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1248 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1249 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1250 |
l.add(new StringTask()); |
1251 |
l.add(null); |
|
1252 |
try { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1253 |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
35394 | 1254 |
shouldThrow(); |
1255 |
} catch (NullPointerException success) {} |
|
1256 |
} |
|
1257 |
} |
|
1258 |
||
1259 |
/** |
|
1260 |
* get of element of invokeAll(c) throws exception on failed task |
|
1261 |
*/ |
|
1262 |
public void testTimedInvokeAll4() throws Exception { |
|
1263 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1264 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1265 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1266 |
l.add(new NPETask()); |
1267 |
List<Future<String>> futures = |
|
1268 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
|
1269 |
assertEquals(1, futures.size()); |
|
1270 |
try { |
|
1271 |
futures.get(0).get(); |
|
1272 |
shouldThrow(); |
|
1273 |
} catch (ExecutionException success) { |
|
1274 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
1275 |
} |
|
1276 |
} |
|
1277 |
} |
|
1278 |
||
1279 |
/** |
|
1280 |
* timed invokeAll(c) returns results of all completed tasks |
|
1281 |
*/ |
|
1282 |
public void testTimedInvokeAll5() throws Exception { |
|
1283 |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
|
1284 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
1285 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 1286 |
l.add(new StringTask()); |
1287 |
l.add(new StringTask()); |
|
1288 |
List<Future<String>> futures = |
|
1289 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
|
1290 |
assertEquals(2, futures.size()); |
|
1291 |
for (Future<String> future : futures) |
|
1292 |
assertSame(TEST_STRING, future.get()); |
|
1293 |
} |
|
1294 |
} |
|
1295 |
||
1296 |
/** |
|
1297 |
* timed invokeAll(c) cancels tasks not completed by timeout |
|
1298 |
*/ |
|
1299 |
public void testTimedInvokeAll6() throws Exception { |
|
1300 |
for (long timeout = timeoutMillis();;) { |
|
1301 |
final CountDownLatch done = new CountDownLatch(1); |
|
1302 |
final Callable<String> waiter = new CheckedCallable<String>() { |
|
1303 |
public String realCall() { |
|
1304 |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
|
1305 |
catch (InterruptedException ok) {} |
|
1306 |
return "1"; }}; |
|
1307 |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
|
1308 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
|
1309 |
List<Callable<String>> tasks = new ArrayList<>(); |
|
1310 |
tasks.add(new StringTask("0")); |
|
1311 |
tasks.add(waiter); |
|
1312 |
tasks.add(new StringTask("2")); |
|
1313 |
long startTime = System.nanoTime(); |
|
1314 |
List<Future<String>> futures = |
|
1315 |
p.invokeAll(tasks, timeout, MILLISECONDS); |
|
1316 |
assertEquals(tasks.size(), futures.size()); |
|
1317 |
assertTrue(millisElapsedSince(startTime) >= timeout); |
|
1318 |
for (Future future : futures) |
|
1319 |
assertTrue(future.isDone()); |
|
1320 |
assertTrue(futures.get(1).isCancelled()); |
|
1321 |
try { |
|
1322 |
assertEquals("0", futures.get(0).get()); |
|
1323 |
assertEquals("2", futures.get(2).get()); |
|
1324 |
break; |
|
1325 |
} catch (CancellationException retryWithLongerTimeout) { |
|
1326 |
timeout *= 2; |
|
1327 |
if (timeout >= LONG_DELAY_MS / 2) |
|
1328 |
fail("expected exactly one task to be cancelled"); |
|
1329 |
} |
|
1330 |
} |
|
1331 |
} |
|
1332 |
} |
|
1333 |
||
1334 |
/** |
|
1335 |
* A fixed delay task with overflowing period should not prevent a |
|
1336 |
* one-shot task from executing. |
|
1337 |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
|
1338 |
*/ |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1339 |
@SuppressWarnings("FutureReturnValueIgnored") |
35394 | 1340 |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1341 |
final CountDownLatch delayedDone = new CountDownLatch(1); |
|
1342 |
final CountDownLatch immediateDone = new CountDownLatch(1); |
|
1343 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
|
1344 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1345 |
final Runnable delayed = () -> { |
35394 | 1346 |
delayedDone.countDown(); |
45936
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1347 |
p.submit(() -> immediateDone.countDown()); |
d564ef4bed8f
8177632: ScheduledThreadPoolExecutor delayed task shutdown policy affects non-scheduled tasks
dl
parents:
43522
diff
changeset
|
1348 |
}; |
35394 | 1349 |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1350 |
await(delayedDone); |
|
1351 |
await(immediateDone); |
|
1352 |
} |
|
1353 |
} |
|
1354 |
||
1355 |
} |