11 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
|
13 |
|
import java.util.ArrayList; |
14 |
+ |
import java.util.Collection; |
15 |
+ |
import java.util.Collections; |
16 |
|
import java.util.List; |
17 |
|
import java.util.concurrent.ArrayBlockingQueue; |
18 |
|
import java.util.concurrent.BlockingQueue; |
20 |
|
import java.util.concurrent.CancellationException; |
21 |
|
import java.util.concurrent.CountDownLatch; |
22 |
|
import java.util.concurrent.ExecutionException; |
21 |
– |
import java.util.concurrent.Executors; |
23 |
|
import java.util.concurrent.ExecutorService; |
24 |
|
import java.util.concurrent.Future; |
25 |
|
import java.util.concurrent.FutureTask; |
29 |
|
import java.util.concurrent.SynchronousQueue; |
30 |
|
import java.util.concurrent.ThreadFactory; |
31 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
– |
import java.util.concurrent.TimeUnit; |
32 |
|
import java.util.concurrent.atomic.AtomicInteger; |
33 |
|
|
34 |
|
import junit.framework.Test; |
92 |
|
final Runnable task = new CheckedRunnable() { |
93 |
|
public void realRun() { done.countDown(); }}; |
94 |
|
p.execute(task); |
95 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
95 |
> |
await(done); |
96 |
|
} |
97 |
|
} |
98 |
|
|
101 |
|
* thread becomes active |
102 |
|
*/ |
103 |
|
public void testGetActiveCount() throws InterruptedException { |
104 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
105 |
|
final ThreadPoolExecutor p = |
106 |
|
new ThreadPoolExecutor(2, 2, |
107 |
|
LONG_DELAY_MS, MILLISECONDS, |
108 |
|
new ArrayBlockingQueue<Runnable>(10)); |
109 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
109 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
110 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
– |
final CountDownLatch done = new CountDownLatch(1); |
111 |
|
assertEquals(0, p.getActiveCount()); |
112 |
|
p.execute(new CheckedRunnable() { |
113 |
|
public void realRun() throws InterruptedException { |
114 |
|
threadStarted.countDown(); |
115 |
|
assertEquals(1, p.getActiveCount()); |
116 |
< |
done.await(); |
116 |
> |
await(done); |
117 |
|
}}); |
118 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
118 |
> |
await(threadStarted); |
119 |
|
assertEquals(1, p.getActiveCount()); |
120 |
– |
done.countDown(); |
120 |
|
} |
121 |
|
} |
122 |
|
|
186 |
|
public void realRun() throws InterruptedException { |
187 |
|
threadStarted.countDown(); |
188 |
|
assertEquals(0, p.getCompletedTaskCount()); |
189 |
< |
threadProceed.await(); |
189 |
> |
await(threadProceed); |
190 |
|
threadDone.countDown(); |
191 |
|
}}); |
192 |
|
await(threadStarted); |
193 |
|
assertEquals(0, p.getCompletedTaskCount()); |
194 |
|
threadProceed.countDown(); |
195 |
< |
threadDone.await(); |
195 |
> |
await(threadDone); |
196 |
|
long startTime = System.nanoTime(); |
197 |
|
while (p.getCompletedTaskCount() != 1) { |
198 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
276 |
|
} |
277 |
|
|
278 |
|
/** |
279 |
+ |
* The default rejected execution handler is AbortPolicy. |
280 |
+ |
*/ |
281 |
+ |
public void testDefaultRejectedExecutionHandler() { |
282 |
+ |
final ThreadPoolExecutor p = |
283 |
+ |
new ThreadPoolExecutor(1, 2, |
284 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
285 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
286 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
287 |
+ |
assertTrue(p.getRejectedExecutionHandler() |
288 |
+ |
instanceof ThreadPoolExecutor.AbortPolicy); |
289 |
+ |
} |
290 |
+ |
} |
291 |
+ |
|
292 |
+ |
/** |
293 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
294 |
|
*/ |
295 |
|
public void testGetRejectedExecutionHandler() { |
342 |
|
*/ |
343 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
344 |
|
final int THREADS = 3; |
345 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
346 |
|
final ThreadPoolExecutor p = |
347 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
348 |
|
LONG_DELAY_MS, MILLISECONDS, |
349 |
|
new ArrayBlockingQueue<Runnable>(10)); |
350 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
338 |
< |
final CountDownLatch done = new CountDownLatch(1); |
350 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
351 |
|
assertEquals(0, p.getLargestPoolSize()); |
352 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
353 |
|
for (int i = 0; i < THREADS; i++) |
354 |
|
p.execute(new CheckedRunnable() { |
355 |
|
public void realRun() throws InterruptedException { |
356 |
|
threadsStarted.countDown(); |
357 |
< |
done.await(); |
357 |
> |
await(done); |
358 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
359 |
|
}}); |
360 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
360 |
> |
await(threadsStarted); |
361 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
349 |
– |
done.countDown(); // release pool |
362 |
|
} |
363 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
364 |
|
} |
386 |
|
* become active |
387 |
|
*/ |
388 |
|
public void testGetPoolSize() throws InterruptedException { |
389 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
390 |
|
final ThreadPoolExecutor p = |
391 |
|
new ThreadPoolExecutor(1, 1, |
392 |
|
LONG_DELAY_MS, MILLISECONDS, |
393 |
|
new ArrayBlockingQueue<Runnable>(10)); |
394 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
382 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
383 |
< |
final CountDownLatch done = new CountDownLatch(1); |
394 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
395 |
|
assertEquals(0, p.getPoolSize()); |
396 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
397 |
|
p.execute(new CheckedRunnable() { |
398 |
|
public void realRun() throws InterruptedException { |
399 |
|
threadStarted.countDown(); |
400 |
|
assertEquals(1, p.getPoolSize()); |
401 |
< |
done.await(); |
401 |
> |
await(done); |
402 |
|
}}); |
403 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
403 |
> |
await(threadStarted); |
404 |
|
assertEquals(1, p.getPoolSize()); |
393 |
– |
done.countDown(); // release pool |
405 |
|
} |
406 |
|
} |
407 |
|
|
422 |
|
p.execute(new CheckedRunnable() { |
423 |
|
public void realRun() throws InterruptedException { |
424 |
|
threadStarted.countDown(); |
425 |
< |
done.await(); |
425 |
> |
await(done); |
426 |
|
}}); |
427 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
427 |
> |
await(threadStarted); |
428 |
|
assertEquals(1, p.getTaskCount()); |
429 |
|
assertEquals(0, p.getCompletedTaskCount()); |
430 |
|
for (int i = 0; i < TASKS; i++) { |
433 |
|
public void realRun() throws InterruptedException { |
434 |
|
threadStarted.countDown(); |
435 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
436 |
< |
done.await(); |
436 |
> |
await(done); |
437 |
|
}}); |
438 |
|
} |
439 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
472 |
|
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
473 |
|
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
474 |
|
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
475 |
< |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
476 |
< |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
475 |
> |
assertFalse(p.awaitTermination(randomExpiredTimeout(), |
476 |
> |
randomTimeUnit())); |
477 |
|
long timeoutNanos = 999999L; |
478 |
|
long startTime = System.nanoTime(); |
479 |
|
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
506 |
|
public void realRun() throws InterruptedException { |
507 |
|
assertFalse(p.isTerminating()); |
508 |
|
threadStarted.countDown(); |
509 |
< |
done.await(); |
509 |
> |
await(done); |
510 |
|
}}); |
511 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
511 |
> |
await(threadStarted); |
512 |
|
assertFalse(p.isTerminating()); |
513 |
|
done.countDown(); |
514 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
534 |
|
public void realRun() throws InterruptedException { |
535 |
|
assertFalse(p.isTerminating()); |
536 |
|
threadStarted.countDown(); |
537 |
< |
done.await(); |
537 |
> |
await(done); |
538 |
|
}}); |
539 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
539 |
> |
await(threadStarted); |
540 |
|
assertFalse(p.isTerminating()); |
541 |
|
done.countDown(); |
542 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
550 |
|
* getQueue returns the work queue, which contains queued tasks |
551 |
|
*/ |
552 |
|
public void testGetQueue() throws InterruptedException { |
553 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
553 |
> |
final CountDownLatch done = new CountDownLatch(1); |
554 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
555 |
|
final ThreadPoolExecutor p = |
556 |
|
new ThreadPoolExecutor(1, 1, |
557 |
|
LONG_DELAY_MS, MILLISECONDS, |
558 |
|
q); |
559 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
559 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
560 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
549 |
– |
final CountDownLatch done = new CountDownLatch(1); |
561 |
|
FutureTask[] tasks = new FutureTask[5]; |
562 |
|
for (int i = 0; i < tasks.length; i++) { |
563 |
|
Callable task = new CheckedCallable<Boolean>() { |
564 |
|
public Boolean realCall() throws InterruptedException { |
565 |
|
threadStarted.countDown(); |
566 |
|
assertSame(q, p.getQueue()); |
567 |
< |
done.await(); |
567 |
> |
await(done); |
568 |
|
return Boolean.TRUE; |
569 |
|
}}; |
570 |
|
tasks[i] = new FutureTask(task); |
571 |
|
p.execute(tasks[i]); |
572 |
|
} |
573 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
573 |
> |
await(threadStarted); |
574 |
|
assertSame(q, p.getQueue()); |
575 |
|
assertFalse(q.contains(tasks[0])); |
576 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
577 |
|
assertEquals(tasks.length - 1, q.size()); |
567 |
– |
done.countDown(); |
578 |
|
} |
579 |
|
} |
580 |
|
|
582 |
|
* remove(task) removes queued task, and fails to remove active task |
583 |
|
*/ |
584 |
|
public void testRemove() throws InterruptedException { |
585 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
585 |
> |
final CountDownLatch done = new CountDownLatch(1); |
586 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
587 |
|
final ThreadPoolExecutor p = |
588 |
|
new ThreadPoolExecutor(1, 1, |
589 |
|
LONG_DELAY_MS, MILLISECONDS, |
590 |
|
q); |
591 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
591 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
592 |
|
Runnable[] tasks = new Runnable[6]; |
593 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
583 |
– |
final CountDownLatch done = new CountDownLatch(1); |
594 |
|
for (int i = 0; i < tasks.length; i++) { |
595 |
|
tasks[i] = new CheckedRunnable() { |
596 |
|
public void realRun() throws InterruptedException { |
597 |
|
threadStarted.countDown(); |
598 |
< |
done.await(); |
598 |
> |
await(done); |
599 |
|
}}; |
600 |
|
p.execute(tasks[i]); |
601 |
|
} |
602 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
602 |
> |
await(threadStarted); |
603 |
|
assertFalse(p.remove(tasks[0])); |
604 |
|
assertTrue(q.contains(tasks[4])); |
605 |
|
assertTrue(q.contains(tasks[3])); |
609 |
|
assertTrue(q.contains(tasks[3])); |
610 |
|
assertTrue(p.remove(tasks[3])); |
611 |
|
assertFalse(q.contains(tasks[3])); |
602 |
– |
done.countDown(); |
612 |
|
} |
613 |
|
} |
614 |
|
|
618 |
|
public void testPurge() throws InterruptedException { |
619 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
620 |
|
final CountDownLatch done = new CountDownLatch(1); |
621 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
621 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
622 |
|
final ThreadPoolExecutor p = |
623 |
|
new ThreadPoolExecutor(1, 1, |
624 |
|
LONG_DELAY_MS, MILLISECONDS, |
629 |
|
Callable task = new CheckedCallable<Boolean>() { |
630 |
|
public Boolean realCall() throws InterruptedException { |
631 |
|
threadStarted.countDown(); |
632 |
< |
done.await(); |
632 |
> |
await(done); |
633 |
|
return Boolean.TRUE; |
634 |
|
}}; |
635 |
|
tasks[i] = new FutureTask(task); |
636 |
|
p.execute(tasks[i]); |
637 |
|
} |
638 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
638 |
> |
await(threadStarted); |
639 |
|
assertEquals(tasks.length, p.getTaskCount()); |
640 |
|
assertEquals(tasks.length - 1, q.size()); |
641 |
|
assertEquals(1L, p.getActiveCount()); |
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; |
1035 |
|
* get of submitted callable throws InterruptedException if interrupted |
1036 |
|
*/ |
1037 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1038 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1039 |
|
final ThreadPoolExecutor p = |
1040 |
|
new ThreadPoolExecutor(1, 1, |
1041 |
|
60, SECONDS, |
1042 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1043 |
|
|
1044 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1044 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1045 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1036 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1046 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1047 |
|
public void realRun() throws Exception { |
1048 |
|
Callable task = new CheckedCallable<Boolean>() { |
1049 |
|
public Boolean realCall() throws InterruptedException { |
1050 |
|
threadStarted.countDown(); |
1051 |
< |
done.await(); |
1051 |
> |
await(done); |
1052 |
|
return Boolean.TRUE; |
1053 |
|
}}; |
1054 |
|
p.submit(task).get(); |
1055 |
|
}}); |
1056 |
|
|
1057 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1057 |
> |
await(threadStarted); |
1058 |
|
t.interrupt(); |
1059 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1051 |
< |
done.countDown(); |
1059 |
> |
awaitTermination(t); |
1060 |
|
} |
1061 |
|
} |
1062 |
|
|
1064 |
|
* execute throws RejectedExecutionException if saturated. |
1065 |
|
*/ |
1066 |
|
public void testSaturatedExecute() { |
1067 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1068 |
|
final ThreadPoolExecutor p = |
1069 |
|
new ThreadPoolExecutor(1, 1, |
1070 |
|
LONG_DELAY_MS, MILLISECONDS, |
1071 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1072 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1064 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1072 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1073 |
|
Runnable task = new CheckedRunnable() { |
1074 |
|
public void realRun() throws InterruptedException { |
1075 |
< |
done.await(); |
1075 |
> |
await(done); |
1076 |
|
}}; |
1077 |
|
for (int i = 0; i < 2; ++i) |
1078 |
|
p.execute(task); |
1083 |
|
} catch (RejectedExecutionException success) {} |
1084 |
|
assertTrue(p.getTaskCount() <= 2); |
1085 |
|
} |
1078 |
– |
done.countDown(); |
1086 |
|
} |
1087 |
|
} |
1088 |
|
|
1090 |
|
* submit(runnable) throws RejectedExecutionException if saturated. |
1091 |
|
*/ |
1092 |
|
public void testSaturatedSubmitRunnable() { |
1093 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1094 |
|
final ThreadPoolExecutor p = |
1095 |
|
new ThreadPoolExecutor(1, 1, |
1096 |
|
LONG_DELAY_MS, MILLISECONDS, |
1097 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1098 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1091 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1098 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1099 |
|
Runnable task = new CheckedRunnable() { |
1100 |
|
public void realRun() throws InterruptedException { |
1101 |
< |
done.await(); |
1101 |
> |
await(done); |
1102 |
|
}}; |
1103 |
|
for (int i = 0; i < 2; ++i) |
1104 |
|
p.submit(task); |
1109 |
|
} catch (RejectedExecutionException success) {} |
1110 |
|
assertTrue(p.getTaskCount() <= 2); |
1111 |
|
} |
1105 |
– |
done.countDown(); |
1112 |
|
} |
1113 |
|
} |
1114 |
|
|
1116 |
|
* submit(callable) throws RejectedExecutionException if saturated. |
1117 |
|
*/ |
1118 |
|
public void testSaturatedSubmitCallable() { |
1119 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1120 |
|
final ThreadPoolExecutor p = |
1121 |
|
new ThreadPoolExecutor(1, 1, |
1122 |
|
LONG_DELAY_MS, MILLISECONDS, |
1123 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1124 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1118 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1124 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1125 |
|
Runnable task = new CheckedRunnable() { |
1126 |
|
public void realRun() throws InterruptedException { |
1127 |
< |
done.await(); |
1127 |
> |
await(done); |
1128 |
|
}}; |
1129 |
|
for (int i = 0; i < 2; ++i) |
1130 |
< |
p.submit(Executors.callable(task)); |
1130 |
> |
p.execute(task); |
1131 |
|
for (int i = 0; i < 2; ++i) { |
1132 |
|
try { |
1133 |
|
p.execute(task); |
1135 |
|
} catch (RejectedExecutionException success) {} |
1136 |
|
assertTrue(p.getTaskCount() <= 2); |
1137 |
|
} |
1132 |
– |
done.countDown(); |
1138 |
|
} |
1139 |
|
} |
1140 |
|
|
1152 |
|
final CountDownLatch done = new CountDownLatch(1); |
1153 |
|
Runnable blocker = new CheckedRunnable() { |
1154 |
|
public void realRun() throws InterruptedException { |
1155 |
< |
done.await(); |
1155 |
> |
await(done); |
1156 |
|
}}; |
1157 |
|
p.execute(blocker); |
1158 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1171 |
|
* executor using DiscardPolicy drops task if saturated. |
1172 |
|
*/ |
1173 |
|
public void testSaturatedExecute3() { |
1174 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1175 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1176 |
|
for (int i = 0; i < tasks.length; ++i) |
1177 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1180 |
|
LONG_DELAY_MS, MILLISECONDS, |
1181 |
|
new ArrayBlockingQueue<Runnable>(1), |
1182 |
|
new ThreadPoolExecutor.DiscardPolicy()); |
1183 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1178 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1183 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1184 |
|
p.execute(awaiter(done)); |
1185 |
|
|
1186 |
|
for (TrackedNoOpRunnable task : tasks) |
1187 |
|
p.execute(task); |
1188 |
|
for (int i = 1; i < tasks.length; i++) |
1189 |
|
assertFalse(tasks[i].done); |
1185 |
– |
done.countDown(); |
1190 |
|
} |
1191 |
|
for (int i = 1; i < tasks.length; i++) |
1192 |
|
assertFalse(tasks[i].done); |
1206 |
|
LONG_DELAY_MS, MILLISECONDS, |
1207 |
|
new ArrayBlockingQueue<Runnable>(1), |
1208 |
|
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1209 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1209 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1210 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1211 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1212 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1216 |
|
p.execute(r3); |
1217 |
|
assertFalse(p.getQueue().contains(r2)); |
1218 |
|
assertTrue(p.getQueue().contains(r3)); |
1215 |
– |
done.countDown(); |
1219 |
|
} |
1220 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1221 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1497 |
|
} |
1498 |
|
|
1499 |
|
/** |
1500 |
< |
* invokeAny(empty collection) throws IAE |
1500 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
1501 |
|
*/ |
1502 |
|
public void testInvokeAny2() throws Exception { |
1503 |
|
final ExecutorService e = |
1522 |
|
LONG_DELAY_MS, MILLISECONDS, |
1523 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1524 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1525 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1525 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1526 |
|
l.add(latchAwaitingStringTask(latch)); |
1527 |
|
l.add(null); |
1528 |
|
try { |
1542 |
|
LONG_DELAY_MS, MILLISECONDS, |
1543 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1544 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1545 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1545 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1546 |
|
l.add(new NPETask()); |
1547 |
|
try { |
1548 |
|
e.invokeAny(l); |
1562 |
|
LONG_DELAY_MS, MILLISECONDS, |
1563 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1564 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1565 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1565 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1566 |
|
l.add(new StringTask()); |
1567 |
|
l.add(new StringTask()); |
1568 |
|
String result = e.invokeAny(l); |
1587 |
|
} |
1588 |
|
|
1589 |
|
/** |
1590 |
< |
* invokeAll(empty collection) returns empty collection |
1590 |
> |
* invokeAll(empty collection) returns empty list |
1591 |
|
*/ |
1592 |
|
public void testInvokeAll2() throws InterruptedException { |
1593 |
|
final ExecutorService e = |
1594 |
|
new ThreadPoolExecutor(2, 2, |
1595 |
|
LONG_DELAY_MS, MILLISECONDS, |
1596 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1597 |
+ |
final Collection<Callable<String>> emptyCollection |
1598 |
+ |
= Collections.emptyList(); |
1599 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1600 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1600 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1601 |
|
assertTrue(r.isEmpty()); |
1602 |
|
} |
1603 |
|
} |
1611 |
|
LONG_DELAY_MS, MILLISECONDS, |
1612 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1613 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1614 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1614 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1615 |
|
l.add(new StringTask()); |
1616 |
|
l.add(null); |
1617 |
|
try { |
1630 |
|
LONG_DELAY_MS, MILLISECONDS, |
1631 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1632 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1633 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1633 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1634 |
|
l.add(new NPETask()); |
1635 |
|
List<Future<String>> futures = e.invokeAll(l); |
1636 |
|
assertEquals(1, futures.size()); |
1652 |
|
LONG_DELAY_MS, MILLISECONDS, |
1653 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1654 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1655 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1655 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1656 |
|
l.add(new StringTask()); |
1657 |
|
l.add(new StringTask()); |
1658 |
|
List<Future<String>> futures = e.invokeAll(l); |
1672 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1673 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1674 |
|
try { |
1675 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1675 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1676 |
|
shouldThrow(); |
1677 |
|
} catch (NullPointerException success) {} |
1678 |
|
} |
1687 |
|
LONG_DELAY_MS, MILLISECONDS, |
1688 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1689 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1690 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1690 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1691 |
|
l.add(new StringTask()); |
1692 |
|
try { |
1693 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1693 |
> |
e.invokeAny(l, randomTimeout(), null); |
1694 |
|
shouldThrow(); |
1695 |
|
} catch (NullPointerException success) {} |
1696 |
|
} |
1697 |
|
} |
1698 |
|
|
1699 |
|
/** |
1700 |
< |
* timed invokeAny(empty collection) throws IAE |
1700 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1701 |
|
*/ |
1702 |
|
public void testTimedInvokeAny2() throws Exception { |
1703 |
|
final ExecutorService e = |
1707 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1708 |
|
try { |
1709 |
|
e.invokeAny(new ArrayList<Callable<String>>(), |
1710 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1710 |
> |
randomTimeout(), randomTimeUnit()); |
1711 |
|
shouldThrow(); |
1712 |
|
} catch (IllegalArgumentException success) {} |
1713 |
|
} |
1714 |
|
} |
1715 |
|
|
1716 |
|
/** |
1717 |
< |
* timed invokeAny(c) throws NPE if c has null elements |
1717 |
> |
* timed invokeAny(c) throws NullPointerException if c has null elements |
1718 |
|
*/ |
1719 |
|
public void testTimedInvokeAny3() throws Exception { |
1720 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1723 |
|
LONG_DELAY_MS, MILLISECONDS, |
1724 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1725 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1726 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1726 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1727 |
|
l.add(latchAwaitingStringTask(latch)); |
1728 |
|
l.add(null); |
1729 |
|
try { |
1730 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1730 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
1731 |
|
shouldThrow(); |
1732 |
|
} catch (NullPointerException success) {} |
1733 |
|
latch.countDown(); |
1743 |
|
LONG_DELAY_MS, MILLISECONDS, |
1744 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1745 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1746 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1746 |
> |
long startTime = System.nanoTime(); |
1747 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1748 |
|
l.add(new NPETask()); |
1749 |
|
try { |
1750 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1750 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1751 |
|
shouldThrow(); |
1752 |
|
} catch (ExecutionException success) { |
1753 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1754 |
|
} |
1755 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1756 |
|
} |
1757 |
|
} |
1758 |
|
|
1765 |
|
LONG_DELAY_MS, MILLISECONDS, |
1766 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1767 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1768 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1768 |
> |
long startTime = System.nanoTime(); |
1769 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1770 |
|
l.add(new StringTask()); |
1771 |
|
l.add(new StringTask()); |
1772 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1772 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1773 |
|
assertSame(TEST_STRING, result); |
1774 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1775 |
|
} |
1776 |
|
} |
1777 |
|
|
1785 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1786 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1787 |
|
try { |
1788 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1788 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1789 |
|
shouldThrow(); |
1790 |
|
} catch (NullPointerException success) {} |
1791 |
|
} |
1800 |
|
LONG_DELAY_MS, MILLISECONDS, |
1801 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1802 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1803 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1803 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1804 |
|
l.add(new StringTask()); |
1805 |
|
try { |
1806 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1806 |
> |
e.invokeAll(l, randomTimeout(), null); |
1807 |
|
shouldThrow(); |
1808 |
|
} catch (NullPointerException success) {} |
1809 |
|
} |
1810 |
|
} |
1811 |
|
|
1812 |
|
/** |
1813 |
< |
* timed invokeAll(empty collection) returns empty collection |
1813 |
> |
* timed invokeAll(empty collection) returns empty list |
1814 |
|
*/ |
1815 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1816 |
|
final ExecutorService e = |
1817 |
|
new ThreadPoolExecutor(2, 2, |
1818 |
|
LONG_DELAY_MS, MILLISECONDS, |
1819 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1820 |
+ |
final Collection<Callable<String>> emptyCollection |
1821 |
+ |
= Collections.emptyList(); |
1822 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1823 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1824 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1823 |
> |
List<Future<String>> r = |
1824 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1825 |
|
assertTrue(r.isEmpty()); |
1826 |
|
} |
1827 |
|
} |
1835 |
|
LONG_DELAY_MS, MILLISECONDS, |
1836 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1837 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1838 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1838 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1839 |
|
l.add(new StringTask()); |
1840 |
|
l.add(null); |
1841 |
|
try { |
1842 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1842 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1843 |
|
shouldThrow(); |
1844 |
|
} catch (NullPointerException success) {} |
1845 |
|
} |
1854 |
|
LONG_DELAY_MS, MILLISECONDS, |
1855 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1856 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1857 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1857 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1858 |
|
l.add(new NPETask()); |
1859 |
|
List<Future<String>> futures = |
1860 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1860 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1861 |
|
assertEquals(1, futures.size()); |
1862 |
|
try { |
1863 |
|
futures.get(0).get(); |
1877 |
|
LONG_DELAY_MS, MILLISECONDS, |
1878 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1879 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1880 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1880 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1881 |
|
l.add(new StringTask()); |
1882 |
|
l.add(new StringTask()); |
1883 |
|
List<Future<String>> futures = |
1892 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1893 |
|
*/ |
1894 |
|
public void testTimedInvokeAll6() throws Exception { |
1895 |
< |
final ExecutorService e = |
1896 |
< |
new ThreadPoolExecutor(2, 2, |
1897 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1898 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1899 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1900 |
< |
for (long timeout = timeoutMillis();;) { |
1895 |
> |
for (long timeout = timeoutMillis();;) { |
1896 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1897 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1898 |
> |
public String realCall() { |
1899 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1900 |
> |
catch (InterruptedException ok) {} |
1901 |
> |
return "1"; }}; |
1902 |
> |
final ExecutorService p = |
1903 |
> |
new ThreadPoolExecutor(2, 2, |
1904 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1905 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1906 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1907 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1908 |
|
tasks.add(new StringTask("0")); |
1909 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1909 |
> |
tasks.add(waiter); |
1910 |
|
tasks.add(new StringTask("2")); |
1911 |
|
long startTime = System.nanoTime(); |
1912 |
|
List<Future<String>> futures = |
1913 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1913 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1914 |
|
assertEquals(tasks.size(), futures.size()); |
1915 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1916 |
|
for (Future future : futures) |
1947 |
|
public void realRun() { |
1948 |
|
done.countDown(); |
1949 |
|
}}); |
1950 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1950 |
> |
await(done); |
1951 |
|
} |
1952 |
|
} |
1953 |
|
|
2040 |
|
} |
2041 |
|
} |
2042 |
|
// enough time to run all tasks |
2043 |
< |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2043 |
> |
await(done, nTasks * SHORT_DELAY_MS); |
2044 |
|
} |
2045 |
|
} |
2046 |
|
|
2048 |
|
* get(cancelled task) throws CancellationException |
2049 |
|
*/ |
2050 |
|
public void testGet_cancelled() throws Exception { |
2051 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2052 |
|
final ExecutorService e = |
2053 |
|
new ThreadPoolExecutor(1, 1, |
2054 |
|
LONG_DELAY_MS, MILLISECONDS, |
2055 |
|
new LinkedBlockingQueue<Runnable>()); |
2056 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
2056 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2057 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2040 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2058 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2059 |
|
for (int i = 0; i < 2; i++) { |
2060 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2064 |
|
}}; |
2065 |
|
futures.add(e.submit(r)); |
2066 |
|
} |
2067 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2067 |
> |
await(blockerStarted); |
2068 |
|
for (Future<?> future : futures) future.cancel(false); |
2069 |
|
for (Future<?> future : futures) { |
2070 |
|
try { |
2078 |
|
assertTrue(future.isCancelled()); |
2079 |
|
assertTrue(future.isDone()); |
2080 |
|
} |
2064 |
– |
done.countDown(); |
2081 |
|
} |
2082 |
|
} |
2083 |
|
|
2084 |
+ |
public void testAbortPolicy() { |
2085 |
+ |
final RejectedExecutionHandler handler = |
2086 |
+ |
new ThreadPoolExecutor.AbortPolicy(); |
2087 |
+ |
final ThreadPoolExecutor p = |
2088 |
+ |
new ThreadPoolExecutor(1, 1, |
2089 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
2090 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
2091 |
+ |
final TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
2092 |
+ |
try { |
2093 |
+ |
handler.rejectedExecution(r, p); |
2094 |
+ |
shouldThrow(); |
2095 |
+ |
} catch (RejectedExecutionException success) {} |
2096 |
+ |
assertFalse(r.done); |
2097 |
+ |
assertEquals(0, p.getTaskCount()); |
2098 |
+ |
assertTrue(p.getQueue().isEmpty()); |
2099 |
+ |
} |
2100 |
+ |
|
2101 |
|
} |