10 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
|
import java.util.ArrayList; |
13 |
+ |
import java.util.Collection; |
14 |
+ |
import java.util.Collections; |
15 |
|
import java.util.List; |
16 |
|
import java.util.concurrent.ArrayBlockingQueue; |
17 |
|
import java.util.concurrent.BlockingQueue; |
19 |
|
import java.util.concurrent.CancellationException; |
20 |
|
import java.util.concurrent.CountDownLatch; |
21 |
|
import java.util.concurrent.ExecutionException; |
20 |
– |
import java.util.concurrent.Executors; |
22 |
|
import java.util.concurrent.ExecutorService; |
23 |
|
import java.util.concurrent.Future; |
24 |
|
import java.util.concurrent.FutureTask; |
25 |
|
import java.util.concurrent.LinkedBlockingQueue; |
25 |
– |
import java.util.concurrent.RejectedExecutionException; |
26 |
|
import java.util.concurrent.RejectedExecutionHandler; |
27 |
|
import java.util.concurrent.RunnableFuture; |
28 |
|
import java.util.concurrent.SynchronousQueue; |
29 |
|
import java.util.concurrent.ThreadFactory; |
30 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
31 |
|
import java.util.concurrent.ThreadPoolExecutor; |
32 |
|
import java.util.concurrent.TimeoutException; |
33 |
|
import java.util.concurrent.TimeUnit; |
240 |
|
final Runnable task = new CheckedRunnable() { |
241 |
|
public void realRun() { done.countDown(); }}; |
242 |
|
p.execute(task); |
243 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
243 |
> |
await(done); |
244 |
|
} |
245 |
|
} |
246 |
|
|
249 |
|
* thread becomes active |
250 |
|
*/ |
251 |
|
public void testGetActiveCount() throws InterruptedException { |
252 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
253 |
|
final ThreadPoolExecutor p = |
254 |
|
new CustomTPE(2, 2, |
255 |
|
LONG_DELAY_MS, MILLISECONDS, |
256 |
|
new ArrayBlockingQueue<Runnable>(10)); |
257 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
257 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
258 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
257 |
– |
final CountDownLatch done = new CountDownLatch(1); |
259 |
|
assertEquals(0, p.getActiveCount()); |
260 |
|
p.execute(new CheckedRunnable() { |
261 |
|
public void realRun() throws InterruptedException { |
262 |
|
threadStarted.countDown(); |
263 |
|
assertEquals(1, p.getActiveCount()); |
264 |
< |
done.await(); |
264 |
> |
await(done); |
265 |
|
}}); |
266 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
266 |
> |
await(threadStarted); |
267 |
|
assertEquals(1, p.getActiveCount()); |
267 |
– |
done.countDown(); |
268 |
|
} |
269 |
|
} |
270 |
|
|
334 |
|
public void realRun() throws InterruptedException { |
335 |
|
threadStarted.countDown(); |
336 |
|
assertEquals(0, p.getCompletedTaskCount()); |
337 |
< |
threadProceed.await(); |
337 |
> |
await(threadProceed); |
338 |
|
threadDone.countDown(); |
339 |
|
}}); |
340 |
|
await(threadStarted); |
341 |
|
assertEquals(0, p.getCompletedTaskCount()); |
342 |
|
threadProceed.countDown(); |
343 |
< |
threadDone.await(); |
343 |
> |
await(threadDone); |
344 |
|
long startTime = System.nanoTime(); |
345 |
|
while (p.getCompletedTaskCount() != 1) { |
346 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
488 |
|
p.execute(new CheckedRunnable() { |
489 |
|
public void realRun() throws InterruptedException { |
490 |
|
threadsStarted.countDown(); |
491 |
< |
done.await(); |
491 |
> |
await(done); |
492 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
493 |
|
}}); |
494 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
494 |
> |
await(threadsStarted); |
495 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
496 |
|
} |
497 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
532 |
|
public void realRun() throws InterruptedException { |
533 |
|
threadStarted.countDown(); |
534 |
|
assertEquals(1, p.getPoolSize()); |
535 |
< |
done.await(); |
535 |
> |
await(done); |
536 |
|
}}); |
537 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
537 |
> |
await(threadStarted); |
538 |
|
assertEquals(1, p.getPoolSize()); |
539 |
|
} |
540 |
|
} |
556 |
|
p.execute(new CheckedRunnable() { |
557 |
|
public void realRun() throws InterruptedException { |
558 |
|
threadStarted.countDown(); |
559 |
< |
done.await(); |
559 |
> |
await(done); |
560 |
|
}}); |
561 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
561 |
> |
await(threadStarted); |
562 |
|
assertEquals(1, p.getTaskCount()); |
563 |
|
assertEquals(0, p.getCompletedTaskCount()); |
564 |
|
for (int i = 0; i < TASKS; i++) { |
567 |
|
public void realRun() throws InterruptedException { |
568 |
|
threadStarted.countDown(); |
569 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
570 |
< |
done.await(); |
570 |
> |
await(done); |
571 |
|
}}); |
572 |
|
} |
573 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
608 |
|
public void realRun() throws InterruptedException { |
609 |
|
assertFalse(p.isTerminating()); |
610 |
|
threadStarted.countDown(); |
611 |
< |
done.await(); |
611 |
> |
await(done); |
612 |
|
}}); |
613 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
613 |
> |
await(threadStarted); |
614 |
|
assertFalse(p.isTerminating()); |
615 |
|
done.countDown(); |
616 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
636 |
|
public void realRun() throws InterruptedException { |
637 |
|
assertFalse(p.isTerminating()); |
638 |
|
threadStarted.countDown(); |
639 |
< |
done.await(); |
639 |
> |
await(done); |
640 |
|
}}); |
641 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
641 |
> |
await(threadStarted); |
642 |
|
assertFalse(p.isTerminating()); |
643 |
|
done.countDown(); |
644 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
652 |
|
* getQueue returns the work queue, which contains queued tasks |
653 |
|
*/ |
654 |
|
public void testGetQueue() throws InterruptedException { |
655 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
655 |
> |
final CountDownLatch done = new CountDownLatch(1); |
656 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
657 |
|
final ThreadPoolExecutor p = |
658 |
|
new CustomTPE(1, 1, |
659 |
|
LONG_DELAY_MS, MILLISECONDS, |
660 |
|
q); |
661 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
661 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
662 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
662 |
– |
final CountDownLatch done = new CountDownLatch(1); |
663 |
|
FutureTask[] tasks = new FutureTask[5]; |
664 |
|
for (int i = 0; i < tasks.length; i++) { |
665 |
< |
Callable task = new CheckedCallable<Boolean>() { |
665 |
> |
Callable<Boolean> task = new CheckedCallable<Boolean>() { |
666 |
|
public Boolean realCall() throws InterruptedException { |
667 |
|
threadStarted.countDown(); |
668 |
|
assertSame(q, p.getQueue()); |
669 |
< |
done.await(); |
669 |
> |
await(done); |
670 |
|
return Boolean.TRUE; |
671 |
|
}}; |
672 |
|
tasks[i] = new FutureTask(task); |
673 |
|
p.execute(tasks[i]); |
674 |
|
} |
675 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
675 |
> |
await(threadStarted); |
676 |
|
assertSame(q, p.getQueue()); |
677 |
|
assertFalse(q.contains(tasks[0])); |
678 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
679 |
|
assertEquals(tasks.length - 1, q.size()); |
680 |
– |
done.countDown(); |
680 |
|
} |
681 |
|
} |
682 |
|
|
684 |
|
* remove(task) removes queued task, and fails to remove active task |
685 |
|
*/ |
686 |
|
public void testRemove() throws InterruptedException { |
687 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
687 |
> |
final CountDownLatch done = new CountDownLatch(1); |
688 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
689 |
|
final ThreadPoolExecutor p = |
690 |
|
new CustomTPE(1, 1, |
691 |
|
LONG_DELAY_MS, MILLISECONDS, |
692 |
|
q); |
693 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
693 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
694 |
|
Runnable[] tasks = new Runnable[6]; |
695 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
696 |
– |
final CountDownLatch done = new CountDownLatch(1); |
696 |
|
for (int i = 0; i < tasks.length; i++) { |
697 |
|
tasks[i] = new CheckedRunnable() { |
698 |
|
public void realRun() throws InterruptedException { |
699 |
|
threadStarted.countDown(); |
700 |
< |
done.await(); |
700 |
> |
await(done); |
701 |
|
}}; |
702 |
|
p.execute(tasks[i]); |
703 |
|
} |
704 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
704 |
> |
await(threadStarted); |
705 |
|
assertFalse(p.remove(tasks[0])); |
706 |
|
assertTrue(q.contains(tasks[4])); |
707 |
|
assertTrue(q.contains(tasks[3])); |
711 |
|
assertTrue(q.contains(tasks[3])); |
712 |
|
assertTrue(p.remove(tasks[3])); |
713 |
|
assertFalse(q.contains(tasks[3])); |
715 |
– |
done.countDown(); |
714 |
|
} |
715 |
|
} |
716 |
|
|
720 |
|
public void testPurge() throws InterruptedException { |
721 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
722 |
|
final CountDownLatch done = new CountDownLatch(1); |
723 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
723 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
724 |
|
final ThreadPoolExecutor p = |
725 |
|
new CustomTPE(1, 1, |
726 |
|
LONG_DELAY_MS, MILLISECONDS, |
728 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
729 |
|
FutureTask[] tasks = new FutureTask[5]; |
730 |
|
for (int i = 0; i < tasks.length; i++) { |
731 |
< |
Callable task = new CheckedCallable<Boolean>() { |
731 |
> |
Callable<Boolean> task = new CheckedCallable<Boolean>() { |
732 |
|
public Boolean realCall() throws InterruptedException { |
733 |
|
threadStarted.countDown(); |
734 |
< |
done.await(); |
734 |
> |
await(done); |
735 |
|
return Boolean.TRUE; |
736 |
|
}}; |
737 |
|
tasks[i] = new FutureTask(task); |
738 |
|
p.execute(tasks[i]); |
739 |
|
} |
740 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
740 |
> |
await(threadStarted); |
741 |
|
assertEquals(tasks.length, p.getTaskCount()); |
742 |
|
assertEquals(tasks.length - 1, q.size()); |
743 |
|
assertEquals(1L, p.getActiveCount()); |
775 |
|
}}; |
776 |
|
for (int i = 0; i < count; i++) |
777 |
|
p.execute(waiter); |
778 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
778 |
> |
await(threadsStarted); |
779 |
|
assertEquals(poolSize, p.getActiveCount()); |
780 |
|
assertEquals(0, p.getCompletedTaskCount()); |
781 |
|
final List<Runnable> queuedTasks; |
1131 |
|
} |
1132 |
|
|
1133 |
|
/** |
1134 |
< |
* execute throws RejectedExecutionException if saturated. |
1134 |
> |
* Submitted tasks are rejected when saturated or shutdown |
1135 |
|
*/ |
1136 |
< |
public void testSaturatedExecute() { |
1136 |
> |
public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException { |
1137 |
|
final ThreadPoolExecutor p = |
1138 |
|
new CustomTPE(1, 1, |
1139 |
|
LONG_DELAY_MS, MILLISECONDS, |
1140 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1141 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1142 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1143 |
< |
Runnable task = new CheckedRunnable() { |
1144 |
< |
public void realRun() throws InterruptedException { |
1141 |
> |
final int saturatedSize = saturatedSize(p); |
1142 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1143 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize()); |
1144 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1145 |
> |
final Runnable r = () -> { |
1146 |
> |
threadsStarted.countDown(); |
1147 |
> |
for (;;) { |
1148 |
> |
try { |
1149 |
|
done.await(); |
1150 |
< |
}}; |
1151 |
< |
for (int i = 0; i < 2; ++i) |
1152 |
< |
p.execute(task); |
1153 |
< |
for (int i = 0; i < 2; ++i) { |
1150 |
> |
return; |
1151 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1152 |
> |
}}; |
1153 |
> |
final Callable<Boolean> c = () -> { |
1154 |
> |
threadsStarted.countDown(); |
1155 |
> |
for (;;) { |
1156 |
|
try { |
1157 |
< |
p.execute(task); |
1158 |
< |
shouldThrow(); |
1159 |
< |
} catch (RejectedExecutionException success) {} |
1160 |
< |
assertTrue(p.getTaskCount() <= 2); |
1157 |
> |
done.await(); |
1158 |
> |
return Boolean.TRUE; |
1159 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1160 |
> |
}}; |
1161 |
> |
final boolean shutdownNow = rnd.nextBoolean(); |
1162 |
> |
|
1163 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1164 |
> |
// saturate |
1165 |
> |
for (int i = saturatedSize; i--> 0; ) { |
1166 |
> |
switch (rnd.nextInt(4)) { |
1167 |
> |
case 0: p.execute(r); break; |
1168 |
> |
case 1: assertFalse(p.submit(r).isDone()); break; |
1169 |
> |
case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; |
1170 |
> |
case 3: assertFalse(p.submit(c).isDone()); break; |
1171 |
> |
} |
1172 |
|
} |
1158 |
– |
done.countDown(); |
1159 |
– |
} |
1160 |
– |
} |
1173 |
|
|
1174 |
< |
/** |
1175 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1164 |
< |
*/ |
1165 |
< |
public void testSaturatedExecute2() { |
1166 |
< |
final ThreadPoolExecutor p = |
1167 |
< |
new CustomTPE(1, 1, |
1168 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1169 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1170 |
< |
new CustomTPE.CallerRunsPolicy()); |
1171 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1172 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1173 |
< |
Runnable blocker = new CheckedRunnable() { |
1174 |
< |
public void realRun() throws InterruptedException { |
1175 |
< |
done.await(); |
1176 |
< |
}}; |
1177 |
< |
p.execute(blocker); |
1178 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1179 |
< |
for (int i = 0; i < tasks.length; i++) |
1180 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1181 |
< |
for (int i = 0; i < tasks.length; i++) |
1182 |
< |
p.execute(tasks[i]); |
1183 |
< |
for (int i = 1; i < tasks.length; i++) |
1184 |
< |
assertTrue(tasks[i].done); |
1185 |
< |
assertFalse(tasks[0].done); // waiting in queue |
1186 |
< |
done.countDown(); |
1187 |
< |
} |
1188 |
< |
} |
1174 |
> |
await(threadsStarted); |
1175 |
> |
assertTaskSubmissionsAreRejected(p); |
1176 |
|
|
1177 |
< |
/** |
1178 |
< |
* executor using DiscardPolicy drops task if saturated. |
1179 |
< |
*/ |
1180 |
< |
public void testSaturatedExecute3() { |
1181 |
< |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1182 |
< |
for (int i = 0; i < tasks.length; ++i) |
1183 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1197 |
< |
final ThreadPoolExecutor p = |
1198 |
< |
new CustomTPE(1, 1, |
1199 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1200 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1201 |
< |
new CustomTPE.DiscardPolicy()); |
1202 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1203 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1204 |
< |
p.execute(awaiter(done)); |
1177 |
> |
if (shutdownNow) |
1178 |
> |
p.shutdownNow(); |
1179 |
> |
else |
1180 |
> |
p.shutdown(); |
1181 |
> |
// Pool is shutdown, but not yet terminated |
1182 |
> |
assertTaskSubmissionsAreRejected(p); |
1183 |
> |
assertFalse(p.isTerminated()); |
1184 |
|
|
1185 |
< |
for (TrackedNoOpRunnable task : tasks) |
1186 |
< |
p.execute(task); |
1187 |
< |
for (int i = 1; i < tasks.length; i++) |
1188 |
< |
assertFalse(tasks[i].done); |
1210 |
< |
done.countDown(); |
1185 |
> |
done.countDown(); // release blocking tasks |
1186 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
1187 |
> |
|
1188 |
> |
assertTaskSubmissionsAreRejected(p); |
1189 |
|
} |
1190 |
< |
for (int i = 1; i < tasks.length; i++) |
1191 |
< |
assertFalse(tasks[i].done); |
1192 |
< |
assertTrue(tasks[0].done); // was waiting in queue |
1190 |
> |
assertEquals(saturatedSize(p) |
1191 |
> |
- (shutdownNow ? p.getQueue().remainingCapacity() : 0), |
1192 |
> |
p.getCompletedTaskCount()); |
1193 |
|
} |
1194 |
|
|
1195 |
|
/** |
1196 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1197 |
|
*/ |
1198 |
< |
public void testSaturatedExecute4() { |
1198 |
> |
public void testSaturatedExecute_DiscardOldestPolicy() { |
1199 |
|
final CountDownLatch done = new CountDownLatch(1); |
1200 |
|
LatchAwaiter r1 = awaiter(done); |
1201 |
|
LatchAwaiter r2 = awaiter(done); |
1204 |
|
new CustomTPE(1, 1, |
1205 |
|
LONG_DELAY_MS, MILLISECONDS, |
1206 |
|
new ArrayBlockingQueue<Runnable>(1), |
1207 |
< |
new CustomTPE.DiscardOldestPolicy()); |
1208 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
1207 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1208 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1209 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1210 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1211 |
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1215 |
|
p.execute(r3); |
1216 |
|
assertFalse(p.getQueue().contains(r2)); |
1217 |
|
assertTrue(p.getQueue().contains(r3)); |
1240 |
– |
done.countDown(); |
1218 |
|
} |
1219 |
|
assertEquals(LatchAwaiter.DONE, r1.state); |
1220 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1222 |
|
} |
1223 |
|
|
1224 |
|
/** |
1248 |
– |
* execute throws RejectedExecutionException if shutdown |
1249 |
– |
*/ |
1250 |
– |
public void testRejectedExecutionExceptionOnShutdown() { |
1251 |
– |
final ThreadPoolExecutor p = |
1252 |
– |
new CustomTPE(1, 1, |
1253 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1254 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1255 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1256 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1257 |
– |
try { |
1258 |
– |
p.execute(new NoOpRunnable()); |
1259 |
– |
shouldThrow(); |
1260 |
– |
} catch (RejectedExecutionException success) {} |
1261 |
– |
} |
1262 |
– |
} |
1263 |
– |
|
1264 |
– |
/** |
1265 |
– |
* execute using CallerRunsPolicy drops task on shutdown |
1266 |
– |
*/ |
1267 |
– |
public void testCallerRunsOnShutdown() { |
1268 |
– |
final ThreadPoolExecutor p = |
1269 |
– |
new CustomTPE(1, 1, |
1270 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1271 |
– |
new ArrayBlockingQueue<Runnable>(1), |
1272 |
– |
new CustomTPE.CallerRunsPolicy()); |
1273 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1274 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1275 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1276 |
– |
p.execute(r); |
1277 |
– |
assertFalse(r.done); |
1278 |
– |
} |
1279 |
– |
} |
1280 |
– |
|
1281 |
– |
/** |
1282 |
– |
* execute using DiscardPolicy drops task on shutdown |
1283 |
– |
*/ |
1284 |
– |
public void testDiscardOnShutdown() { |
1285 |
– |
final ThreadPoolExecutor p = |
1286 |
– |
new CustomTPE(1, 1, |
1287 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1288 |
– |
new ArrayBlockingQueue<Runnable>(1), |
1289 |
– |
new CustomTPE.DiscardPolicy()); |
1290 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1291 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1292 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1293 |
– |
p.execute(r); |
1294 |
– |
assertFalse(r.done); |
1295 |
– |
} |
1296 |
– |
} |
1297 |
– |
|
1298 |
– |
/** |
1225 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1226 |
|
*/ |
1227 |
|
public void testDiscardOldestOnShutdown() { |
1229 |
|
new CustomTPE(1, 1, |
1230 |
|
LONG_DELAY_MS, MILLISECONDS, |
1231 |
|
new ArrayBlockingQueue<Runnable>(1), |
1232 |
< |
new CustomTPE.DiscardOldestPolicy()); |
1232 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1233 |
|
|
1234 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1235 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1240 |
|
} |
1241 |
|
|
1242 |
|
/** |
1243 |
< |
* execute(null) throws NPE |
1243 |
> |
* Submitting null tasks throws NullPointerException |
1244 |
|
*/ |
1245 |
< |
public void testExecuteNull() { |
1245 |
> |
public void testNullTaskSubmission() { |
1246 |
|
final ThreadPoolExecutor p = |
1247 |
|
new CustomTPE(1, 2, |
1248 |
|
1L, SECONDS, |
1249 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1250 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1251 |
< |
try { |
1326 |
< |
p.execute(null); |
1327 |
< |
shouldThrow(); |
1328 |
< |
} catch (NullPointerException success) {} |
1251 |
> |
assertNullTaskSubmissionThrowsNullPointerException(p); |
1252 |
|
} |
1253 |
|
} |
1254 |
|
|
1292 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1293 |
|
final ThreadPoolExecutor p = |
1294 |
|
new CustomTPE(2, 3, |
1295 |
< |
LONG_DELAY_MS, |
1296 |
< |
MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1295 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1296 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1297 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1298 |
|
try { |
1299 |
|
p.setMaximumPoolSize(-1); |
1395 |
|
} |
1396 |
|
|
1397 |
|
/** |
1398 |
< |
* invokeAny(null) throws NPE |
1398 |
> |
* invokeAny(null) throws NullPointerException |
1399 |
|
*/ |
1400 |
|
public void testInvokeAny1() throws Exception { |
1401 |
|
final ExecutorService e = |
1411 |
|
} |
1412 |
|
|
1413 |
|
/** |
1414 |
< |
* invokeAny(empty collection) throws IAE |
1414 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
1415 |
|
*/ |
1416 |
|
public void testInvokeAny2() throws Exception { |
1417 |
|
final ExecutorService e = |
1436 |
|
LONG_DELAY_MS, MILLISECONDS, |
1437 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1438 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1439 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1439 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1440 |
|
l.add(latchAwaitingStringTask(latch)); |
1441 |
|
l.add(null); |
1442 |
|
try { |
1456 |
|
LONG_DELAY_MS, MILLISECONDS, |
1457 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1458 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1459 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1459 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1460 |
|
l.add(new NPETask()); |
1461 |
|
try { |
1462 |
|
e.invokeAny(l); |
1476 |
|
LONG_DELAY_MS, MILLISECONDS, |
1477 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1478 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1479 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1479 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1480 |
|
l.add(new StringTask()); |
1481 |
|
l.add(new StringTask()); |
1482 |
|
String result = e.invokeAny(l); |
1501 |
|
} |
1502 |
|
|
1503 |
|
/** |
1504 |
< |
* invokeAll(empty collection) returns empty collection |
1504 |
> |
* invokeAll(empty collection) returns empty list |
1505 |
|
*/ |
1506 |
|
public void testInvokeAll2() throws Exception { |
1507 |
|
final ExecutorService e = |
1508 |
|
new CustomTPE(2, 2, |
1509 |
|
LONG_DELAY_MS, MILLISECONDS, |
1510 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1511 |
+ |
final Collection<Callable<String>> emptyCollection |
1512 |
+ |
= Collections.emptyList(); |
1513 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1514 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1514 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1515 |
|
assertTrue(r.isEmpty()); |
1516 |
|
} |
1517 |
|
} |
1525 |
|
LONG_DELAY_MS, MILLISECONDS, |
1526 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1527 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1528 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1528 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1529 |
|
l.add(new StringTask()); |
1530 |
|
l.add(null); |
1531 |
|
try { |
1544 |
|
LONG_DELAY_MS, MILLISECONDS, |
1545 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1546 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1547 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1547 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1548 |
|
l.add(new NPETask()); |
1549 |
|
List<Future<String>> futures = e.invokeAll(l); |
1550 |
|
assertEquals(1, futures.size()); |
1566 |
|
LONG_DELAY_MS, MILLISECONDS, |
1567 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1568 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1569 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1569 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1570 |
|
l.add(new StringTask()); |
1571 |
|
l.add(new StringTask()); |
1572 |
|
List<Future<String>> futures = e.invokeAll(l); |
1586 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1587 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1588 |
|
try { |
1589 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1589 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1590 |
|
shouldThrow(); |
1591 |
|
} catch (NullPointerException success) {} |
1592 |
|
} |
1601 |
|
LONG_DELAY_MS, MILLISECONDS, |
1602 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1603 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1604 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1604 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1605 |
|
l.add(new StringTask()); |
1606 |
|
try { |
1607 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1607 |
> |
e.invokeAny(l, randomTimeout(), null); |
1608 |
|
shouldThrow(); |
1609 |
|
} catch (NullPointerException success) {} |
1610 |
|
} |
1611 |
|
} |
1612 |
|
|
1613 |
|
/** |
1614 |
< |
* timed invokeAny(empty collection) throws IAE |
1614 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1615 |
|
*/ |
1616 |
|
public void testTimedInvokeAny2() throws Exception { |
1617 |
|
final ExecutorService e = |
1618 |
|
new CustomTPE(2, 2, |
1619 |
|
LONG_DELAY_MS, MILLISECONDS, |
1620 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1621 |
+ |
final Collection<Callable<String>> emptyCollection |
1622 |
+ |
= Collections.emptyList(); |
1623 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1624 |
|
try { |
1625 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), |
1699 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1625 |
> |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
1626 |
|
shouldThrow(); |
1627 |
|
} catch (IllegalArgumentException success) {} |
1628 |
|
} |
1638 |
|
LONG_DELAY_MS, MILLISECONDS, |
1639 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1640 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1641 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1641 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1642 |
|
l.add(latchAwaitingStringTask(latch)); |
1643 |
|
l.add(null); |
1644 |
|
try { |
1645 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1645 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
1646 |
|
shouldThrow(); |
1647 |
|
} catch (NullPointerException success) {} |
1648 |
|
latch.countDown(); |
1658 |
|
LONG_DELAY_MS, MILLISECONDS, |
1659 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1660 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1661 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1661 |
> |
long startTime = System.nanoTime(); |
1662 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1663 |
|
l.add(new NPETask()); |
1664 |
|
try { |
1665 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1665 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1666 |
|
shouldThrow(); |
1667 |
|
} catch (ExecutionException success) { |
1668 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1669 |
|
} |
1670 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1671 |
|
} |
1672 |
|
} |
1673 |
|
|
1680 |
|
LONG_DELAY_MS, MILLISECONDS, |
1681 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1682 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1683 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1683 |
> |
long startTime = System.nanoTime(); |
1684 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1685 |
|
l.add(new StringTask()); |
1686 |
|
l.add(new StringTask()); |
1687 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1687 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1688 |
|
assertSame(TEST_STRING, result); |
1689 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1690 |
|
} |
1691 |
|
} |
1692 |
|
|
1700 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1701 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1702 |
|
try { |
1703 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1703 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1704 |
|
shouldThrow(); |
1705 |
|
} catch (NullPointerException success) {} |
1706 |
|
} |
1715 |
|
LONG_DELAY_MS, MILLISECONDS, |
1716 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1717 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1718 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1718 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1719 |
|
l.add(new StringTask()); |
1720 |
|
try { |
1721 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1721 |
> |
e.invokeAll(l, randomTimeout(), null); |
1722 |
|
shouldThrow(); |
1723 |
|
} catch (NullPointerException success) {} |
1724 |
|
} |
1725 |
|
} |
1726 |
|
|
1727 |
|
/** |
1728 |
< |
* timed invokeAll(empty collection) returns empty collection |
1728 |
> |
* timed invokeAll(empty collection) returns empty list |
1729 |
|
*/ |
1730 |
|
public void testTimedInvokeAll2() throws Exception { |
1731 |
|
final ExecutorService e = |
1732 |
|
new CustomTPE(2, 2, |
1733 |
|
LONG_DELAY_MS, MILLISECONDS, |
1734 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1735 |
+ |
final Collection<Callable<String>> emptyCollection |
1736 |
+ |
= Collections.emptyList(); |
1737 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1738 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1739 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1738 |
> |
List<Future<String>> r = |
1739 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1740 |
|
assertTrue(r.isEmpty()); |
1741 |
|
} |
1742 |
|
} |
1750 |
|
LONG_DELAY_MS, MILLISECONDS, |
1751 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1752 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1753 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1753 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1754 |
|
l.add(new StringTask()); |
1755 |
|
l.add(null); |
1756 |
|
try { |
1757 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1757 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1758 |
|
shouldThrow(); |
1759 |
|
} catch (NullPointerException success) {} |
1760 |
|
} |
1769 |
|
LONG_DELAY_MS, MILLISECONDS, |
1770 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1771 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1772 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1772 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1773 |
|
l.add(new NPETask()); |
1774 |
|
List<Future<String>> futures = |
1775 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1775 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1776 |
|
assertEquals(1, futures.size()); |
1777 |
|
try { |
1778 |
|
futures.get(0).get(); |
1792 |
|
LONG_DELAY_MS, MILLISECONDS, |
1793 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1794 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1795 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1795 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1796 |
|
l.add(new StringTask()); |
1797 |
|
l.add(new StringTask()); |
1798 |
|
List<Future<String>> futures = |
1807 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1808 |
|
*/ |
1809 |
|
public void testTimedInvokeAll6() throws Exception { |
1810 |
< |
final ExecutorService e = |
1811 |
< |
new CustomTPE(2, 2, |
1812 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1813 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1814 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1815 |
< |
for (long timeout = timeoutMillis();;) { |
1810 |
> |
for (long timeout = timeoutMillis();;) { |
1811 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1812 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1813 |
> |
public String realCall() { |
1814 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1815 |
> |
catch (InterruptedException ok) {} |
1816 |
> |
return "1"; }}; |
1817 |
> |
final ExecutorService p = |
1818 |
> |
new CustomTPE(2, 2, |
1819 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1820 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1821 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1822 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1823 |
|
tasks.add(new StringTask("0")); |
1824 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1824 |
> |
tasks.add(waiter); |
1825 |
|
tasks.add(new StringTask("2")); |
1826 |
|
long startTime = System.nanoTime(); |
1827 |
|
List<Future<String>> futures = |
1828 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1828 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1829 |
|
assertEquals(tasks.size(), futures.size()); |
1830 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1831 |
|
for (Future future : futures) |
1862 |
|
public void realRun() { |
1863 |
|
done.countDown(); |
1864 |
|
}}); |
1865 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1865 |
> |
await(done); |
1866 |
|
} |
1867 |
|
} |
1868 |
|
|
1934 |
|
* (in part, a test of CustomTPE itself) |
1935 |
|
*/ |
1936 |
|
public void testGet_cancelled() throws Exception { |
1937 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1938 |
|
final ExecutorService e = |
1939 |
|
new CustomTPE(1, 1, |
1940 |
|
LONG_DELAY_MS, MILLISECONDS, |
1941 |
|
new LinkedBlockingQueue<Runnable>()); |
1942 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1942 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
1943 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2005 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1944 |
|
final List<Future<?>> futures = new ArrayList<>(); |
1945 |
|
for (int i = 0; i < 2; i++) { |
1946 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
1950 |
|
}}; |
1951 |
|
futures.add(e.submit(r)); |
1952 |
|
} |
1953 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1953 |
> |
await(blockerStarted); |
1954 |
|
for (Future<?> future : futures) future.cancel(false); |
1955 |
|
for (Future<?> future : futures) { |
1956 |
|
try { |
1964 |
|
assertTrue(future.isCancelled()); |
1965 |
|
assertTrue(future.isDone()); |
1966 |
|
} |
2029 |
– |
done.countDown(); |
1967 |
|
} |
1968 |
|
} |
1969 |
|
|
1970 |
+ |
public void testFinalizeMethodCallsSuperFinalize() { |
1971 |
+ |
new CustomTPE(1, 1, |
1972 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1973 |
+ |
new LinkedBlockingQueue<Runnable>()) { |
1974 |
+ |
|
1975 |
+ |
/** |
1976 |
+ |
* A finalize method without "throws Throwable", that |
1977 |
+ |
* calls super.finalize(). |
1978 |
+ |
*/ |
1979 |
+ |
protected void finalize() { |
1980 |
+ |
super.finalize(); |
1981 |
+ |
} |
1982 |
+ |
}.shutdown(); |
1983 |
+ |
} |
1984 |
+ |
|
1985 |
|
} |