18 |
|
import java.util.concurrent.CancellationException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.ExecutionException; |
21 |
– |
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
|
import java.util.concurrent.Future; |
23 |
|
import java.util.concurrent.FutureTask; |
27 |
|
import java.util.concurrent.SynchronousQueue; |
28 |
|
import java.util.concurrent.ThreadFactory; |
29 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
– |
import java.util.concurrent.TimeUnit; |
30 |
|
import java.util.concurrent.atomic.AtomicInteger; |
31 |
|
|
32 |
|
import junit.framework.Test; |
90 |
|
final Runnable task = new CheckedRunnable() { |
91 |
|
public void realRun() { done.countDown(); }}; |
92 |
|
p.execute(task); |
93 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
93 |
> |
await(done); |
94 |
|
} |
95 |
|
} |
96 |
|
|
99 |
|
* thread becomes active |
100 |
|
*/ |
101 |
|
public void testGetActiveCount() throws InterruptedException { |
102 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
103 |
|
final ThreadPoolExecutor p = |
104 |
|
new ThreadPoolExecutor(2, 2, |
105 |
|
LONG_DELAY_MS, MILLISECONDS, |
106 |
|
new ArrayBlockingQueue<Runnable>(10)); |
107 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
107 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
108 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
– |
final CountDownLatch done = new CountDownLatch(1); |
109 |
|
assertEquals(0, p.getActiveCount()); |
110 |
|
p.execute(new CheckedRunnable() { |
111 |
|
public void realRun() throws InterruptedException { |
112 |
|
threadStarted.countDown(); |
113 |
|
assertEquals(1, p.getActiveCount()); |
114 |
< |
done.await(); |
114 |
> |
await(done); |
115 |
|
}}); |
116 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
116 |
> |
await(threadStarted); |
117 |
|
assertEquals(1, p.getActiveCount()); |
120 |
– |
done.countDown(); |
118 |
|
} |
119 |
|
} |
120 |
|
|
184 |
|
public void realRun() throws InterruptedException { |
185 |
|
threadStarted.countDown(); |
186 |
|
assertEquals(0, p.getCompletedTaskCount()); |
187 |
< |
threadProceed.await(); |
187 |
> |
await(threadProceed); |
188 |
|
threadDone.countDown(); |
189 |
|
}}); |
190 |
|
await(threadStarted); |
191 |
|
assertEquals(0, p.getCompletedTaskCount()); |
192 |
|
threadProceed.countDown(); |
193 |
< |
threadDone.await(); |
193 |
> |
await(threadDone); |
194 |
|
long startTime = System.nanoTime(); |
195 |
|
while (p.getCompletedTaskCount() != 1) { |
196 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
+ |
* The default rejected execution handler is AbortPolicy. |
278 |
+ |
*/ |
279 |
+ |
public void testDefaultRejectedExecutionHandler() { |
280 |
+ |
final ThreadPoolExecutor p = |
281 |
+ |
new ThreadPoolExecutor(1, 2, |
282 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
283 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
284 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
285 |
+ |
assertTrue(p.getRejectedExecutionHandler() |
286 |
+ |
instanceof ThreadPoolExecutor.AbortPolicy); |
287 |
+ |
} |
288 |
+ |
} |
289 |
+ |
|
290 |
+ |
/** |
291 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
292 |
|
*/ |
293 |
|
public void testGetRejectedExecutionHandler() { |
352 |
|
p.execute(new CheckedRunnable() { |
353 |
|
public void realRun() throws InterruptedException { |
354 |
|
threadsStarted.countDown(); |
355 |
< |
done.await(); |
355 |
> |
await(done); |
356 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
357 |
|
}}); |
358 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
358 |
> |
await(threadsStarted); |
359 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
360 |
|
} |
361 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
396 |
|
public void realRun() throws InterruptedException { |
397 |
|
threadStarted.countDown(); |
398 |
|
assertEquals(1, p.getPoolSize()); |
399 |
< |
done.await(); |
399 |
> |
await(done); |
400 |
|
}}); |
401 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
401 |
> |
await(threadStarted); |
402 |
|
assertEquals(1, p.getPoolSize()); |
403 |
|
} |
404 |
|
} |
420 |
|
p.execute(new CheckedRunnable() { |
421 |
|
public void realRun() throws InterruptedException { |
422 |
|
threadStarted.countDown(); |
423 |
< |
done.await(); |
423 |
> |
await(done); |
424 |
|
}}); |
425 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
425 |
> |
await(threadStarted); |
426 |
|
assertEquals(1, p.getTaskCount()); |
427 |
|
assertEquals(0, p.getCompletedTaskCount()); |
428 |
|
for (int i = 0; i < TASKS; i++) { |
431 |
|
public void realRun() throws InterruptedException { |
432 |
|
threadStarted.countDown(); |
433 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
434 |
< |
done.await(); |
434 |
> |
await(done); |
435 |
|
}}); |
436 |
|
} |
437 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
470 |
|
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
471 |
|
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
472 |
|
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
473 |
< |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
474 |
< |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
473 |
> |
assertFalse(p.awaitTermination(randomExpiredTimeout(), |
474 |
> |
randomTimeUnit())); |
475 |
|
long timeoutNanos = 999999L; |
476 |
|
long startTime = System.nanoTime(); |
477 |
|
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
504 |
|
public void realRun() throws InterruptedException { |
505 |
|
assertFalse(p.isTerminating()); |
506 |
|
threadStarted.countDown(); |
507 |
< |
done.await(); |
507 |
> |
await(done); |
508 |
|
}}); |
509 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
509 |
> |
await(threadStarted); |
510 |
|
assertFalse(p.isTerminating()); |
511 |
|
done.countDown(); |
512 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
532 |
|
public void realRun() throws InterruptedException { |
533 |
|
assertFalse(p.isTerminating()); |
534 |
|
threadStarted.countDown(); |
535 |
< |
done.await(); |
535 |
> |
await(done); |
536 |
|
}}); |
537 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
537 |
> |
await(threadStarted); |
538 |
|
assertFalse(p.isTerminating()); |
539 |
|
done.countDown(); |
540 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
548 |
|
* getQueue returns the work queue, which contains queued tasks |
549 |
|
*/ |
550 |
|
public void testGetQueue() throws InterruptedException { |
551 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
551 |
> |
final CountDownLatch done = new CountDownLatch(1); |
552 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
553 |
|
final ThreadPoolExecutor p = |
554 |
|
new ThreadPoolExecutor(1, 1, |
555 |
|
LONG_DELAY_MS, MILLISECONDS, |
556 |
|
q); |
557 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
557 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
558 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
547 |
– |
final CountDownLatch done = new CountDownLatch(1); |
559 |
|
FutureTask[] tasks = new FutureTask[5]; |
560 |
|
for (int i = 0; i < tasks.length; i++) { |
561 |
|
Callable task = new CheckedCallable<Boolean>() { |
562 |
|
public Boolean realCall() throws InterruptedException { |
563 |
|
threadStarted.countDown(); |
564 |
|
assertSame(q, p.getQueue()); |
565 |
< |
done.await(); |
565 |
> |
await(done); |
566 |
|
return Boolean.TRUE; |
567 |
|
}}; |
568 |
|
tasks[i] = new FutureTask(task); |
569 |
|
p.execute(tasks[i]); |
570 |
|
} |
571 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
571 |
> |
await(threadStarted); |
572 |
|
assertSame(q, p.getQueue()); |
573 |
|
assertFalse(q.contains(tasks[0])); |
574 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
575 |
|
assertEquals(tasks.length - 1, q.size()); |
565 |
– |
done.countDown(); |
576 |
|
} |
577 |
|
} |
578 |
|
|
580 |
|
* remove(task) removes queued task, and fails to remove active task |
581 |
|
*/ |
582 |
|
public void testRemove() throws InterruptedException { |
583 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
583 |
> |
final CountDownLatch done = new CountDownLatch(1); |
584 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
585 |
|
final ThreadPoolExecutor p = |
586 |
|
new ThreadPoolExecutor(1, 1, |
587 |
|
LONG_DELAY_MS, MILLISECONDS, |
588 |
|
q); |
589 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
589 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
590 |
|
Runnable[] tasks = new Runnable[6]; |
591 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
581 |
– |
final CountDownLatch done = new CountDownLatch(1); |
592 |
|
for (int i = 0; i < tasks.length; i++) { |
593 |
|
tasks[i] = new CheckedRunnable() { |
594 |
|
public void realRun() throws InterruptedException { |
595 |
|
threadStarted.countDown(); |
596 |
< |
done.await(); |
596 |
> |
await(done); |
597 |
|
}}; |
598 |
|
p.execute(tasks[i]); |
599 |
|
} |
600 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
600 |
> |
await(threadStarted); |
601 |
|
assertFalse(p.remove(tasks[0])); |
602 |
|
assertTrue(q.contains(tasks[4])); |
603 |
|
assertTrue(q.contains(tasks[3])); |
607 |
|
assertTrue(q.contains(tasks[3])); |
608 |
|
assertTrue(p.remove(tasks[3])); |
609 |
|
assertFalse(q.contains(tasks[3])); |
600 |
– |
done.countDown(); |
610 |
|
} |
611 |
|
} |
612 |
|
|
616 |
|
public void testPurge() throws InterruptedException { |
617 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
618 |
|
final CountDownLatch done = new CountDownLatch(1); |
619 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
619 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
620 |
|
final ThreadPoolExecutor p = |
621 |
|
new ThreadPoolExecutor(1, 1, |
622 |
|
LONG_DELAY_MS, MILLISECONDS, |
627 |
|
Callable task = new CheckedCallable<Boolean>() { |
628 |
|
public Boolean realCall() throws InterruptedException { |
629 |
|
threadStarted.countDown(); |
630 |
< |
done.await(); |
630 |
> |
await(done); |
631 |
|
return Boolean.TRUE; |
632 |
|
}}; |
633 |
|
tasks[i] = new FutureTask(task); |
634 |
|
p.execute(tasks[i]); |
635 |
|
} |
636 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
636 |
> |
await(threadStarted); |
637 |
|
assertEquals(tasks.length, p.getTaskCount()); |
638 |
|
assertEquals(tasks.length - 1, q.size()); |
639 |
|
assertEquals(1L, p.getActiveCount()); |
671 |
|
}}; |
672 |
|
for (int i = 0; i < count; i++) |
673 |
|
p.execute(waiter); |
674 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
674 |
> |
await(threadsStarted); |
675 |
|
assertEquals(poolSize, p.getActiveCount()); |
676 |
|
assertEquals(0, p.getCompletedTaskCount()); |
677 |
|
final List<Runnable> queuedTasks; |
1033 |
|
* get of submitted callable throws InterruptedException if interrupted |
1034 |
|
*/ |
1035 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1036 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1037 |
|
final ThreadPoolExecutor p = |
1038 |
|
new ThreadPoolExecutor(1, 1, |
1039 |
|
60, SECONDS, |
1040 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1041 |
|
|
1042 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1042 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1043 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1034 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1044 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1045 |
|
public void realRun() throws Exception { |
1046 |
|
Callable task = new CheckedCallable<Boolean>() { |
1047 |
|
public Boolean realCall() throws InterruptedException { |
1048 |
|
threadStarted.countDown(); |
1049 |
< |
done.await(); |
1049 |
> |
await(done); |
1050 |
|
return Boolean.TRUE; |
1051 |
|
}}; |
1052 |
|
p.submit(task).get(); |
1053 |
|
}}); |
1054 |
|
|
1055 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1055 |
> |
await(threadStarted); |
1056 |
|
t.interrupt(); |
1057 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
< |
done.countDown(); |
1057 |
> |
awaitTermination(t); |
1058 |
|
} |
1059 |
|
} |
1060 |
|
|
1062 |
|
* execute throws RejectedExecutionException if saturated. |
1063 |
|
*/ |
1064 |
|
public void testSaturatedExecute() { |
1065 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1066 |
|
final ThreadPoolExecutor p = |
1067 |
|
new ThreadPoolExecutor(1, 1, |
1068 |
|
LONG_DELAY_MS, MILLISECONDS, |
1069 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1070 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1062 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1070 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1071 |
|
Runnable task = new CheckedRunnable() { |
1072 |
|
public void realRun() throws InterruptedException { |
1073 |
< |
done.await(); |
1073 |
> |
await(done); |
1074 |
|
}}; |
1075 |
|
for (int i = 0; i < 2; ++i) |
1076 |
|
p.execute(task); |
1081 |
|
} catch (RejectedExecutionException success) {} |
1082 |
|
assertTrue(p.getTaskCount() <= 2); |
1083 |
|
} |
1076 |
– |
done.countDown(); |
1084 |
|
} |
1085 |
|
} |
1086 |
|
|
1088 |
|
* submit(runnable) throws RejectedExecutionException if saturated. |
1089 |
|
*/ |
1090 |
|
public void testSaturatedSubmitRunnable() { |
1091 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1092 |
|
final ThreadPoolExecutor p = |
1093 |
|
new ThreadPoolExecutor(1, 1, |
1094 |
|
LONG_DELAY_MS, MILLISECONDS, |
1095 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1096 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1089 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1096 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1097 |
|
Runnable task = new CheckedRunnable() { |
1098 |
|
public void realRun() throws InterruptedException { |
1099 |
< |
done.await(); |
1099 |
> |
await(done); |
1100 |
|
}}; |
1101 |
|
for (int i = 0; i < 2; ++i) |
1102 |
|
p.submit(task); |
1107 |
|
} catch (RejectedExecutionException success) {} |
1108 |
|
assertTrue(p.getTaskCount() <= 2); |
1109 |
|
} |
1103 |
– |
done.countDown(); |
1110 |
|
} |
1111 |
|
} |
1112 |
|
|
1114 |
|
* submit(callable) throws RejectedExecutionException if saturated. |
1115 |
|
*/ |
1116 |
|
public void testSaturatedSubmitCallable() { |
1117 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1118 |
|
final ThreadPoolExecutor p = |
1119 |
|
new ThreadPoolExecutor(1, 1, |
1120 |
|
LONG_DELAY_MS, MILLISECONDS, |
1121 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1122 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1116 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1122 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1123 |
|
Runnable task = new CheckedRunnable() { |
1124 |
|
public void realRun() throws InterruptedException { |
1125 |
< |
done.await(); |
1125 |
> |
await(done); |
1126 |
|
}}; |
1127 |
|
for (int i = 0; i < 2; ++i) |
1128 |
< |
p.submit(Executors.callable(task)); |
1128 |
> |
p.execute(task); |
1129 |
|
for (int i = 0; i < 2; ++i) { |
1130 |
|
try { |
1131 |
|
p.execute(task); |
1133 |
|
} catch (RejectedExecutionException success) {} |
1134 |
|
assertTrue(p.getTaskCount() <= 2); |
1135 |
|
} |
1130 |
– |
done.countDown(); |
1136 |
|
} |
1137 |
|
} |
1138 |
|
|
1150 |
|
final CountDownLatch done = new CountDownLatch(1); |
1151 |
|
Runnable blocker = new CheckedRunnable() { |
1152 |
|
public void realRun() throws InterruptedException { |
1153 |
< |
done.await(); |
1153 |
> |
await(done); |
1154 |
|
}}; |
1155 |
|
p.execute(blocker); |
1156 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1169 |
|
* executor using DiscardPolicy drops task if saturated. |
1170 |
|
*/ |
1171 |
|
public void testSaturatedExecute3() { |
1172 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1173 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1174 |
|
for (int i = 0; i < tasks.length; ++i) |
1175 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1178 |
|
LONG_DELAY_MS, MILLISECONDS, |
1179 |
|
new ArrayBlockingQueue<Runnable>(1), |
1180 |
|
new ThreadPoolExecutor.DiscardPolicy()); |
1181 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1176 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1181 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1182 |
|
p.execute(awaiter(done)); |
1183 |
|
|
1184 |
|
for (TrackedNoOpRunnable task : tasks) |
1185 |
|
p.execute(task); |
1186 |
|
for (int i = 1; i < tasks.length; i++) |
1187 |
|
assertFalse(tasks[i].done); |
1183 |
– |
done.countDown(); |
1188 |
|
} |
1189 |
|
for (int i = 1; i < tasks.length; i++) |
1190 |
|
assertFalse(tasks[i].done); |
1204 |
|
LONG_DELAY_MS, MILLISECONDS, |
1205 |
|
new ArrayBlockingQueue<Runnable>(1), |
1206 |
|
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1207 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1207 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1208 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1209 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1210 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1214 |
|
p.execute(r3); |
1215 |
|
assertFalse(p.getQueue().contains(r2)); |
1216 |
|
assertTrue(p.getQueue().contains(r3)); |
1213 |
– |
done.countDown(); |
1217 |
|
} |
1218 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1219 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1520 |
|
LONG_DELAY_MS, MILLISECONDS, |
1521 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1522 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1523 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1523 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1524 |
|
l.add(latchAwaitingStringTask(latch)); |
1525 |
|
l.add(null); |
1526 |
|
try { |
1540 |
|
LONG_DELAY_MS, MILLISECONDS, |
1541 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1542 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1543 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1543 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1544 |
|
l.add(new NPETask()); |
1545 |
|
try { |
1546 |
|
e.invokeAny(l); |
1560 |
|
LONG_DELAY_MS, MILLISECONDS, |
1561 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1562 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1563 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1563 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1564 |
|
l.add(new StringTask()); |
1565 |
|
l.add(new StringTask()); |
1566 |
|
String result = e.invokeAny(l); |
1607 |
|
LONG_DELAY_MS, MILLISECONDS, |
1608 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1609 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1610 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1610 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1611 |
|
l.add(new StringTask()); |
1612 |
|
l.add(null); |
1613 |
|
try { |
1626 |
|
LONG_DELAY_MS, MILLISECONDS, |
1627 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1628 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1629 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1629 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1630 |
|
l.add(new NPETask()); |
1631 |
|
List<Future<String>> futures = e.invokeAll(l); |
1632 |
|
assertEquals(1, futures.size()); |
1648 |
|
LONG_DELAY_MS, MILLISECONDS, |
1649 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1650 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1651 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1651 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1652 |
|
l.add(new StringTask()); |
1653 |
|
l.add(new StringTask()); |
1654 |
|
List<Future<String>> futures = e.invokeAll(l); |
1683 |
|
LONG_DELAY_MS, MILLISECONDS, |
1684 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1685 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1686 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1686 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1687 |
|
l.add(new StringTask()); |
1688 |
|
try { |
1689 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1719 |
|
LONG_DELAY_MS, MILLISECONDS, |
1720 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1721 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1722 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1722 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1723 |
|
l.add(latchAwaitingStringTask(latch)); |
1724 |
|
l.add(null); |
1725 |
|
try { |
1739 |
|
LONG_DELAY_MS, MILLISECONDS, |
1740 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1741 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1742 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1742 |
> |
long startTime = System.nanoTime(); |
1743 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1744 |
|
l.add(new NPETask()); |
1745 |
|
try { |
1746 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1746 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1747 |
|
shouldThrow(); |
1748 |
|
} catch (ExecutionException success) { |
1749 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1750 |
|
} |
1751 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1752 |
|
} |
1753 |
|
} |
1754 |
|
|
1761 |
|
LONG_DELAY_MS, MILLISECONDS, |
1762 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1763 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1764 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1764 |
> |
long startTime = System.nanoTime(); |
1765 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1766 |
|
l.add(new StringTask()); |
1767 |
|
l.add(new StringTask()); |
1768 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1768 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1769 |
|
assertSame(TEST_STRING, result); |
1770 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1771 |
|
} |
1772 |
|
} |
1773 |
|
|
1796 |
|
LONG_DELAY_MS, MILLISECONDS, |
1797 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1798 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1799 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1799 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1800 |
|
l.add(new StringTask()); |
1801 |
|
try { |
1802 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1829 |
|
LONG_DELAY_MS, MILLISECONDS, |
1830 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1831 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1832 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1832 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1833 |
|
l.add(new StringTask()); |
1834 |
|
l.add(null); |
1835 |
|
try { |
1848 |
|
LONG_DELAY_MS, MILLISECONDS, |
1849 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1850 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1851 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1851 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1852 |
|
l.add(new NPETask()); |
1853 |
|
List<Future<String>> futures = |
1854 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1854 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1855 |
|
assertEquals(1, futures.size()); |
1856 |
|
try { |
1857 |
|
futures.get(0).get(); |
1871 |
|
LONG_DELAY_MS, MILLISECONDS, |
1872 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1873 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1874 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1874 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1875 |
|
l.add(new StringTask()); |
1876 |
|
l.add(new StringTask()); |
1877 |
|
List<Future<String>> futures = |
1886 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1887 |
|
*/ |
1888 |
|
public void testTimedInvokeAll6() throws Exception { |
1889 |
< |
final ExecutorService e = |
1890 |
< |
new ThreadPoolExecutor(2, 2, |
1891 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1892 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1893 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1894 |
< |
for (long timeout = timeoutMillis();;) { |
1889 |
> |
for (long timeout = timeoutMillis();;) { |
1890 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1891 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1892 |
> |
public String realCall() { |
1893 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1894 |
> |
catch (InterruptedException ok) {} |
1895 |
> |
return "1"; }}; |
1896 |
> |
final ExecutorService p = |
1897 |
> |
new ThreadPoolExecutor(2, 2, |
1898 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1899 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1900 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1901 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1902 |
|
tasks.add(new StringTask("0")); |
1903 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1903 |
> |
tasks.add(waiter); |
1904 |
|
tasks.add(new StringTask("2")); |
1905 |
|
long startTime = System.nanoTime(); |
1906 |
|
List<Future<String>> futures = |
1907 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1907 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1908 |
|
assertEquals(tasks.size(), futures.size()); |
1909 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1910 |
|
for (Future future : futures) |
1941 |
|
public void realRun() { |
1942 |
|
done.countDown(); |
1943 |
|
}}); |
1944 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1944 |
> |
await(done); |
1945 |
|
} |
1946 |
|
} |
1947 |
|
|
2034 |
|
} |
2035 |
|
} |
2036 |
|
// enough time to run all tasks |
2037 |
< |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2037 |
> |
await(done, nTasks * SHORT_DELAY_MS); |
2038 |
|
} |
2039 |
|
} |
2040 |
|
|
2042 |
|
* get(cancelled task) throws CancellationException |
2043 |
|
*/ |
2044 |
|
public void testGet_cancelled() throws Exception { |
2045 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2046 |
|
final ExecutorService e = |
2047 |
|
new ThreadPoolExecutor(1, 1, |
2048 |
|
LONG_DELAY_MS, MILLISECONDS, |
2049 |
|
new LinkedBlockingQueue<Runnable>()); |
2050 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
2050 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2051 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2038 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2052 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2053 |
|
for (int i = 0; i < 2; i++) { |
2054 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2058 |
|
}}; |
2059 |
|
futures.add(e.submit(r)); |
2060 |
|
} |
2061 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2061 |
> |
await(blockerStarted); |
2062 |
|
for (Future<?> future : futures) future.cancel(false); |
2063 |
|
for (Future<?> future : futures) { |
2064 |
|
try { |
2072 |
|
assertTrue(future.isCancelled()); |
2073 |
|
assertTrue(future.isDone()); |
2074 |
|
} |
2062 |
– |
done.countDown(); |
2075 |
|
} |
2076 |
|
} |
2077 |
|
|