28 |
|
import java.util.concurrent.SynchronousQueue; |
29 |
|
import java.util.concurrent.ThreadFactory; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
– |
import java.util.concurrent.TimeUnit; |
31 |
|
import java.util.concurrent.atomic.AtomicInteger; |
32 |
|
|
33 |
|
import junit.framework.Test; |
100 |
|
* thread becomes active |
101 |
|
*/ |
102 |
|
public void testGetActiveCount() throws InterruptedException { |
103 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
104 |
|
final ThreadPoolExecutor p = |
105 |
|
new ThreadPoolExecutor(2, 2, |
106 |
|
LONG_DELAY_MS, MILLISECONDS, |
107 |
|
new ArrayBlockingQueue<Runnable>(10)); |
108 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
108 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
109 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
– |
final CountDownLatch done = new CountDownLatch(1); |
110 |
|
assertEquals(0, p.getActiveCount()); |
111 |
|
p.execute(new CheckedRunnable() { |
112 |
|
public void realRun() throws InterruptedException { |
113 |
|
threadStarted.countDown(); |
114 |
|
assertEquals(1, p.getActiveCount()); |
115 |
< |
done.await(); |
115 |
> |
await(done); |
116 |
|
}}); |
117 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
117 |
> |
await(threadStarted); |
118 |
|
assertEquals(1, p.getActiveCount()); |
120 |
– |
done.countDown(); |
119 |
|
} |
120 |
|
} |
121 |
|
|
339 |
|
p.execute(new CheckedRunnable() { |
340 |
|
public void realRun() throws InterruptedException { |
341 |
|
threadsStarted.countDown(); |
342 |
< |
done.await(); |
342 |
> |
await(done); |
343 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
344 |
|
}}); |
345 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
345 |
> |
await(threadsStarted); |
346 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
347 |
|
} |
348 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
383 |
|
public void realRun() throws InterruptedException { |
384 |
|
threadStarted.countDown(); |
385 |
|
assertEquals(1, p.getPoolSize()); |
386 |
< |
done.await(); |
386 |
> |
await(done); |
387 |
|
}}); |
388 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
388 |
> |
await(threadStarted); |
389 |
|
assertEquals(1, p.getPoolSize()); |
390 |
|
} |
391 |
|
} |
407 |
|
p.execute(new CheckedRunnable() { |
408 |
|
public void realRun() throws InterruptedException { |
409 |
|
threadStarted.countDown(); |
410 |
< |
done.await(); |
410 |
> |
await(done); |
411 |
|
}}); |
412 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
412 |
> |
await(threadStarted); |
413 |
|
assertEquals(1, p.getTaskCount()); |
414 |
|
assertEquals(0, p.getCompletedTaskCount()); |
415 |
|
for (int i = 0; i < TASKS; i++) { |
418 |
|
public void realRun() throws InterruptedException { |
419 |
|
threadStarted.countDown(); |
420 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
421 |
< |
done.await(); |
421 |
> |
await(done); |
422 |
|
}}); |
423 |
|
} |
424 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
491 |
|
public void realRun() throws InterruptedException { |
492 |
|
assertFalse(p.isTerminating()); |
493 |
|
threadStarted.countDown(); |
494 |
< |
done.await(); |
494 |
> |
await(done); |
495 |
|
}}); |
496 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
496 |
> |
await(threadStarted); |
497 |
|
assertFalse(p.isTerminating()); |
498 |
|
done.countDown(); |
499 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
519 |
|
public void realRun() throws InterruptedException { |
520 |
|
assertFalse(p.isTerminating()); |
521 |
|
threadStarted.countDown(); |
522 |
< |
done.await(); |
522 |
> |
await(done); |
523 |
|
}}); |
524 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
524 |
> |
await(threadStarted); |
525 |
|
assertFalse(p.isTerminating()); |
526 |
|
done.countDown(); |
527 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
535 |
|
* getQueue returns the work queue, which contains queued tasks |
536 |
|
*/ |
537 |
|
public void testGetQueue() throws InterruptedException { |
538 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
539 |
|
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
540 |
|
final ThreadPoolExecutor p = |
541 |
|
new ThreadPoolExecutor(1, 1, |
542 |
|
LONG_DELAY_MS, MILLISECONDS, |
543 |
|
q); |
544 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
544 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
545 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
547 |
– |
final CountDownLatch done = new CountDownLatch(1); |
546 |
|
FutureTask[] tasks = new FutureTask[5]; |
547 |
|
for (int i = 0; i < tasks.length; i++) { |
548 |
|
Callable task = new CheckedCallable<Boolean>() { |
549 |
|
public Boolean realCall() throws InterruptedException { |
550 |
|
threadStarted.countDown(); |
551 |
|
assertSame(q, p.getQueue()); |
552 |
< |
done.await(); |
552 |
> |
await(done); |
553 |
|
return Boolean.TRUE; |
554 |
|
}}; |
555 |
|
tasks[i] = new FutureTask(task); |
556 |
|
p.execute(tasks[i]); |
557 |
|
} |
558 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
558 |
> |
await(threadStarted); |
559 |
|
assertSame(q, p.getQueue()); |
560 |
|
assertFalse(q.contains(tasks[0])); |
561 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
562 |
|
assertEquals(tasks.length - 1, q.size()); |
565 |
– |
done.countDown(); |
563 |
|
} |
564 |
|
} |
565 |
|
|
567 |
|
* remove(task) removes queued task, and fails to remove active task |
568 |
|
*/ |
569 |
|
public void testRemove() throws InterruptedException { |
570 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
571 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
572 |
|
final ThreadPoolExecutor p = |
573 |
|
new ThreadPoolExecutor(1, 1, |
574 |
|
LONG_DELAY_MS, MILLISECONDS, |
575 |
|
q); |
576 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
576 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
577 |
|
Runnable[] tasks = new Runnable[6]; |
578 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
581 |
– |
final CountDownLatch done = new CountDownLatch(1); |
579 |
|
for (int i = 0; i < tasks.length; i++) { |
580 |
|
tasks[i] = new CheckedRunnable() { |
581 |
|
public void realRun() throws InterruptedException { |
582 |
|
threadStarted.countDown(); |
583 |
< |
done.await(); |
583 |
> |
await(done); |
584 |
|
}}; |
585 |
|
p.execute(tasks[i]); |
586 |
|
} |
587 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
587 |
> |
await(threadStarted); |
588 |
|
assertFalse(p.remove(tasks[0])); |
589 |
|
assertTrue(q.contains(tasks[4])); |
590 |
|
assertTrue(q.contains(tasks[3])); |
594 |
|
assertTrue(q.contains(tasks[3])); |
595 |
|
assertTrue(p.remove(tasks[3])); |
596 |
|
assertFalse(q.contains(tasks[3])); |
600 |
– |
done.countDown(); |
597 |
|
} |
598 |
|
} |
599 |
|
|
614 |
|
Callable task = new CheckedCallable<Boolean>() { |
615 |
|
public Boolean realCall() throws InterruptedException { |
616 |
|
threadStarted.countDown(); |
617 |
< |
done.await(); |
617 |
> |
await(done); |
618 |
|
return Boolean.TRUE; |
619 |
|
}}; |
620 |
|
tasks[i] = new FutureTask(task); |
621 |
|
p.execute(tasks[i]); |
622 |
|
} |
623 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
623 |
> |
await(threadStarted); |
624 |
|
assertEquals(tasks.length, p.getTaskCount()); |
625 |
|
assertEquals(tasks.length - 1, q.size()); |
626 |
|
assertEquals(1L, p.getActiveCount()); |
658 |
|
}}; |
659 |
|
for (int i = 0; i < count; i++) |
660 |
|
p.execute(waiter); |
661 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
661 |
> |
await(threadsStarted); |
662 |
|
assertEquals(poolSize, p.getActiveCount()); |
663 |
|
assertEquals(0, p.getCompletedTaskCount()); |
664 |
|
final List<Runnable> queuedTasks; |
1033 |
|
Callable task = new CheckedCallable<Boolean>() { |
1034 |
|
public Boolean realCall() throws InterruptedException { |
1035 |
|
threadStarted.countDown(); |
1036 |
< |
done.await(); |
1036 |
> |
await(done); |
1037 |
|
return Boolean.TRUE; |
1038 |
|
}}; |
1039 |
|
p.submit(task).get(); |
1049 |
|
* execute throws RejectedExecutionException if saturated. |
1050 |
|
*/ |
1051 |
|
public void testSaturatedExecute() { |
1052 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1053 |
|
final ThreadPoolExecutor p = |
1054 |
|
new ThreadPoolExecutor(1, 1, |
1055 |
|
LONG_DELAY_MS, MILLISECONDS, |
1056 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1057 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1061 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1057 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1058 |
|
Runnable task = new CheckedRunnable() { |
1059 |
|
public void realRun() throws InterruptedException { |
1060 |
< |
done.await(); |
1060 |
> |
await(done); |
1061 |
|
}}; |
1062 |
|
for (int i = 0; i < 2; ++i) |
1063 |
|
p.execute(task); |
1068 |
|
} catch (RejectedExecutionException success) {} |
1069 |
|
assertTrue(p.getTaskCount() <= 2); |
1070 |
|
} |
1075 |
– |
done.countDown(); |
1071 |
|
} |
1072 |
|
} |
1073 |
|
|
1075 |
|
* submit(runnable) throws RejectedExecutionException if saturated. |
1076 |
|
*/ |
1077 |
|
public void testSaturatedSubmitRunnable() { |
1078 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1079 |
|
final ThreadPoolExecutor p = |
1080 |
|
new ThreadPoolExecutor(1, 1, |
1081 |
|
LONG_DELAY_MS, MILLISECONDS, |
1082 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1083 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1088 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1083 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1084 |
|
Runnable task = new CheckedRunnable() { |
1085 |
|
public void realRun() throws InterruptedException { |
1086 |
< |
done.await(); |
1086 |
> |
await(done); |
1087 |
|
}}; |
1088 |
|
for (int i = 0; i < 2; ++i) |
1089 |
|
p.submit(task); |
1094 |
|
} catch (RejectedExecutionException success) {} |
1095 |
|
assertTrue(p.getTaskCount() <= 2); |
1096 |
|
} |
1102 |
– |
done.countDown(); |
1097 |
|
} |
1098 |
|
} |
1099 |
|
|
1101 |
|
* submit(callable) throws RejectedExecutionException if saturated. |
1102 |
|
*/ |
1103 |
|
public void testSaturatedSubmitCallable() { |
1104 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1105 |
|
final ThreadPoolExecutor p = |
1106 |
|
new ThreadPoolExecutor(1, 1, |
1107 |
|
LONG_DELAY_MS, MILLISECONDS, |
1108 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1109 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1115 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1109 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1110 |
|
Runnable task = new CheckedRunnable() { |
1111 |
|
public void realRun() throws InterruptedException { |
1112 |
< |
done.await(); |
1112 |
> |
await(done); |
1113 |
|
}}; |
1114 |
|
for (int i = 0; i < 2; ++i) |
1115 |
|
p.submit(Executors.callable(task)); |
1120 |
|
} catch (RejectedExecutionException success) {} |
1121 |
|
assertTrue(p.getTaskCount() <= 2); |
1122 |
|
} |
1129 |
– |
done.countDown(); |
1123 |
|
} |
1124 |
|
} |
1125 |
|
|
1137 |
|
final CountDownLatch done = new CountDownLatch(1); |
1138 |
|
Runnable blocker = new CheckedRunnable() { |
1139 |
|
public void realRun() throws InterruptedException { |
1140 |
< |
done.await(); |
1140 |
> |
await(done); |
1141 |
|
}}; |
1142 |
|
p.execute(blocker); |
1143 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1156 |
|
* executor using DiscardPolicy drops task if saturated. |
1157 |
|
*/ |
1158 |
|
public void testSaturatedExecute3() { |
1159 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1160 |
|
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1161 |
|
for (int i = 0; i < tasks.length; ++i) |
1162 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1165 |
|
LONG_DELAY_MS, MILLISECONDS, |
1166 |
|
new ArrayBlockingQueue<Runnable>(1), |
1167 |
|
new ThreadPoolExecutor.DiscardPolicy()); |
1168 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1175 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1168 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1169 |
|
p.execute(awaiter(done)); |
1170 |
|
|
1171 |
|
for (TrackedNoOpRunnable task : tasks) |
1172 |
|
p.execute(task); |
1173 |
|
for (int i = 1; i < tasks.length; i++) |
1174 |
|
assertFalse(tasks[i].done); |
1182 |
– |
done.countDown(); |
1175 |
|
} |
1176 |
|
for (int i = 1; i < tasks.length; i++) |
1177 |
|
assertFalse(tasks[i].done); |
1191 |
|
LONG_DELAY_MS, MILLISECONDS, |
1192 |
|
new ArrayBlockingQueue<Runnable>(1), |
1193 |
|
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1194 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1194 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1195 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1196 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1197 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1201 |
|
p.execute(r3); |
1202 |
|
assertFalse(p.getQueue().contains(r2)); |
1203 |
|
assertTrue(p.getQueue().contains(r3)); |
1212 |
– |
done.countDown(); |
1204 |
|
} |
1205 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1206 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1726 |
|
LONG_DELAY_MS, MILLISECONDS, |
1727 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1728 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1729 |
+ |
long startTime = System.nanoTime(); |
1730 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1731 |
|
l.add(new NPETask()); |
1732 |
|
try { |
1733 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1733 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1734 |
|
shouldThrow(); |
1735 |
|
} catch (ExecutionException success) { |
1736 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1737 |
|
} |
1738 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1739 |
|
} |
1740 |
|
} |
1741 |
|
|
1748 |
|
LONG_DELAY_MS, MILLISECONDS, |
1749 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1750 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1751 |
+ |
long startTime = System.nanoTime(); |
1752 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1753 |
|
l.add(new StringTask()); |
1754 |
|
l.add(new StringTask()); |
1755 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1755 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1756 |
|
assertSame(TEST_STRING, result); |
1757 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1758 |
|
} |
1759 |
|
} |
1760 |
|
|
1873 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1874 |
|
*/ |
1875 |
|
public void testTimedInvokeAll6() throws Exception { |
1876 |
< |
final ExecutorService e = |
1877 |
< |
new ThreadPoolExecutor(2, 2, |
1878 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1879 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1880 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1881 |
< |
for (long timeout = timeoutMillis();;) { |
1876 |
> |
for (long timeout = timeoutMillis();;) { |
1877 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1878 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1879 |
> |
public String realCall() { |
1880 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1881 |
> |
catch (InterruptedException ok) {} |
1882 |
> |
return "1"; }}; |
1883 |
> |
final ExecutorService p = |
1884 |
> |
new ThreadPoolExecutor(2, 2, |
1885 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1886 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1887 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1888 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1889 |
|
tasks.add(new StringTask("0")); |
1890 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1890 |
> |
tasks.add(waiter); |
1891 |
|
tasks.add(new StringTask("2")); |
1892 |
|
long startTime = System.nanoTime(); |
1893 |
|
List<Future<String>> futures = |
1894 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1894 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1895 |
|
assertEquals(tasks.size(), futures.size()); |
1896 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1897 |
|
for (Future future : futures) |
2029 |
|
* get(cancelled task) throws CancellationException |
2030 |
|
*/ |
2031 |
|
public void testGet_cancelled() throws Exception { |
2032 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2033 |
|
final ExecutorService e = |
2034 |
|
new ThreadPoolExecutor(1, 1, |
2035 |
|
LONG_DELAY_MS, MILLISECONDS, |
2036 |
|
new LinkedBlockingQueue<Runnable>()); |
2037 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
2037 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2038 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2037 |
– |
final CountDownLatch done = new CountDownLatch(1); |
2039 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2040 |
|
for (int i = 0; i < 2; i++) { |
2041 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
2045 |
|
}}; |
2046 |
|
futures.add(e.submit(r)); |
2047 |
|
} |
2048 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2048 |
> |
await(blockerStarted); |
2049 |
|
for (Future<?> future : futures) future.cancel(false); |
2050 |
|
for (Future<?> future : futures) { |
2051 |
|
try { |
2059 |
|
assertTrue(future.isCancelled()); |
2060 |
|
assertTrue(future.isDone()); |
2061 |
|
} |
2061 |
– |
done.countDown(); |
2062 |
|
} |
2063 |
|
} |
2064 |
|
|