7 |
|
*/ |
8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
|
13 |
|
import java.util.ArrayList; |
18 |
|
import java.util.concurrent.CancellationException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.ExecutionException; |
20 |
– |
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
|
import java.util.concurrent.Future; |
23 |
|
import java.util.concurrent.RejectedExecutionException; |
25 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
26 |
|
import java.util.concurrent.ThreadFactory; |
27 |
|
import java.util.concurrent.ThreadPoolExecutor; |
28 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
29 |
|
import java.util.concurrent.atomic.AtomicInteger; |
30 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
31 |
|
|
32 |
|
import junit.framework.Test; |
33 |
|
import junit.framework.TestSuite; |
44 |
|
* execute successfully executes a runnable |
45 |
|
*/ |
46 |
|
public void testExecute() throws InterruptedException { |
47 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
47 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
48 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
49 |
|
final CountDownLatch done = new CountDownLatch(1); |
50 |
|
final Runnable task = new CheckedRunnable() { |
51 |
|
public void realRun() { done.countDown(); }}; |
52 |
|
p.execute(task); |
53 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
53 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
54 |
|
} |
55 |
|
} |
56 |
|
|
58 |
|
* delayed schedule of callable successfully executes after delay |
59 |
|
*/ |
60 |
|
public void testSchedule1() throws Exception { |
61 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
61 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
62 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
63 |
|
final long startTime = System.nanoTime(); |
64 |
|
final CountDownLatch done = new CountDownLatch(1); |
79 |
|
* delayed schedule of runnable successfully executes after delay |
80 |
|
*/ |
81 |
|
public void testSchedule3() throws Exception { |
82 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
82 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
83 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
84 |
|
final long startTime = System.nanoTime(); |
85 |
|
final CountDownLatch done = new CountDownLatch(1); |
99 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
100 |
|
*/ |
101 |
|
public void testSchedule4() throws Exception { |
102 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
102 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
103 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
104 |
|
final long startTime = System.nanoTime(); |
105 |
|
final CountDownLatch done = new CountDownLatch(1); |
121 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
122 |
|
*/ |
123 |
|
public void testSchedule5() throws Exception { |
124 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
124 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
125 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
126 |
|
final long startTime = System.nanoTime(); |
127 |
|
final CountDownLatch done = new CountDownLatch(1); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
148 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
149 |
> |
* Eventually, it must hold that: |
150 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
151 |
|
*/ |
152 |
|
public void testFixedRateSequence() throws InterruptedException { |
153 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
153 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
154 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
155 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
156 |
< |
long startTime = System.nanoTime(); |
157 |
< |
int cycles = 10; |
156 |
> |
final long startTime = System.nanoTime(); |
157 |
> |
final int cycles = 8; |
158 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
159 |
< |
Runnable task = new CheckedRunnable() { |
159 |
> |
final Runnable task = new CheckedRunnable() { |
160 |
|
public void realRun() { done.countDown(); }}; |
161 |
< |
ScheduledFuture h = |
161 |
> |
final ScheduledFuture periodicTask = |
162 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
163 |
< |
done.await(); |
164 |
< |
h.cancel(true); |
165 |
< |
double normalizedTime = |
166 |
< |
(double) millisElapsedSince(startTime) / delay; |
167 |
< |
if (normalizedTime >= cycles - 1 && |
168 |
< |
normalizedTime <= cycles) |
163 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
164 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
165 |
> |
periodicTask.cancel(true); |
166 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
167 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
168 |
> |
if (elapsedMillis <= cycles * delay) |
169 |
|
return; |
170 |
+ |
// else retry with longer delay |
171 |
|
} |
172 |
< |
throw new AssertionError("unexpected execution rate"); |
172 |
> |
fail("unexpected execution rate"); |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
177 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
178 |
> |
* Eventually, it must hold that each task starts at least delay and at |
179 |
> |
* most 2 * delay after the termination of the previous task. |
180 |
|
*/ |
181 |
|
public void testFixedDelaySequence() throws InterruptedException { |
182 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
182 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
183 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
184 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
185 |
< |
long startTime = System.nanoTime(); |
186 |
< |
int cycles = 10; |
185 |
> |
final long startTime = System.nanoTime(); |
186 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
187 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
188 |
> |
final int cycles = 8; |
189 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
190 |
< |
Runnable task = new CheckedRunnable() { |
191 |
< |
public void realRun() { done.countDown(); }}; |
192 |
< |
ScheduledFuture h = |
190 |
> |
final int d = delay; |
191 |
> |
final Runnable task = new CheckedRunnable() { |
192 |
> |
public void realRun() { |
193 |
> |
long now = System.nanoTime(); |
194 |
> |
long elapsedMillis |
195 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
196 |
> |
if (done.getCount() == cycles) { // first execution |
197 |
> |
if (elapsedMillis >= d) |
198 |
> |
tryLongerDelay.set(true); |
199 |
> |
} else { |
200 |
> |
assertTrue(elapsedMillis >= d); |
201 |
> |
if (elapsedMillis >= 2 * d) |
202 |
> |
tryLongerDelay.set(true); |
203 |
> |
} |
204 |
> |
previous.set(now); |
205 |
> |
done.countDown(); |
206 |
> |
}}; |
207 |
> |
final ScheduledFuture periodicTask = |
208 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
209 |
< |
done.await(); |
210 |
< |
h.cancel(true); |
211 |
< |
double normalizedTime = |
212 |
< |
(double) millisElapsedSince(startTime) / delay; |
213 |
< |
if (normalizedTime >= cycles - 1 && |
214 |
< |
normalizedTime <= cycles) |
209 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
210 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
211 |
> |
periodicTask.cancel(true); |
212 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
213 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
214 |
> |
if (!tryLongerDelay.get()) |
215 |
|
return; |
216 |
+ |
// else retry with longer delay |
217 |
|
} |
218 |
< |
throw new AssertionError("unexpected execution rate"); |
218 |
> |
fail("unexpected execution rate"); |
219 |
|
} |
220 |
|
} |
221 |
|
|
223 |
|
* execute(null) throws NPE |
224 |
|
*/ |
225 |
|
public void testExecuteNull() throws InterruptedException { |
226 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
226 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
227 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
228 |
|
try { |
229 |
|
p.execute(null); |
250 |
|
* execute throws RejectedExecutionException if shutdown |
251 |
|
*/ |
252 |
|
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
253 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
253 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
254 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
255 |
|
try { |
256 |
|
p.shutdown(); |
266 |
|
* schedule throws RejectedExecutionException if shutdown |
267 |
|
*/ |
268 |
|
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
269 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
269 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
270 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
271 |
|
try { |
272 |
|
p.shutdown(); |
282 |
|
* schedule callable throws RejectedExecutionException if shutdown |
283 |
|
*/ |
284 |
|
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
285 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
285 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
286 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
287 |
|
try { |
288 |
|
p.shutdown(); |
298 |
|
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
299 |
|
*/ |
300 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
301 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
301 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
302 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
303 |
|
try { |
304 |
|
p.shutdown(); |
314 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
315 |
|
*/ |
316 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
317 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
317 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
318 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
319 |
|
try { |
320 |
|
p.shutdown(); |
331 |
|
* thread becomes active |
332 |
|
*/ |
333 |
|
public void testGetActiveCount() throws InterruptedException { |
334 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
335 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
336 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
336 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
337 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
312 |
– |
final CountDownLatch done = new CountDownLatch(1); |
338 |
|
assertEquals(0, p.getActiveCount()); |
339 |
|
p.execute(new CheckedRunnable() { |
340 |
|
public void realRun() throws InterruptedException { |
341 |
|
threadStarted.countDown(); |
342 |
|
assertEquals(1, p.getActiveCount()); |
343 |
< |
done.await(); |
343 |
> |
await(done); |
344 |
|
}}); |
345 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
345 |
> |
await(threadStarted); |
346 |
|
assertEquals(1, p.getActiveCount()); |
322 |
– |
done.countDown(); |
347 |
|
} |
348 |
|
} |
349 |
|
|
397 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
398 |
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
399 |
|
final CountDownLatch done = new CountDownLatch(1); |
400 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
400 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
401 |
|
assertEquals(0, p.getLargestPoolSize()); |
402 |
|
for (int i = 0; i < THREADS; i++) |
403 |
|
p.execute(new CheckedRunnable() { |
404 |
|
public void realRun() throws InterruptedException { |
405 |
|
threadsStarted.countDown(); |
406 |
< |
done.await(); |
406 |
> |
await(done); |
407 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
408 |
|
}}); |
409 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
409 |
> |
await(threadsStarted); |
410 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
387 |
– |
done.countDown(); |
411 |
|
} |
412 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
413 |
|
} |
420 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
421 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
422 |
|
final CountDownLatch done = new CountDownLatch(1); |
423 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
423 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
424 |
|
assertEquals(0, p.getPoolSize()); |
425 |
|
p.execute(new CheckedRunnable() { |
426 |
|
public void realRun() throws InterruptedException { |
427 |
|
threadStarted.countDown(); |
428 |
|
assertEquals(1, p.getPoolSize()); |
429 |
< |
done.await(); |
429 |
> |
await(done); |
430 |
|
}}); |
431 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
431 |
> |
await(threadStarted); |
432 |
|
assertEquals(1, p.getPoolSize()); |
410 |
– |
done.countDown(); |
433 |
|
} |
434 |
|
} |
435 |
|
|
448 |
|
p.execute(new CheckedRunnable() { |
449 |
|
public void realRun() throws InterruptedException { |
450 |
|
threadStarted.countDown(); |
451 |
< |
done.await(); |
451 |
> |
await(done); |
452 |
|
}}); |
453 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
453 |
> |
await(threadStarted); |
454 |
|
assertEquals(1, p.getTaskCount()); |
455 |
|
assertEquals(0, p.getCompletedTaskCount()); |
456 |
|
for (int i = 0; i < TASKS; i++) { |
459 |
|
public void realRun() throws InterruptedException { |
460 |
|
threadStarted.countDown(); |
461 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
462 |
< |
done.await(); |
462 |
> |
await(done); |
463 |
|
}}); |
464 |
|
} |
465 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
486 |
|
*/ |
487 |
|
public void testSetThreadFactory() throws InterruptedException { |
488 |
|
ThreadFactory threadFactory = new SimpleThreadFactory(); |
489 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
489 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
490 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
491 |
|
p.setThreadFactory(threadFactory); |
492 |
|
assertSame(threadFactory, p.getThreadFactory()); |
497 |
|
* setThreadFactory(null) throws NPE |
498 |
|
*/ |
499 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
500 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
500 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
501 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
502 |
|
try { |
503 |
|
p.setThreadFactory(null); |
510 |
|
* isShutdown is false before shutdown, true after |
511 |
|
*/ |
512 |
|
public void testIsShutdown() { |
513 |
< |
|
514 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
515 |
< |
try { |
516 |
< |
assertFalse(p.isShutdown()); |
517 |
< |
} |
518 |
< |
finally { |
519 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
513 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
514 |
> |
assertFalse(p.isShutdown()); |
515 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
516 |
> |
try { |
517 |
> |
p.shutdown(); |
518 |
> |
assertTrue(p.isShutdown()); |
519 |
> |
} catch (SecurityException ok) {} |
520 |
|
} |
499 |
– |
assertTrue(p.isShutdown()); |
521 |
|
} |
522 |
|
|
523 |
|
/** |
533 |
|
public void realRun() throws InterruptedException { |
534 |
|
assertFalse(p.isTerminated()); |
535 |
|
threadStarted.countDown(); |
536 |
< |
done.await(); |
536 |
> |
await(done); |
537 |
|
}}); |
538 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
538 |
> |
await(threadStarted); |
539 |
|
assertFalse(p.isTerminating()); |
540 |
|
done.countDown(); |
541 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
557 |
|
public void realRun() throws InterruptedException { |
558 |
|
assertFalse(p.isTerminating()); |
559 |
|
threadStarted.countDown(); |
560 |
< |
done.await(); |
560 |
> |
await(done); |
561 |
|
}}); |
562 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
562 |
> |
await(threadStarted); |
563 |
|
assertFalse(p.isTerminating()); |
564 |
|
done.countDown(); |
565 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
573 |
|
* getQueue returns the work queue, which contains queued tasks |
574 |
|
*/ |
575 |
|
public void testGetQueue() throws InterruptedException { |
576 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
577 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
576 |
> |
final CountDownLatch done = new CountDownLatch(1); |
577 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
578 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
579 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
558 |
– |
final CountDownLatch done = new CountDownLatch(1); |
580 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
581 |
|
for (int i = 0; i < tasks.length; i++) { |
582 |
|
Runnable r = new CheckedRunnable() { |
583 |
|
public void realRun() throws InterruptedException { |
584 |
|
threadStarted.countDown(); |
585 |
< |
done.await(); |
585 |
> |
await(done); |
586 |
|
}}; |
587 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
588 |
|
} |
589 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
589 |
> |
await(threadStarted); |
590 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
591 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
592 |
|
assertFalse(q.contains(tasks[0])); |
572 |
– |
done.countDown(); |
593 |
|
} |
594 |
|
} |
595 |
|
|
597 |
|
* remove(task) removes queued task, and fails to remove active task |
598 |
|
*/ |
599 |
|
public void testRemove() throws InterruptedException { |
600 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
601 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
602 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
602 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
603 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
604 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
584 |
– |
final CountDownLatch done = new CountDownLatch(1); |
605 |
|
for (int i = 0; i < tasks.length; i++) { |
606 |
|
Runnable r = new CheckedRunnable() { |
607 |
|
public void realRun() throws InterruptedException { |
608 |
|
threadStarted.countDown(); |
609 |
< |
done.await(); |
609 |
> |
await(done); |
610 |
|
}}; |
611 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
612 |
|
} |
613 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
613 |
> |
await(threadStarted); |
614 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
615 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
616 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
621 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
622 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
623 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
604 |
– |
done.countDown(); |
624 |
|
} |
625 |
|
} |
626 |
|
|
673 |
|
}}; |
674 |
|
for (int i = 0; i < count; i++) |
675 |
|
p.execute(waiter); |
676 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
676 |
> |
await(threadsStarted); |
677 |
|
assertEquals(poolSize, p.getActiveCount()); |
678 |
|
assertEquals(0, p.getCompletedTaskCount()); |
679 |
|
final List<Runnable> queuedTasks; |
696 |
|
* and those tasks are drained from the queue |
697 |
|
*/ |
698 |
|
public void testShutdownNow_delayedTasks() throws InterruptedException { |
699 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
699 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
700 |
|
List<ScheduledFuture> tasks = new ArrayList<>(); |
701 |
|
for (int i = 0; i < 3; i++) { |
702 |
|
Runnable r = new NoOpRunnable(); |
814 |
|
assertTrue(periodic.isDone()); |
815 |
|
} |
816 |
|
} |
817 |
+ |
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
818 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
819 |
|
assertTrue(p.isTerminated()); |
820 |
|
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
889 |
|
CountDownLatch latch = new CountDownLatch(1); |
890 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
891 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
892 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
892 |
> |
List<Callable<String>> l = new ArrayList<>(); |
893 |
|
l.add(latchAwaitingStringTask(latch)); |
894 |
|
l.add(null); |
895 |
|
try { |
906 |
|
public void testInvokeAny4() throws Exception { |
907 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
908 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
909 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
909 |
> |
List<Callable<String>> l = new ArrayList<>(); |
910 |
|
l.add(new NPETask()); |
911 |
|
try { |
912 |
|
e.invokeAny(l); |
923 |
|
public void testInvokeAny5() throws Exception { |
924 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
925 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
926 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
926 |
> |
List<Callable<String>> l = new ArrayList<>(); |
927 |
|
l.add(new StringTask()); |
928 |
|
l.add(new StringTask()); |
929 |
|
String result = e.invokeAny(l); |
961 |
|
public void testInvokeAll3() throws Exception { |
962 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
963 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
964 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
964 |
> |
List<Callable<String>> l = new ArrayList<>(); |
965 |
|
l.add(new StringTask()); |
966 |
|
l.add(null); |
967 |
|
try { |
977 |
|
public void testInvokeAll4() throws Exception { |
978 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
979 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
980 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
980 |
> |
List<Callable<String>> l = new ArrayList<>(); |
981 |
|
l.add(new NPETask()); |
982 |
|
List<Future<String>> futures = e.invokeAll(l); |
983 |
|
assertEquals(1, futures.size()); |
996 |
|
public void testInvokeAll5() throws Exception { |
997 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
998 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
999 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
999 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1000 |
|
l.add(new StringTask()); |
1001 |
|
l.add(new StringTask()); |
1002 |
|
List<Future<String>> futures = e.invokeAll(l); |
1025 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1026 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1027 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1028 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1028 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1029 |
|
l.add(new StringTask()); |
1030 |
|
try { |
1031 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1054 |
|
CountDownLatch latch = new CountDownLatch(1); |
1055 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1056 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1057 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1057 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1058 |
|
l.add(latchAwaitingStringTask(latch)); |
1059 |
|
l.add(null); |
1060 |
|
try { |
1071 |
|
public void testTimedInvokeAny4() throws Exception { |
1072 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1073 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1074 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1074 |
> |
long startTime = System.nanoTime(); |
1075 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1076 |
|
l.add(new NPETask()); |
1077 |
|
try { |
1078 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1078 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1079 |
|
shouldThrow(); |
1080 |
|
} catch (ExecutionException success) { |
1081 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1082 |
|
} |
1083 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1084 |
|
} |
1085 |
|
} |
1086 |
|
|
1090 |
|
public void testTimedInvokeAny5() throws Exception { |
1091 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1092 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1093 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1093 |
> |
long startTime = System.nanoTime(); |
1094 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1095 |
|
l.add(new StringTask()); |
1096 |
|
l.add(new StringTask()); |
1097 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1097 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1098 |
|
assertSame(TEST_STRING, result); |
1099 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1100 |
|
} |
1101 |
|
} |
1102 |
|
|
1119 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1120 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1121 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1122 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1122 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1123 |
|
l.add(new StringTask()); |
1124 |
|
try { |
1125 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1146 |
|
public void testTimedInvokeAll3() throws Exception { |
1147 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1148 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1149 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1149 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1150 |
|
l.add(new StringTask()); |
1151 |
|
l.add(null); |
1152 |
|
try { |
1162 |
|
public void testTimedInvokeAll4() throws Exception { |
1163 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1164 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1165 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1165 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1166 |
|
l.add(new NPETask()); |
1167 |
|
List<Future<String>> futures = |
1168 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1168 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1169 |
|
assertEquals(1, futures.size()); |
1170 |
|
try { |
1171 |
|
futures.get(0).get(); |
1182 |
|
public void testTimedInvokeAll5() throws Exception { |
1183 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1184 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1185 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1185 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1186 |
|
l.add(new StringTask()); |
1187 |
|
l.add(new StringTask()); |
1188 |
|
List<Future<String>> futures = |
1197 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1198 |
|
*/ |
1199 |
|
public void testTimedInvokeAll6() throws Exception { |
1200 |
< |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1201 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1202 |
< |
for (long timeout = timeoutMillis();;) { |
1200 |
> |
for (long timeout = timeoutMillis();;) { |
1201 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1202 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1203 |
> |
public String realCall() { |
1204 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1205 |
> |
catch (InterruptedException ok) {} |
1206 |
> |
return "1"; }}; |
1207 |
> |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
1208 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1209 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1210 |
|
tasks.add(new StringTask("0")); |
1211 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1211 |
> |
tasks.add(waiter); |
1212 |
|
tasks.add(new StringTask("2")); |
1213 |
|
long startTime = System.nanoTime(); |
1214 |
|
List<Future<String>> futures = |
1215 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1215 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1216 |
|
assertEquals(tasks.size(), futures.size()); |
1217 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1218 |
|
for (Future future : futures) |
1231 |
|
} |
1232 |
|
} |
1233 |
|
|
1234 |
+ |
/** |
1235 |
+ |
* A fixed delay task with overflowing period should not prevent a |
1236 |
+ |
* one-shot task from executing. |
1237 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1238 |
+ |
*/ |
1239 |
+ |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1240 |
+ |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1241 |
+ |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1242 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1243 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1244 |
+ |
final Runnable immediate = new Runnable() { public void run() { |
1245 |
+ |
immediateDone.countDown(); |
1246 |
+ |
}}; |
1247 |
+ |
final Runnable delayed = new Runnable() { public void run() { |
1248 |
+ |
delayedDone.countDown(); |
1249 |
+ |
p.submit(immediate); |
1250 |
+ |
}}; |
1251 |
+ |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1252 |
+ |
await(delayedDone); |
1253 |
+ |
await(immediateDone); |
1254 |
+ |
} |
1255 |
+ |
} |
1256 |
+ |
|
1257 |
|
} |