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; |
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 |
|
|
326 |
|
*/ |
327 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
328 |
|
final int THREADS = 3; |
329 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
330 |
|
final ThreadPoolExecutor p = |
331 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
332 |
|
LONG_DELAY_MS, MILLISECONDS, |
333 |
|
new ArrayBlockingQueue<Runnable>(10)); |
334 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
338 |
< |
final CountDownLatch done = new CountDownLatch(1); |
334 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
335 |
|
assertEquals(0, p.getLargestPoolSize()); |
336 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
337 |
|
for (int i = 0; i < THREADS; i++) |
338 |
|
p.execute(new CheckedRunnable() { |
339 |
|
public void realRun() throws InterruptedException { |
340 |
|
threadsStarted.countDown(); |
341 |
< |
done.await(); |
341 |
> |
await(done); |
342 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
343 |
|
}}); |
344 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
344 |
> |
await(threadsStarted); |
345 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
349 |
– |
done.countDown(); // release pool |
346 |
|
} |
347 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
348 |
|
} |
370 |
|
* become active |
371 |
|
*/ |
372 |
|
public void testGetPoolSize() throws InterruptedException { |
373 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
374 |
|
final ThreadPoolExecutor p = |
375 |
|
new ThreadPoolExecutor(1, 1, |
376 |
|
LONG_DELAY_MS, MILLISECONDS, |
377 |
|
new ArrayBlockingQueue<Runnable>(10)); |
378 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
382 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
383 |
< |
final CountDownLatch done = new CountDownLatch(1); |
378 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
379 |
|
assertEquals(0, p.getPoolSize()); |
380 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
381 |
|
p.execute(new CheckedRunnable() { |
382 |
|
public void realRun() throws InterruptedException { |
383 |
|
threadStarted.countDown(); |
384 |
|
assertEquals(1, p.getPoolSize()); |
385 |
< |
done.await(); |
385 |
> |
await(done); |
386 |
|
}}); |
387 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
387 |
> |
await(threadStarted); |
388 |
|
assertEquals(1, p.getPoolSize()); |
393 |
– |
done.countDown(); // release pool |
389 |
|
} |
390 |
|
} |
391 |
|
|
406 |
|
p.execute(new CheckedRunnable() { |
407 |
|
public void realRun() throws InterruptedException { |
408 |
|
threadStarted.countDown(); |
409 |
< |
done.await(); |
409 |
> |
await(done); |
410 |
|
}}); |
411 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
411 |
> |
await(threadStarted); |
412 |
|
assertEquals(1, p.getTaskCount()); |
413 |
|
assertEquals(0, p.getCompletedTaskCount()); |
414 |
|
for (int i = 0; i < TASKS; i++) { |
417 |
|
public void realRun() throws InterruptedException { |
418 |
|
threadStarted.countDown(); |
419 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
420 |
< |
done.await(); |
420 |
> |
await(done); |
421 |
|
}}); |
422 |
|
} |
423 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
490 |
|
public void realRun() throws InterruptedException { |
491 |
|
assertFalse(p.isTerminating()); |
492 |
|
threadStarted.countDown(); |
493 |
< |
done.await(); |
493 |
> |
await(done); |
494 |
|
}}); |
495 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
495 |
> |
await(threadStarted); |
496 |
|
assertFalse(p.isTerminating()); |
497 |
|
done.countDown(); |
498 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
518 |
|
public void realRun() throws InterruptedException { |
519 |
|
assertFalse(p.isTerminating()); |
520 |
|
threadStarted.countDown(); |
521 |
< |
done.await(); |
521 |
> |
await(done); |
522 |
|
}}); |
523 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
523 |
> |
await(threadStarted); |
524 |
|
assertFalse(p.isTerminating()); |
525 |
|
done.countDown(); |
526 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
534 |
|
* getQueue returns the work queue, which contains queued tasks |
535 |
|
*/ |
536 |
|
public void testGetQueue() throws InterruptedException { |
537 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
537 |
> |
final CountDownLatch done = new CountDownLatch(1); |
538 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
539 |
|
final ThreadPoolExecutor p = |
540 |
|
new ThreadPoolExecutor(1, 1, |
541 |
|
LONG_DELAY_MS, MILLISECONDS, |
542 |
|
q); |
543 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
543 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
544 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
549 |
– |
final CountDownLatch done = new CountDownLatch(1); |
545 |
|
FutureTask[] tasks = new FutureTask[5]; |
546 |
|
for (int i = 0; i < tasks.length; i++) { |
547 |
|
Callable task = new CheckedCallable<Boolean>() { |
548 |
|
public Boolean realCall() throws InterruptedException { |
549 |
|
threadStarted.countDown(); |
550 |
|
assertSame(q, p.getQueue()); |
551 |
< |
done.await(); |
551 |
> |
await(done); |
552 |
|
return Boolean.TRUE; |
553 |
|
}}; |
554 |
|
tasks[i] = new FutureTask(task); |
555 |
|
p.execute(tasks[i]); |
556 |
|
} |
557 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
557 |
> |
await(threadStarted); |
558 |
|
assertSame(q, p.getQueue()); |
559 |
|
assertFalse(q.contains(tasks[0])); |
560 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
561 |
|
assertEquals(tasks.length - 1, q.size()); |
567 |
– |
done.countDown(); |
562 |
|
} |
563 |
|
} |
564 |
|
|
566 |
|
* remove(task) removes queued task, and fails to remove active task |
567 |
|
*/ |
568 |
|
public void testRemove() throws InterruptedException { |
569 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
569 |
> |
final CountDownLatch done = new CountDownLatch(1); |
570 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
571 |
|
final ThreadPoolExecutor p = |
572 |
|
new ThreadPoolExecutor(1, 1, |
573 |
|
LONG_DELAY_MS, MILLISECONDS, |
574 |
|
q); |
575 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
575 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
576 |
|
Runnable[] tasks = new Runnable[6]; |
577 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
583 |
– |
final CountDownLatch done = new CountDownLatch(1); |
578 |
|
for (int i = 0; i < tasks.length; i++) { |
579 |
|
tasks[i] = new CheckedRunnable() { |
580 |
|
public void realRun() throws InterruptedException { |
581 |
|
threadStarted.countDown(); |
582 |
< |
done.await(); |
582 |
> |
await(done); |
583 |
|
}}; |
584 |
|
p.execute(tasks[i]); |
585 |
|
} |
586 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
586 |
> |
await(threadStarted); |
587 |
|
assertFalse(p.remove(tasks[0])); |
588 |
|
assertTrue(q.contains(tasks[4])); |
589 |
|
assertTrue(q.contains(tasks[3])); |
593 |
|
assertTrue(q.contains(tasks[3])); |
594 |
|
assertTrue(p.remove(tasks[3])); |
595 |
|
assertFalse(q.contains(tasks[3])); |
602 |
– |
done.countDown(); |
596 |
|
} |
597 |
|
} |
598 |
|
|
602 |
|
public void testPurge() throws InterruptedException { |
603 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
604 |
|
final CountDownLatch done = new CountDownLatch(1); |
605 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
605 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
606 |
|
final ThreadPoolExecutor p = |
607 |
|
new ThreadPoolExecutor(1, 1, |
608 |
|
LONG_DELAY_MS, MILLISECONDS, |
613 |
|
Callable task = new CheckedCallable<Boolean>() { |
614 |
|
public Boolean realCall() throws InterruptedException { |
615 |
|
threadStarted.countDown(); |
616 |
< |
done.await(); |
616 |
> |
await(done); |
617 |
|
return Boolean.TRUE; |
618 |
|
}}; |
619 |
|
tasks[i] = new FutureTask(task); |
620 |
|
p.execute(tasks[i]); |
621 |
|
} |
622 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
622 |
> |
await(threadStarted); |
623 |
|
assertEquals(tasks.length, p.getTaskCount()); |
624 |
|
assertEquals(tasks.length - 1, q.size()); |
625 |
|
assertEquals(1L, p.getActiveCount()); |
657 |
|
}}; |
658 |
|
for (int i = 0; i < count; i++) |
659 |
|
p.execute(waiter); |
660 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
660 |
> |
await(threadsStarted); |
661 |
|
assertEquals(poolSize, p.getActiveCount()); |
662 |
|
assertEquals(0, p.getCompletedTaskCount()); |
663 |
|
final List<Runnable> queuedTasks; |
1019 |
|
* get of submitted callable throws InterruptedException if interrupted |
1020 |
|
*/ |
1021 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1022 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1023 |
|
final ThreadPoolExecutor p = |
1024 |
|
new ThreadPoolExecutor(1, 1, |
1025 |
|
60, SECONDS, |
1026 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1027 |
|
|
1028 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1028 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1029 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1036 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1030 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1031 |
|
public void realRun() throws Exception { |
1032 |
|
Callable task = new CheckedCallable<Boolean>() { |
1033 |
|
public Boolean realCall() throws InterruptedException { |
1034 |
|
threadStarted.countDown(); |
1035 |
< |
done.await(); |
1035 |
> |
await(done); |
1036 |
|
return Boolean.TRUE; |
1037 |
|
}}; |
1038 |
|
p.submit(task).get(); |
1039 |
|
}}); |
1040 |
|
|
1041 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1041 |
> |
await(threadStarted); |
1042 |
|
t.interrupt(); |
1043 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1051 |
< |
done.countDown(); |
1043 |
> |
awaitTermination(t); |
1044 |
|
} |
1045 |
|
} |
1046 |
|
|
1048 |
|
* execute throws RejectedExecutionException if saturated. |
1049 |
|
*/ |
1050 |
|
public void testSaturatedExecute() { |
1051 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1052 |
|
final ThreadPoolExecutor p = |
1053 |
|
new ThreadPoolExecutor(1, 1, |
1054 |
|
LONG_DELAY_MS, MILLISECONDS, |
1055 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1056 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1064 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1056 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1057 |
|
Runnable task = new CheckedRunnable() { |
1058 |
|
public void realRun() throws InterruptedException { |
1059 |
< |
done.await(); |
1059 |
> |
await(done); |
1060 |
|
}}; |
1061 |
|
for (int i = 0; i < 2; ++i) |
1062 |
|
p.execute(task); |
1067 |
|
} catch (RejectedExecutionException success) {} |
1068 |
|
assertTrue(p.getTaskCount() <= 2); |
1069 |
|
} |
1078 |
– |
done.countDown(); |
1070 |
|
} |
1071 |
|
} |
1072 |
|
|
1074 |
|
* submit(runnable) throws RejectedExecutionException if saturated. |
1075 |
|
*/ |
1076 |
|
public void testSaturatedSubmitRunnable() { |
1077 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1078 |
|
final ThreadPoolExecutor p = |
1079 |
|
new ThreadPoolExecutor(1, 1, |
1080 |
|
LONG_DELAY_MS, MILLISECONDS, |
1081 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1082 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1091 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1082 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1083 |
|
Runnable task = new CheckedRunnable() { |
1084 |
|
public void realRun() throws InterruptedException { |
1085 |
< |
done.await(); |
1085 |
> |
await(done); |
1086 |
|
}}; |
1087 |
|
for (int i = 0; i < 2; ++i) |
1088 |
|
p.submit(task); |
1093 |
|
} catch (RejectedExecutionException success) {} |
1094 |
|
assertTrue(p.getTaskCount() <= 2); |
1095 |
|
} |
1105 |
– |
done.countDown(); |
1096 |
|
} |
1097 |
|
} |
1098 |
|
|
1100 |
|
* submit(callable) throws RejectedExecutionException if saturated. |
1101 |
|
*/ |
1102 |
|
public void testSaturatedSubmitCallable() { |
1103 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1104 |
|
final ThreadPoolExecutor p = |
1105 |
|
new ThreadPoolExecutor(1, 1, |
1106 |
|
LONG_DELAY_MS, MILLISECONDS, |
1107 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1108 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1118 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1108 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1109 |
|
Runnable task = new CheckedRunnable() { |
1110 |
|
public void realRun() throws InterruptedException { |
1111 |
< |
done.await(); |
1111 |
> |
await(done); |
1112 |
|
}}; |
1113 |
|
for (int i = 0; i < 2; ++i) |
1114 |
< |
p.submit(Executors.callable(task)); |
1114 |
> |
p.execute(task); |
1115 |
|
for (int i = 0; i < 2; ++i) { |
1116 |
|
try { |
1117 |
|
p.execute(task); |
1119 |
|
} catch (RejectedExecutionException success) {} |
1120 |
|
assertTrue(p.getTaskCount() <= 2); |
1121 |
|
} |
1132 |
– |
done.countDown(); |
1122 |
|
} |
1123 |
|
} |
1124 |
|
|
1136 |
|
final CountDownLatch done = new CountDownLatch(1); |
1137 |
|
Runnable blocker = new CheckedRunnable() { |
1138 |
|
public void realRun() throws InterruptedException { |
1139 |
< |
done.await(); |
1139 |
> |
await(done); |
1140 |
|
}}; |
1141 |
|
p.execute(blocker); |
1142 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1155 |
|
* executor using DiscardPolicy drops task if saturated. |
1156 |
|
*/ |
1157 |
|
public void testSaturatedExecute3() { |
1158 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1159 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1160 |
|
for (int i = 0; i < tasks.length; ++i) |
1161 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1164 |
|
LONG_DELAY_MS, MILLISECONDS, |
1165 |
|
new ArrayBlockingQueue<Runnable>(1), |
1166 |
|
new ThreadPoolExecutor.DiscardPolicy()); |
1167 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1178 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1167 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1168 |
|
p.execute(awaiter(done)); |
1169 |
|
|
1170 |
|
for (TrackedNoOpRunnable task : tasks) |
1171 |
|
p.execute(task); |
1172 |
|
for (int i = 1; i < tasks.length; i++) |
1173 |
|
assertFalse(tasks[i].done); |
1185 |
– |
done.countDown(); |
1174 |
|
} |
1175 |
|
for (int i = 1; i < tasks.length; i++) |
1176 |
|
assertFalse(tasks[i].done); |
1190 |
|
LONG_DELAY_MS, MILLISECONDS, |
1191 |
|
new ArrayBlockingQueue<Runnable>(1), |
1192 |
|
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1193 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1193 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1194 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1195 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1196 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1200 |
|
p.execute(r3); |
1201 |
|
assertFalse(p.getQueue().contains(r2)); |
1202 |
|
assertTrue(p.getQueue().contains(r3)); |
1215 |
– |
done.countDown(); |
1203 |
|
} |
1204 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1205 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1506 |
|
LONG_DELAY_MS, MILLISECONDS, |
1507 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1508 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1509 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1509 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1510 |
|
l.add(latchAwaitingStringTask(latch)); |
1511 |
|
l.add(null); |
1512 |
|
try { |
1526 |
|
LONG_DELAY_MS, MILLISECONDS, |
1527 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1528 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1529 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1529 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1530 |
|
l.add(new NPETask()); |
1531 |
|
try { |
1532 |
|
e.invokeAny(l); |
1546 |
|
LONG_DELAY_MS, MILLISECONDS, |
1547 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1548 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1549 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1549 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1550 |
|
l.add(new StringTask()); |
1551 |
|
l.add(new StringTask()); |
1552 |
|
String result = e.invokeAny(l); |
1593 |
|
LONG_DELAY_MS, MILLISECONDS, |
1594 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1595 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1596 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1596 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1597 |
|
l.add(new StringTask()); |
1598 |
|
l.add(null); |
1599 |
|
try { |
1612 |
|
LONG_DELAY_MS, MILLISECONDS, |
1613 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1614 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1615 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1615 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1616 |
|
l.add(new NPETask()); |
1617 |
|
List<Future<String>> futures = e.invokeAll(l); |
1618 |
|
assertEquals(1, futures.size()); |
1634 |
|
LONG_DELAY_MS, MILLISECONDS, |
1635 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1636 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1637 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1637 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1638 |
|
l.add(new StringTask()); |
1639 |
|
l.add(new StringTask()); |
1640 |
|
List<Future<String>> futures = e.invokeAll(l); |
1669 |
|
LONG_DELAY_MS, MILLISECONDS, |
1670 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1671 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1672 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1672 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1673 |
|
l.add(new StringTask()); |
1674 |
|
try { |
1675 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1705 |
|
LONG_DELAY_MS, MILLISECONDS, |
1706 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1707 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1708 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1708 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1709 |
|
l.add(latchAwaitingStringTask(latch)); |
1710 |
|
l.add(null); |
1711 |
|
try { |
1725 |
|
LONG_DELAY_MS, MILLISECONDS, |
1726 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1727 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1728 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1728 |
> |
long startTime = System.nanoTime(); |
1729 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1730 |
|
l.add(new NPETask()); |
1731 |
|
try { |
1732 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1732 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1733 |
|
shouldThrow(); |
1734 |
|
} catch (ExecutionException success) { |
1735 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1736 |
|
} |
1737 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1738 |
|
} |
1739 |
|
} |
1740 |
|
|
1747 |
|
LONG_DELAY_MS, MILLISECONDS, |
1748 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1749 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1750 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1750 |
> |
long startTime = System.nanoTime(); |
1751 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1752 |
|
l.add(new StringTask()); |
1753 |
|
l.add(new StringTask()); |
1754 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1754 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1755 |
|
assertSame(TEST_STRING, result); |
1756 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1757 |
|
} |
1758 |
|
} |
1759 |
|
|
1782 |
|
LONG_DELAY_MS, MILLISECONDS, |
1783 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1784 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1785 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1785 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1786 |
|
l.add(new StringTask()); |
1787 |
|
try { |
1788 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1815 |
|
LONG_DELAY_MS, MILLISECONDS, |
1816 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1817 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1818 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1818 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1819 |
|
l.add(new StringTask()); |
1820 |
|
l.add(null); |
1821 |
|
try { |
1834 |
|
LONG_DELAY_MS, MILLISECONDS, |
1835 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1836 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1837 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1837 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1838 |
|
l.add(new NPETask()); |
1839 |
|
List<Future<String>> futures = |
1840 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1840 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1841 |
|
assertEquals(1, futures.size()); |
1842 |
|
try { |
1843 |
|
futures.get(0).get(); |
1857 |
|
LONG_DELAY_MS, MILLISECONDS, |
1858 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1859 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1860 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1860 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1861 |
|
l.add(new StringTask()); |
1862 |
|
l.add(new StringTask()); |
1863 |
|
List<Future<String>> futures = |
1872 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1873 |
|
*/ |
1874 |
|
public void testTimedInvokeAll6() throws Exception { |
1875 |
< |
final ExecutorService e = |
1876 |
< |
new ThreadPoolExecutor(2, 2, |
1877 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1878 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1879 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1880 |
< |
for (long timeout = timeoutMillis();;) { |
1875 |
> |
for (long timeout = timeoutMillis();;) { |
1876 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1877 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1878 |
> |
public String realCall() { |
1879 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1880 |
> |
catch (InterruptedException ok) {} |
1881 |
> |
return "1"; }}; |
1882 |
> |
final ExecutorService p = |
1883 |
> |
new ThreadPoolExecutor(2, 2, |
1884 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1885 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1886 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1887 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1888 |
|
tasks.add(new StringTask("0")); |
1889 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1889 |
> |
tasks.add(waiter); |
1890 |
|
tasks.add(new StringTask("2")); |
1891 |
|
long startTime = System.nanoTime(); |
1892 |
|
List<Future<String>> futures = |
1893 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1893 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1894 |
|
assertEquals(tasks.size(), futures.size()); |
1895 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1896 |
|
for (Future future : futures) |
2028 |
|
* get(cancelled task) throws CancellationException |
2029 |
|
*/ |
2030 |
|
public void testGet_cancelled() throws Exception { |
2031 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2032 |
|
final ExecutorService e = |
2033 |
|
new ThreadPoolExecutor(1, 1, |
2034 |
|
LONG_DELAY_MS, MILLISECONDS, |
2035 |
|
new LinkedBlockingQueue<Runnable>()); |
2036 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
2036 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2037 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2040 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2038 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2039 |
|
for (int i = 0; i < 2; i++) { |
2040 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2044 |
|
}}; |
2045 |
|
futures.add(e.submit(r)); |
2046 |
|
} |
2047 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2047 |
> |
await(blockerStarted); |
2048 |
|
for (Future<?> future : futures) future.cancel(false); |
2049 |
|
for (Future<?> future : futures) { |
2050 |
|
try { |
2058 |
|
assertTrue(future.isCancelled()); |
2059 |
|
assertTrue(future.isDone()); |
2060 |
|
} |
2064 |
– |
done.countDown(); |
2061 |
|
} |
2062 |
|
} |
2063 |
|
|