115 |
|
assertEquals(1, p.getActiveCount()); |
116 |
|
done.await(); |
117 |
|
}}); |
118 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
118 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
119 |
|
assertEquals(1, p.getActiveCount()); |
120 |
|
done.countDown(); |
121 |
|
} |
329 |
|
*/ |
330 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
331 |
|
final int THREADS = 3; |
332 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
333 |
|
final ThreadPoolExecutor p = |
334 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
335 |
|
LONG_DELAY_MS, MILLISECONDS, |
336 |
|
new ArrayBlockingQueue<Runnable>(10)); |
337 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
338 |
< |
final CountDownLatch done = new CountDownLatch(1); |
337 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
338 |
|
assertEquals(0, p.getLargestPoolSize()); |
339 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
340 |
|
for (int i = 0; i < THREADS; i++) |
341 |
|
p.execute(new CheckedRunnable() { |
342 |
|
public void realRun() throws InterruptedException { |
344 |
|
done.await(); |
345 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
346 |
|
}}); |
347 |
< |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
347 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
348 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
349 |
– |
done.countDown(); // release pool |
349 |
|
} |
350 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
351 |
|
} |
373 |
|
* become active |
374 |
|
*/ |
375 |
|
public void testGetPoolSize() throws InterruptedException { |
376 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
377 |
|
final ThreadPoolExecutor p = |
378 |
|
new ThreadPoolExecutor(1, 1, |
379 |
|
LONG_DELAY_MS, MILLISECONDS, |
380 |
|
new ArrayBlockingQueue<Runnable>(10)); |
381 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
382 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
383 |
< |
final CountDownLatch done = new CountDownLatch(1); |
381 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
382 |
|
assertEquals(0, p.getPoolSize()); |
383 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
384 |
|
p.execute(new CheckedRunnable() { |
385 |
|
public void realRun() throws InterruptedException { |
386 |
|
threadStarted.countDown(); |
387 |
|
assertEquals(1, p.getPoolSize()); |
388 |
|
done.await(); |
389 |
|
}}); |
390 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
390 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
391 |
|
assertEquals(1, p.getPoolSize()); |
393 |
– |
done.countDown(); // release pool |
392 |
|
} |
393 |
|
} |
394 |
|
|
396 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
397 |
|
*/ |
398 |
|
public void testGetTaskCount() throws InterruptedException { |
399 |
+ |
final int TASKS = 3; |
400 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
401 |
|
final ThreadPoolExecutor p = |
402 |
|
new ThreadPoolExecutor(1, 1, |
403 |
|
LONG_DELAY_MS, MILLISECONDS, |
404 |
|
new ArrayBlockingQueue<Runnable>(10)); |
405 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
405 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
406 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
407 |
– |
final CountDownLatch done = new CountDownLatch(1); |
407 |
|
assertEquals(0, p.getTaskCount()); |
408 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
409 |
|
p.execute(new CheckedRunnable() { |
410 |
|
public void realRun() throws InterruptedException { |
411 |
|
threadStarted.countDown(); |
412 |
– |
assertEquals(1, p.getTaskCount()); |
412 |
|
done.await(); |
413 |
|
}}); |
414 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
414 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
415 |
|
assertEquals(1, p.getTaskCount()); |
416 |
< |
done.countDown(); |
416 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
417 |
> |
for (int i = 0; i < TASKS; i++) { |
418 |
> |
assertEquals(1 + i, p.getTaskCount()); |
419 |
> |
p.execute(new CheckedRunnable() { |
420 |
> |
public void realRun() throws InterruptedException { |
421 |
> |
threadStarted.countDown(); |
422 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
423 |
> |
done.await(); |
424 |
> |
}}); |
425 |
> |
} |
426 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
427 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
428 |
|
} |
429 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
430 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
431 |
|
} |
432 |
|
|
433 |
|
/** |
495 |
|
threadStarted.countDown(); |
496 |
|
done.await(); |
497 |
|
}}); |
498 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
498 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
499 |
|
assertFalse(p.isTerminating()); |
500 |
|
done.countDown(); |
501 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
523 |
|
threadStarted.countDown(); |
524 |
|
done.await(); |
525 |
|
}}); |
526 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
526 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
527 |
|
assertFalse(p.isTerminating()); |
528 |
|
done.countDown(); |
529 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
557 |
|
tasks[i] = new FutureTask(task); |
558 |
|
p.execute(tasks[i]); |
559 |
|
} |
560 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
560 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
561 |
|
assertSame(q, p.getQueue()); |
562 |
|
assertFalse(q.contains(tasks[0])); |
563 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
587 |
|
}}; |
588 |
|
p.execute(tasks[i]); |
589 |
|
} |
590 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
590 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
591 |
|
assertFalse(p.remove(tasks[0])); |
592 |
|
assertTrue(q.contains(tasks[4])); |
593 |
|
assertTrue(q.contains(tasks[3])); |
612 |
|
new ThreadPoolExecutor(1, 1, |
613 |
|
LONG_DELAY_MS, MILLISECONDS, |
614 |
|
q); |
615 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
615 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
616 |
|
FutureTask[] tasks = new FutureTask[5]; |
617 |
|
for (int i = 0; i < tasks.length; i++) { |
618 |
|
Callable task = new CheckedCallable<Boolean>() { |
624 |
|
tasks[i] = new FutureTask(task); |
625 |
|
p.execute(tasks[i]); |
626 |
|
} |
627 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
627 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
628 |
|
assertEquals(tasks.length, p.getTaskCount()); |
629 |
|
assertEquals(tasks.length - 1, q.size()); |
630 |
|
assertEquals(1L, p.getActiveCount()); |
637 |
|
p.purge(); // Nothing to do |
638 |
|
assertEquals(tasks.length - 3, q.size()); |
639 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
628 |
– |
done.countDown(); |
640 |
|
} |
641 |
|
} |
642 |
|
|
652 |
|
new ThreadPoolExecutor(poolSize, poolSize, |
653 |
|
LONG_DELAY_MS, MILLISECONDS, |
654 |
|
new ArrayBlockingQueue<Runnable>(10)); |
655 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
655 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
656 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
657 |
|
threadsStarted.countDown(); |
658 |
|
try { |
1029 |
|
60, SECONDS, |
1030 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1031 |
|
|
1032 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1033 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1034 |
< |
try { |
1032 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1033 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1034 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1035 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1036 |
|
public void realRun() throws Exception { |
1037 |
|
Callable task = new CheckedCallable<Boolean>() { |
1043 |
|
p.submit(task).get(); |
1044 |
|
}}); |
1045 |
|
|
1046 |
< |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
1046 |
> |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1047 |
|
t.interrupt(); |
1048 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1038 |
– |
} finally { |
1049 |
|
done.countDown(); |
1040 |
– |
joinPool(p); |
1050 |
|
} |
1051 |
|
} |
1052 |
|
|
1054 |
|
* execute throws RejectedExecutionException if saturated. |
1055 |
|
*/ |
1056 |
|
public void testSaturatedExecute() { |
1057 |
< |
ThreadPoolExecutor p = |
1057 |
> |
final ThreadPoolExecutor p = |
1058 |
|
new ThreadPoolExecutor(1, 1, |
1059 |
|
LONG_DELAY_MS, MILLISECONDS, |
1060 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1061 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1062 |
< |
try { |
1061 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1062 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1063 |
|
Runnable task = new CheckedRunnable() { |
1064 |
|
public void realRun() throws InterruptedException { |
1065 |
|
done.await(); |
1073 |
|
} catch (RejectedExecutionException success) {} |
1074 |
|
assertTrue(p.getTaskCount() <= 2); |
1075 |
|
} |
1067 |
– |
} finally { |
1076 |
|
done.countDown(); |
1069 |
– |
joinPool(p); |
1077 |
|
} |
1078 |
|
} |
1079 |
|
|
1081 |
|
* submit(runnable) throws RejectedExecutionException if saturated. |
1082 |
|
*/ |
1083 |
|
public void testSaturatedSubmitRunnable() { |
1084 |
< |
ThreadPoolExecutor p = |
1084 |
> |
final ThreadPoolExecutor p = |
1085 |
|
new ThreadPoolExecutor(1, 1, |
1086 |
|
LONG_DELAY_MS, MILLISECONDS, |
1087 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1088 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1089 |
< |
try { |
1088 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1089 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1090 |
|
Runnable task = new CheckedRunnable() { |
1091 |
|
public void realRun() throws InterruptedException { |
1092 |
|
done.await(); |
1100 |
|
} catch (RejectedExecutionException success) {} |
1101 |
|
assertTrue(p.getTaskCount() <= 2); |
1102 |
|
} |
1096 |
– |
} finally { |
1103 |
|
done.countDown(); |
1098 |
– |
joinPool(p); |
1104 |
|
} |
1105 |
|
} |
1106 |
|
|
1108 |
|
* submit(callable) throws RejectedExecutionException if saturated. |
1109 |
|
*/ |
1110 |
|
public void testSaturatedSubmitCallable() { |
1111 |
< |
ThreadPoolExecutor p = |
1111 |
> |
final ThreadPoolExecutor p = |
1112 |
|
new ThreadPoolExecutor(1, 1, |
1113 |
|
LONG_DELAY_MS, MILLISECONDS, |
1114 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1115 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1116 |
< |
try { |
1115 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1116 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1117 |
|
Runnable task = new CheckedRunnable() { |
1118 |
|
public void realRun() throws InterruptedException { |
1119 |
|
done.await(); |
1127 |
|
} catch (RejectedExecutionException success) {} |
1128 |
|
assertTrue(p.getTaskCount() <= 2); |
1129 |
|
} |
1125 |
– |
} finally { |
1130 |
|
done.countDown(); |
1127 |
– |
joinPool(p); |
1131 |
|
} |
1132 |
|
} |
1133 |
|
|
1135 |
|
* executor using CallerRunsPolicy runs task if saturated. |
1136 |
|
*/ |
1137 |
|
public void testSaturatedExecute2() { |
1135 |
– |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1138 |
|
final ThreadPoolExecutor p = |
1139 |
|
new ThreadPoolExecutor(1, 1, |
1140 |
|
LONG_DELAY_MS, |
1141 |
|
MILLISECONDS, |
1142 |
|
new ArrayBlockingQueue<Runnable>(1), |
1143 |
< |
h); |
1144 |
< |
try { |
1143 |
> |
new ThreadPoolExecutor.CallerRunsPolicy()); |
1144 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1145 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1146 |
> |
Runnable blocker = new CheckedRunnable() { |
1147 |
> |
public void realRun() throws InterruptedException { |
1148 |
> |
done.await(); |
1149 |
> |
}}; |
1150 |
> |
p.execute(blocker); |
1151 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1152 |
< |
for (int i = 0; i < tasks.length; ++i) |
1152 |
> |
for (int i = 0; i < tasks.length; i++) |
1153 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1154 |
< |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1147 |
< |
p.execute(mr); |
1148 |
< |
for (int i = 0; i < tasks.length; ++i) |
1154 |
> |
for (int i = 0; i < tasks.length; i++) |
1155 |
|
p.execute(tasks[i]); |
1156 |
< |
for (int i = 1; i < tasks.length; ++i) |
1156 |
> |
for (int i = 1; i < tasks.length; i++) |
1157 |
|
assertTrue(tasks[i].done); |
1158 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1159 |
< |
} finally { |
1154 |
< |
joinPool(p); |
1158 |
> |
assertFalse(tasks[0].done); // waiting in queue |
1159 |
> |
done.countDown(); |
1160 |
|
} |
1161 |
|
} |
1162 |
|
|
1164 |
|
* executor using DiscardPolicy drops task if saturated. |
1165 |
|
*/ |
1166 |
|
public void testSaturatedExecute3() { |
1167 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1167 |
> |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1168 |
> |
for (int i = 0; i < tasks.length; ++i) |
1169 |
> |
tasks[i] = new TrackedNoOpRunnable(); |
1170 |
|
final ThreadPoolExecutor p = |
1171 |
|
new ThreadPoolExecutor(1, 1, |
1172 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1173 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1174 |
< |
h); |
1175 |
< |
try { |
1176 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1177 |
< |
for (int i = 0; i < tasks.length; ++i) |
1178 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1172 |
< |
p.execute(new TrackedLongRunnable()); |
1172 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1173 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1174 |
> |
new ThreadPoolExecutor.DiscardPolicy()); |
1175 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1176 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1177 |
> |
p.execute(awaiter(done)); |
1178 |
> |
|
1179 |
|
for (TrackedNoOpRunnable task : tasks) |
1180 |
|
p.execute(task); |
1181 |
< |
for (TrackedNoOpRunnable task : tasks) |
1182 |
< |
assertFalse(task.done); |
1183 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1178 |
< |
} finally { |
1179 |
< |
joinPool(p); |
1181 |
> |
for (int i = 1; i < tasks.length; i++) |
1182 |
> |
assertFalse(tasks[i].done); |
1183 |
> |
done.countDown(); |
1184 |
|
} |
1185 |
+ |
for (int i = 1; i < tasks.length; i++) |
1186 |
+ |
assertFalse(tasks[i].done); |
1187 |
+ |
assertTrue(tasks[0].done); // was waiting in queue |
1188 |
|
} |
1189 |
|
|
1190 |
|
/** |
1191 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1192 |
|
*/ |
1193 |
|
public void testSaturatedExecute4() { |
1194 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1194 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1195 |
> |
LatchAwaiter r1 = awaiter(done); |
1196 |
> |
LatchAwaiter r2 = awaiter(done); |
1197 |
> |
LatchAwaiter r3 = awaiter(done); |
1198 |
|
final ThreadPoolExecutor p = |
1199 |
|
new ThreadPoolExecutor(1, 1, |
1200 |
|
LONG_DELAY_MS, MILLISECONDS, |
1201 |
|
new ArrayBlockingQueue<Runnable>(1), |
1202 |
< |
h); |
1203 |
< |
try { |
1204 |
< |
p.execute(new TrackedLongRunnable()); |
1205 |
< |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1202 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1203 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1204 |
> |
assertEquals(LatchAwaiter.NEW, r1.state); |
1205 |
> |
assertEquals(LatchAwaiter.NEW, r2.state); |
1206 |
> |
assertEquals(LatchAwaiter.NEW, r3.state); |
1207 |
> |
p.execute(r1); |
1208 |
|
p.execute(r2); |
1209 |
|
assertTrue(p.getQueue().contains(r2)); |
1198 |
– |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1210 |
|
p.execute(r3); |
1211 |
|
assertFalse(p.getQueue().contains(r2)); |
1212 |
|
assertTrue(p.getQueue().contains(r3)); |
1213 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1203 |
< |
} finally { |
1204 |
< |
joinPool(p); |
1213 |
> |
done.countDown(); |
1214 |
|
} |
1215 |
+ |
assertEquals(LatchAwaiter.DONE, r1.state); |
1216 |
+ |
assertEquals(LatchAwaiter.NEW, r2.state); |
1217 |
+ |
assertEquals(LatchAwaiter.DONE, r3.state); |
1218 |
|
} |
1219 |
|
|
1220 |
|
/** |
1221 |
|
* execute throws RejectedExecutionException if shutdown |
1222 |
|
*/ |
1223 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1224 |
< |
ThreadPoolExecutor p = |
1224 |
> |
final ThreadPoolExecutor p = |
1225 |
|
new ThreadPoolExecutor(1, 1, |
1226 |
|
LONG_DELAY_MS, MILLISECONDS, |
1227 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1228 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1229 |
< |
try { |
1230 |
< |
p.execute(new NoOpRunnable()); |
1231 |
< |
shouldThrow(); |
1232 |
< |
} catch (RejectedExecutionException success) {} |
1233 |
< |
|
1234 |
< |
joinPool(p); |
1229 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1230 |
> |
try { |
1231 |
> |
p.execute(new NoOpRunnable()); |
1232 |
> |
shouldThrow(); |
1233 |
> |
} catch (RejectedExecutionException success) {} |
1234 |
> |
} |
1235 |
|
} |
1236 |
|
|
1237 |
|
/** |
1245 |
|
new ArrayBlockingQueue<Runnable>(1), h); |
1246 |
|
|
1247 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1248 |
< |
try { |
1248 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1249 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1250 |
|
p.execute(r); |
1251 |
|
assertFalse(r.done); |
1240 |
– |
} finally { |
1241 |
– |
joinPool(p); |
1252 |
|
} |
1253 |
|
} |
1254 |
|
|
1256 |
|
* execute using DiscardPolicy drops task on shutdown |
1257 |
|
*/ |
1258 |
|
public void testDiscardOnShutdown() { |
1259 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1250 |
< |
ThreadPoolExecutor p = |
1259 |
> |
final ThreadPoolExecutor p = |
1260 |
|
new ThreadPoolExecutor(1, 1, |
1261 |
|
LONG_DELAY_MS, MILLISECONDS, |
1262 |
|
new ArrayBlockingQueue<Runnable>(1), |
1263 |
< |
h); |
1263 |
> |
new ThreadPoolExecutor.DiscardPolicy()); |
1264 |
|
|
1265 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1266 |
< |
try { |
1266 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1267 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1268 |
|
p.execute(r); |
1269 |
|
assertFalse(r.done); |
1261 |
– |
} finally { |
1262 |
– |
joinPool(p); |
1270 |
|
} |
1271 |
|
} |
1272 |
|
|
1274 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1275 |
|
*/ |
1276 |
|
public void testDiscardOldestOnShutdown() { |
1277 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1271 |
< |
ThreadPoolExecutor p = |
1277 |
> |
final ThreadPoolExecutor p = |
1278 |
|
new ThreadPoolExecutor(1, 1, |
1279 |
|
LONG_DELAY_MS, MILLISECONDS, |
1280 |
|
new ArrayBlockingQueue<Runnable>(1), |
1281 |
< |
h); |
1281 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1282 |
|
|
1283 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1284 |
< |
try { |
1284 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1285 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1286 |
|
p.execute(r); |
1287 |
|
assertFalse(r.done); |
1282 |
– |
} finally { |
1283 |
– |
joinPool(p); |
1288 |
|
} |
1289 |
|
} |
1290 |
|
|
1292 |
|
* execute(null) throws NPE |
1293 |
|
*/ |
1294 |
|
public void testExecuteNull() { |
1295 |
< |
ThreadPoolExecutor p = |
1296 |
< |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1295 |
> |
final ThreadPoolExecutor p = |
1296 |
> |
new ThreadPoolExecutor(1, 2, |
1297 |
> |
1L, SECONDS, |
1298 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1299 |
< |
try { |
1300 |
< |
p.execute(null); |
1301 |
< |
shouldThrow(); |
1302 |
< |
} catch (NullPointerException success) {} |
1303 |
< |
|
1304 |
< |
joinPool(p); |
1299 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1300 |
> |
try { |
1301 |
> |
p.execute(null); |
1302 |
> |
shouldThrow(); |
1303 |
> |
} catch (NullPointerException success) {} |
1304 |
> |
} |
1305 |
|
} |
1306 |
|
|
1307 |
|
/** |
1308 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1309 |
|
*/ |
1310 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1311 |
< |
ThreadPoolExecutor p = |
1311 |
> |
final ThreadPoolExecutor p = |
1312 |
|
new ThreadPoolExecutor(1, 2, |
1313 |
|
LONG_DELAY_MS, MILLISECONDS, |
1314 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1315 |
< |
try { |
1316 |
< |
p.setCorePoolSize(-1); |
1317 |
< |
shouldThrow(); |
1318 |
< |
} catch (IllegalArgumentException success) { |
1319 |
< |
} finally { |
1315 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1315 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1316 |
> |
try { |
1317 |
> |
p.setCorePoolSize(-1); |
1318 |
> |
shouldThrow(); |
1319 |
> |
} catch (IllegalArgumentException success) {} |
1320 |
|
} |
1317 |
– |
joinPool(p); |
1321 |
|
} |
1322 |
|
|
1323 |
|
/** |
1325 |
|
* given a value less the core pool size |
1326 |
|
*/ |
1327 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1328 |
< |
ThreadPoolExecutor p = |
1328 |
> |
final ThreadPoolExecutor p = |
1329 |
|
new ThreadPoolExecutor(2, 3, |
1330 |
|
LONG_DELAY_MS, MILLISECONDS, |
1331 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1332 |
< |
try { |
1333 |
< |
p.setMaximumPoolSize(1); |
1334 |
< |
shouldThrow(); |
1335 |
< |
} catch (IllegalArgumentException success) { |
1336 |
< |
} finally { |
1334 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1332 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1333 |
> |
try { |
1334 |
> |
p.setMaximumPoolSize(1); |
1335 |
> |
shouldThrow(); |
1336 |
> |
} catch (IllegalArgumentException success) {} |
1337 |
|
} |
1336 |
– |
joinPool(p); |
1338 |
|
} |
1339 |
|
|
1340 |
|
/** |
1342 |
|
* if given a negative value |
1343 |
|
*/ |
1344 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1345 |
< |
ThreadPoolExecutor p = |
1345 |
> |
final ThreadPoolExecutor p = |
1346 |
|
new ThreadPoolExecutor(2, 3, |
1347 |
|
LONG_DELAY_MS, MILLISECONDS, |
1348 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1349 |
< |
try { |
1350 |
< |
p.setMaximumPoolSize(-1); |
1351 |
< |
shouldThrow(); |
1352 |
< |
} catch (IllegalArgumentException success) { |
1353 |
< |
} finally { |
1353 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1349 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1350 |
> |
try { |
1351 |
> |
p.setMaximumPoolSize(-1); |
1352 |
> |
shouldThrow(); |
1353 |
> |
} catch (IllegalArgumentException success) {} |
1354 |
|
} |
1355 |
– |
joinPool(p); |
1355 |
|
} |
1356 |
|
|
1357 |
|
/** |
1359 |
|
* max pool size result in IllegalArgumentException. |
1360 |
|
*/ |
1361 |
|
public void testPoolSizeInvariants() { |
1362 |
< |
ThreadPoolExecutor p = |
1362 |
> |
final ThreadPoolExecutor p = |
1363 |
|
new ThreadPoolExecutor(1, 1, |
1364 |
|
LONG_DELAY_MS, MILLISECONDS, |
1365 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1366 |
< |
for (int s = 1; s < 5; s++) { |
1367 |
< |
p.setMaximumPoolSize(s); |
1368 |
< |
p.setCorePoolSize(s); |
1369 |
< |
try { |
1370 |
< |
p.setMaximumPoolSize(s - 1); |
1371 |
< |
shouldThrow(); |
1372 |
< |
} catch (IllegalArgumentException success) {} |
1373 |
< |
assertEquals(s, p.getCorePoolSize()); |
1374 |
< |
assertEquals(s, p.getMaximumPoolSize()); |
1375 |
< |
try { |
1376 |
< |
p.setCorePoolSize(s + 1); |
1377 |
< |
shouldThrow(); |
1378 |
< |
} catch (IllegalArgumentException success) {} |
1379 |
< |
assertEquals(s, p.getCorePoolSize()); |
1380 |
< |
assertEquals(s, p.getMaximumPoolSize()); |
1366 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1367 |
> |
for (int s = 1; s < 5; s++) { |
1368 |
> |
p.setMaximumPoolSize(s); |
1369 |
> |
p.setCorePoolSize(s); |
1370 |
> |
try { |
1371 |
> |
p.setMaximumPoolSize(s - 1); |
1372 |
> |
shouldThrow(); |
1373 |
> |
} catch (IllegalArgumentException success) {} |
1374 |
> |
assertEquals(s, p.getCorePoolSize()); |
1375 |
> |
assertEquals(s, p.getMaximumPoolSize()); |
1376 |
> |
try { |
1377 |
> |
p.setCorePoolSize(s + 1); |
1378 |
> |
shouldThrow(); |
1379 |
> |
} catch (IllegalArgumentException success) {} |
1380 |
> |
assertEquals(s, p.getCorePoolSize()); |
1381 |
> |
assertEquals(s, p.getMaximumPoolSize()); |
1382 |
> |
} |
1383 |
|
} |
1383 |
– |
joinPool(p); |
1384 |
|
} |
1385 |
|
|
1386 |
|
/** |
1388 |
|
* when given a negative value |
1389 |
|
*/ |
1390 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1391 |
< |
ThreadPoolExecutor p = |
1391 |
> |
final ThreadPoolExecutor p = |
1392 |
|
new ThreadPoolExecutor(2, 3, |
1393 |
|
LONG_DELAY_MS, MILLISECONDS, |
1394 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1395 |
< |
try { |
1396 |
< |
p.setKeepAliveTime(-1,MILLISECONDS); |
1397 |
< |
shouldThrow(); |
1398 |
< |
} catch (IllegalArgumentException success) { |
1399 |
< |
} finally { |
1400 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1395 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1396 |
> |
try { |
1397 |
> |
p.setKeepAliveTime(-1, MILLISECONDS); |
1398 |
> |
shouldThrow(); |
1399 |
> |
} catch (IllegalArgumentException success) {} |
1400 |
|
} |
1402 |
– |
joinPool(p); |
1401 |
|
} |
1402 |
|
|
1403 |
|
/** |
1405 |
|
*/ |
1406 |
|
public void testTerminated() { |
1407 |
|
ExtendedTPE p = new ExtendedTPE(); |
1408 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1409 |
< |
assertTrue(p.terminatedCalled()); |
1410 |
< |
joinPool(p); |
1408 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1409 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1410 |
> |
assertTrue(p.terminatedCalled()); |
1411 |
> |
assertTrue(p.isShutdown()); |
1412 |
> |
} |
1413 |
|
} |
1414 |
|
|
1415 |
|
/** |
1417 |
|
*/ |
1418 |
|
public void testBeforeAfter() throws InterruptedException { |
1419 |
|
ExtendedTPE p = new ExtendedTPE(); |
1420 |
< |
try { |
1420 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1421 |
|
final CountDownLatch done = new CountDownLatch(1); |
1422 |
|
p.execute(new CheckedRunnable() { |
1423 |
|
public void realRun() { |
1427 |
|
assertEquals(0, done.getCount()); |
1428 |
|
assertTrue(p.afterCalled()); |
1429 |
|
assertTrue(p.beforeCalled()); |
1430 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1431 |
– |
} finally { |
1432 |
– |
joinPool(p); |
1430 |
|
} |
1431 |
|
} |
1432 |
|
|
1434 |
|
* completed submit of callable returns result |
1435 |
|
*/ |
1436 |
|
public void testSubmitCallable() throws Exception { |
1437 |
< |
ExecutorService e = |
1437 |
> |
final ExecutorService e = |
1438 |
|
new ThreadPoolExecutor(2, 2, |
1439 |
|
LONG_DELAY_MS, MILLISECONDS, |
1440 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1441 |
< |
try { |
1441 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1442 |
|
Future<String> future = e.submit(new StringTask()); |
1443 |
|
String result = future.get(); |
1444 |
|
assertSame(TEST_STRING, result); |
1448 |
– |
} finally { |
1449 |
– |
joinPool(e); |
1445 |
|
} |
1446 |
|
} |
1447 |
|
|
1449 |
|
* completed submit of runnable returns successfully |
1450 |
|
*/ |
1451 |
|
public void testSubmitRunnable() throws Exception { |
1452 |
< |
ExecutorService e = |
1452 |
> |
final ExecutorService e = |
1453 |
|
new ThreadPoolExecutor(2, 2, |
1454 |
|
LONG_DELAY_MS, MILLISECONDS, |
1455 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1456 |
< |
try { |
1456 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1457 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1458 |
|
future.get(); |
1459 |
|
assertTrue(future.isDone()); |
1465 |
– |
} finally { |
1466 |
– |
joinPool(e); |
1460 |
|
} |
1461 |
|
} |
1462 |
|
|
1464 |
|
* completed submit of (runnable, result) returns result |
1465 |
|
*/ |
1466 |
|
public void testSubmitRunnable2() throws Exception { |
1467 |
< |
ExecutorService e = |
1467 |
> |
final ExecutorService e = |
1468 |
|
new ThreadPoolExecutor(2, 2, |
1469 |
|
LONG_DELAY_MS, MILLISECONDS, |
1470 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1471 |
< |
try { |
1471 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1472 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1473 |
|
String result = future.get(); |
1474 |
|
assertSame(TEST_STRING, result); |
1482 |
– |
} finally { |
1483 |
– |
joinPool(e); |
1475 |
|
} |
1476 |
|
} |
1477 |
|
|
1479 |
|
* invokeAny(null) throws NPE |
1480 |
|
*/ |
1481 |
|
public void testInvokeAny1() throws Exception { |
1482 |
< |
ExecutorService e = |
1482 |
> |
final ExecutorService e = |
1483 |
|
new ThreadPoolExecutor(2, 2, |
1484 |
|
LONG_DELAY_MS, MILLISECONDS, |
1485 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1486 |
< |
try { |
1487 |
< |
e.invokeAny(null); |
1488 |
< |
shouldThrow(); |
1489 |
< |
} catch (NullPointerException success) { |
1490 |
< |
} finally { |
1500 |
< |
joinPool(e); |
1486 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1487 |
> |
try { |
1488 |
> |
e.invokeAny(null); |
1489 |
> |
shouldThrow(); |
1490 |
> |
} catch (NullPointerException success) {} |
1491 |
|
} |
1492 |
|
} |
1493 |
|
|
1495 |
|
* invokeAny(empty collection) throws IAE |
1496 |
|
*/ |
1497 |
|
public void testInvokeAny2() throws Exception { |
1498 |
< |
ExecutorService e = |
1498 |
> |
final ExecutorService e = |
1499 |
|
new ThreadPoolExecutor(2, 2, |
1500 |
|
LONG_DELAY_MS, MILLISECONDS, |
1501 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1502 |
< |
try { |
1503 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
1504 |
< |
shouldThrow(); |
1505 |
< |
} catch (IllegalArgumentException success) { |
1506 |
< |
} finally { |
1517 |
< |
joinPool(e); |
1502 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1503 |
> |
try { |
1504 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1505 |
> |
shouldThrow(); |
1506 |
> |
} catch (IllegalArgumentException success) {} |
1507 |
|
} |
1508 |
|
} |
1509 |
|
|
1516 |
|
new ThreadPoolExecutor(2, 2, |
1517 |
|
LONG_DELAY_MS, MILLISECONDS, |
1518 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1519 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1520 |
< |
l.add(latchAwaitingStringTask(latch)); |
1521 |
< |
l.add(null); |
1522 |
< |
try { |
1523 |
< |
e.invokeAny(l); |
1524 |
< |
shouldThrow(); |
1525 |
< |
} catch (NullPointerException success) { |
1526 |
< |
} finally { |
1519 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1520 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1521 |
> |
l.add(latchAwaitingStringTask(latch)); |
1522 |
> |
l.add(null); |
1523 |
> |
try { |
1524 |
> |
e.invokeAny(l); |
1525 |
> |
shouldThrow(); |
1526 |
> |
} catch (NullPointerException success) {} |
1527 |
|
latch.countDown(); |
1539 |
– |
joinPool(e); |
1528 |
|
} |
1529 |
|
} |
1530 |
|
|
1532 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1533 |
|
*/ |
1534 |
|
public void testInvokeAny4() throws Exception { |
1535 |
< |
ExecutorService e = |
1535 |
> |
final ExecutorService e = |
1536 |
|
new ThreadPoolExecutor(2, 2, |
1537 |
|
LONG_DELAY_MS, MILLISECONDS, |
1538 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1539 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1540 |
< |
l.add(new NPETask()); |
1541 |
< |
try { |
1542 |
< |
e.invokeAny(l); |
1543 |
< |
shouldThrow(); |
1544 |
< |
} catch (ExecutionException success) { |
1545 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1546 |
< |
} finally { |
1547 |
< |
joinPool(e); |
1539 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1540 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1541 |
> |
l.add(new NPETask()); |
1542 |
> |
try { |
1543 |
> |
e.invokeAny(l); |
1544 |
> |
shouldThrow(); |
1545 |
> |
} catch (ExecutionException success) { |
1546 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1547 |
> |
} |
1548 |
|
} |
1549 |
|
} |
1550 |
|
|
1552 |
|
* invokeAny(c) returns result of some task |
1553 |
|
*/ |
1554 |
|
public void testInvokeAny5() throws Exception { |
1555 |
< |
ExecutorService e = |
1555 |
> |
final ExecutorService e = |
1556 |
|
new ThreadPoolExecutor(2, 2, |
1557 |
|
LONG_DELAY_MS, MILLISECONDS, |
1558 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1559 |
< |
try { |
1559 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1560 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1561 |
|
l.add(new StringTask()); |
1562 |
|
l.add(new StringTask()); |
1563 |
|
String result = e.invokeAny(l); |
1564 |
|
assertSame(TEST_STRING, result); |
1577 |
– |
} finally { |
1578 |
– |
joinPool(e); |
1565 |
|
} |
1566 |
|
} |
1567 |
|
|
1569 |
|
* invokeAll(null) throws NPE |
1570 |
|
*/ |
1571 |
|
public void testInvokeAll1() throws Exception { |
1572 |
< |
ExecutorService e = |
1572 |
> |
final ExecutorService e = |
1573 |
|
new ThreadPoolExecutor(2, 2, |
1574 |
|
LONG_DELAY_MS, MILLISECONDS, |
1575 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1576 |
< |
try { |
1577 |
< |
e.invokeAll(null); |
1578 |
< |
shouldThrow(); |
1579 |
< |
} catch (NullPointerException success) { |
1580 |
< |
} finally { |
1595 |
< |
joinPool(e); |
1576 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1577 |
> |
try { |
1578 |
> |
e.invokeAll(null); |
1579 |
> |
shouldThrow(); |
1580 |
> |
} catch (NullPointerException success) {} |
1581 |
|
} |
1582 |
|
} |
1583 |
|
|
1585 |
|
* invokeAll(empty collection) returns empty collection |
1586 |
|
*/ |
1587 |
|
public void testInvokeAll2() throws InterruptedException { |
1588 |
< |
ExecutorService e = |
1588 |
> |
final ExecutorService e = |
1589 |
|
new ThreadPoolExecutor(2, 2, |
1590 |
|
LONG_DELAY_MS, MILLISECONDS, |
1591 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1592 |
< |
try { |
1592 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1593 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1594 |
|
assertTrue(r.isEmpty()); |
1610 |
– |
} finally { |
1611 |
– |
joinPool(e); |
1595 |
|
} |
1596 |
|
} |
1597 |
|
|
1599 |
|
* invokeAll(c) throws NPE if c has null elements |
1600 |
|
*/ |
1601 |
|
public void testInvokeAll3() throws Exception { |
1602 |
< |
ExecutorService e = |
1602 |
> |
final ExecutorService e = |
1603 |
|
new ThreadPoolExecutor(2, 2, |
1604 |
|
LONG_DELAY_MS, MILLISECONDS, |
1605 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1606 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1607 |
< |
l.add(new StringTask()); |
1608 |
< |
l.add(null); |
1609 |
< |
try { |
1610 |
< |
e.invokeAll(l); |
1611 |
< |
shouldThrow(); |
1612 |
< |
} catch (NullPointerException success) { |
1613 |
< |
} finally { |
1631 |
< |
joinPool(e); |
1606 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1607 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1608 |
> |
l.add(new StringTask()); |
1609 |
> |
l.add(null); |
1610 |
> |
try { |
1611 |
> |
e.invokeAll(l); |
1612 |
> |
shouldThrow(); |
1613 |
> |
} catch (NullPointerException success) {} |
1614 |
|
} |
1615 |
|
} |
1616 |
|
|
1618 |
|
* get of element of invokeAll(c) throws exception on failed task |
1619 |
|
*/ |
1620 |
|
public void testInvokeAll4() throws Exception { |
1621 |
< |
ExecutorService e = |
1621 |
> |
final ExecutorService e = |
1622 |
|
new ThreadPoolExecutor(2, 2, |
1623 |
|
LONG_DELAY_MS, MILLISECONDS, |
1624 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1625 |
< |
try { |
1625 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1626 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1627 |
|
l.add(new NPETask()); |
1628 |
|
List<Future<String>> futures = e.invokeAll(l); |
1633 |
|
} catch (ExecutionException success) { |
1634 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1635 |
|
} |
1654 |
– |
} finally { |
1655 |
– |
joinPool(e); |
1636 |
|
} |
1637 |
|
} |
1638 |
|
|
1640 |
|
* invokeAll(c) returns results of all completed tasks |
1641 |
|
*/ |
1642 |
|
public void testInvokeAll5() throws Exception { |
1643 |
< |
ExecutorService e = |
1643 |
> |
final ExecutorService e = |
1644 |
|
new ThreadPoolExecutor(2, 2, |
1645 |
|
LONG_DELAY_MS, MILLISECONDS, |
1646 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1647 |
< |
try { |
1647 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1648 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1649 |
|
l.add(new StringTask()); |
1650 |
|
l.add(new StringTask()); |
1652 |
|
assertEquals(2, futures.size()); |
1653 |
|
for (Future<String> future : futures) |
1654 |
|
assertSame(TEST_STRING, future.get()); |
1675 |
– |
} finally { |
1676 |
– |
joinPool(e); |
1655 |
|
} |
1656 |
|
} |
1657 |
|
|
1659 |
|
* timed invokeAny(null) throws NPE |
1660 |
|
*/ |
1661 |
|
public void testTimedInvokeAny1() throws Exception { |
1662 |
< |
ExecutorService e = |
1662 |
> |
final ExecutorService e = |
1663 |
|
new ThreadPoolExecutor(2, 2, |
1664 |
|
LONG_DELAY_MS, MILLISECONDS, |
1665 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1666 |
< |
try { |
1667 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1668 |
< |
shouldThrow(); |
1669 |
< |
} catch (NullPointerException success) { |
1670 |
< |
} finally { |
1693 |
< |
joinPool(e); |
1666 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1667 |
> |
try { |
1668 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1669 |
> |
shouldThrow(); |
1670 |
> |
} catch (NullPointerException success) {} |
1671 |
|
} |
1672 |
|
} |
1673 |
|
|
1675 |
|
* timed invokeAny(,,null) throws NPE |
1676 |
|
*/ |
1677 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1678 |
< |
ExecutorService e = |
1678 |
> |
final ExecutorService e = |
1679 |
|
new ThreadPoolExecutor(2, 2, |
1680 |
|
LONG_DELAY_MS, MILLISECONDS, |
1681 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1682 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1683 |
< |
l.add(new StringTask()); |
1684 |
< |
try { |
1685 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1686 |
< |
shouldThrow(); |
1687 |
< |
} catch (NullPointerException success) { |
1688 |
< |
} finally { |
1712 |
< |
joinPool(e); |
1682 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1683 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1684 |
> |
l.add(new StringTask()); |
1685 |
> |
try { |
1686 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1687 |
> |
shouldThrow(); |
1688 |
> |
} catch (NullPointerException success) {} |
1689 |
|
} |
1690 |
|
} |
1691 |
|
|
1693 |
|
* timed invokeAny(empty collection) throws IAE |
1694 |
|
*/ |
1695 |
|
public void testTimedInvokeAny2() throws Exception { |
1696 |
< |
ExecutorService e = |
1696 |
> |
final ExecutorService e = |
1697 |
|
new ThreadPoolExecutor(2, 2, |
1698 |
|
LONG_DELAY_MS, MILLISECONDS, |
1699 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1700 |
< |
try { |
1701 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1702 |
< |
shouldThrow(); |
1703 |
< |
} catch (IllegalArgumentException success) { |
1704 |
< |
} finally { |
1705 |
< |
joinPool(e); |
1700 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1701 |
> |
try { |
1702 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
1703 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1704 |
> |
shouldThrow(); |
1705 |
> |
} catch (IllegalArgumentException success) {} |
1706 |
|
} |
1707 |
|
} |
1708 |
|
|
1715 |
|
new ThreadPoolExecutor(2, 2, |
1716 |
|
LONG_DELAY_MS, MILLISECONDS, |
1717 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1718 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1719 |
< |
l.add(latchAwaitingStringTask(latch)); |
1720 |
< |
l.add(null); |
1721 |
< |
try { |
1722 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1723 |
< |
shouldThrow(); |
1724 |
< |
} catch (NullPointerException success) { |
1725 |
< |
} finally { |
1718 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1719 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1720 |
> |
l.add(latchAwaitingStringTask(latch)); |
1721 |
> |
l.add(null); |
1722 |
> |
try { |
1723 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1724 |
> |
shouldThrow(); |
1725 |
> |
} catch (NullPointerException success) {} |
1726 |
|
latch.countDown(); |
1751 |
– |
joinPool(e); |
1727 |
|
} |
1728 |
|
} |
1729 |
|
|
1731 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1732 |
|
*/ |
1733 |
|
public void testTimedInvokeAny4() throws Exception { |
1734 |
< |
ExecutorService e = |
1734 |
> |
final ExecutorService e = |
1735 |
|
new ThreadPoolExecutor(2, 2, |
1736 |
|
LONG_DELAY_MS, MILLISECONDS, |
1737 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1738 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1739 |
< |
l.add(new NPETask()); |
1740 |
< |
try { |
1741 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1742 |
< |
shouldThrow(); |
1743 |
< |
} catch (ExecutionException success) { |
1744 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1745 |
< |
} finally { |
1746 |
< |
joinPool(e); |
1738 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1739 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1740 |
> |
l.add(new NPETask()); |
1741 |
> |
try { |
1742 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1743 |
> |
shouldThrow(); |
1744 |
> |
} catch (ExecutionException success) { |
1745 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1746 |
> |
} |
1747 |
|
} |
1748 |
|
} |
1749 |
|
|
1751 |
|
* timed invokeAny(c) returns result of some task |
1752 |
|
*/ |
1753 |
|
public void testTimedInvokeAny5() throws Exception { |
1754 |
< |
ExecutorService e = |
1754 |
> |
final ExecutorService e = |
1755 |
|
new ThreadPoolExecutor(2, 2, |
1756 |
|
LONG_DELAY_MS, MILLISECONDS, |
1757 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1758 |
< |
try { |
1758 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1759 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1760 |
|
l.add(new StringTask()); |
1761 |
|
l.add(new StringTask()); |
1762 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1763 |
|
assertSame(TEST_STRING, result); |
1789 |
– |
} finally { |
1790 |
– |
joinPool(e); |
1764 |
|
} |
1765 |
|
} |
1766 |
|
|
1768 |
|
* timed invokeAll(null) throws NPE |
1769 |
|
*/ |
1770 |
|
public void testTimedInvokeAll1() throws Exception { |
1771 |
< |
ExecutorService e = |
1771 |
> |
final ExecutorService e = |
1772 |
|
new ThreadPoolExecutor(2, 2, |
1773 |
|
LONG_DELAY_MS, MILLISECONDS, |
1774 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1775 |
< |
try { |
1776 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1777 |
< |
shouldThrow(); |
1778 |
< |
} catch (NullPointerException success) { |
1779 |
< |
} finally { |
1807 |
< |
joinPool(e); |
1775 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1776 |
> |
try { |
1777 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1778 |
> |
shouldThrow(); |
1779 |
> |
} catch (NullPointerException success) {} |
1780 |
|
} |
1781 |
|
} |
1782 |
|
|
1784 |
|
* timed invokeAll(,,null) throws NPE |
1785 |
|
*/ |
1786 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1787 |
< |
ExecutorService e = |
1787 |
> |
final ExecutorService e = |
1788 |
|
new ThreadPoolExecutor(2, 2, |
1789 |
|
LONG_DELAY_MS, MILLISECONDS, |
1790 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1791 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1792 |
< |
l.add(new StringTask()); |
1793 |
< |
try { |
1794 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1795 |
< |
shouldThrow(); |
1796 |
< |
} catch (NullPointerException success) { |
1797 |
< |
} finally { |
1826 |
< |
joinPool(e); |
1791 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1792 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1793 |
> |
l.add(new StringTask()); |
1794 |
> |
try { |
1795 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1796 |
> |
shouldThrow(); |
1797 |
> |
} catch (NullPointerException success) {} |
1798 |
|
} |
1799 |
|
} |
1800 |
|
|
1802 |
|
* timed invokeAll(empty collection) returns empty collection |
1803 |
|
*/ |
1804 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1805 |
< |
ExecutorService e = |
1805 |
> |
final ExecutorService e = |
1806 |
|
new ThreadPoolExecutor(2, 2, |
1807 |
|
LONG_DELAY_MS, MILLISECONDS, |
1808 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1809 |
< |
try { |
1810 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1809 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1810 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1811 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1812 |
|
assertTrue(r.isEmpty()); |
1841 |
– |
} finally { |
1842 |
– |
joinPool(e); |
1813 |
|
} |
1814 |
|
} |
1815 |
|
|
1817 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1818 |
|
*/ |
1819 |
|
public void testTimedInvokeAll3() throws Exception { |
1820 |
< |
ExecutorService e = |
1820 |
> |
final ExecutorService e = |
1821 |
|
new ThreadPoolExecutor(2, 2, |
1822 |
|
LONG_DELAY_MS, MILLISECONDS, |
1823 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1824 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1825 |
< |
l.add(new StringTask()); |
1826 |
< |
l.add(null); |
1827 |
< |
try { |
1828 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1829 |
< |
shouldThrow(); |
1830 |
< |
} catch (NullPointerException success) { |
1831 |
< |
} finally { |
1862 |
< |
joinPool(e); |
1824 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1825 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1826 |
> |
l.add(new StringTask()); |
1827 |
> |
l.add(null); |
1828 |
> |
try { |
1829 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1830 |
> |
shouldThrow(); |
1831 |
> |
} catch (NullPointerException success) {} |
1832 |
|
} |
1833 |
|
} |
1834 |
|
|
1836 |
|
* get of element of invokeAll(c) throws exception on failed task |
1837 |
|
*/ |
1838 |
|
public void testTimedInvokeAll4() throws Exception { |
1839 |
< |
ExecutorService e = |
1839 |
> |
final ExecutorService e = |
1840 |
|
new ThreadPoolExecutor(2, 2, |
1841 |
|
LONG_DELAY_MS, MILLISECONDS, |
1842 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1843 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1844 |
< |
l.add(new NPETask()); |
1845 |
< |
List<Future<String>> futures = |
1846 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1847 |
< |
assertEquals(1, futures.size()); |
1848 |
< |
try { |
1849 |
< |
futures.get(0).get(); |
1850 |
< |
shouldThrow(); |
1851 |
< |
} catch (ExecutionException success) { |
1852 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1853 |
< |
} finally { |
1854 |
< |
joinPool(e); |
1843 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1844 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1845 |
> |
l.add(new NPETask()); |
1846 |
> |
List<Future<String>> futures = |
1847 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1848 |
> |
assertEquals(1, futures.size()); |
1849 |
> |
try { |
1850 |
> |
futures.get(0).get(); |
1851 |
> |
shouldThrow(); |
1852 |
> |
} catch (ExecutionException success) { |
1853 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1854 |
> |
} |
1855 |
|
} |
1856 |
|
} |
1857 |
|
|
1859 |
|
* timed invokeAll(c) returns results of all completed tasks |
1860 |
|
*/ |
1861 |
|
public void testTimedInvokeAll5() throws Exception { |
1862 |
< |
ExecutorService e = |
1862 |
> |
final ExecutorService e = |
1863 |
|
new ThreadPoolExecutor(2, 2, |
1864 |
|
LONG_DELAY_MS, MILLISECONDS, |
1865 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1866 |
< |
try { |
1866 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1867 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1868 |
|
l.add(new StringTask()); |
1869 |
|
l.add(new StringTask()); |
1872 |
|
assertEquals(2, futures.size()); |
1873 |
|
for (Future<String> future : futures) |
1874 |
|
assertSame(TEST_STRING, future.get()); |
1906 |
– |
} finally { |
1907 |
– |
joinPool(e); |
1875 |
|
} |
1876 |
|
} |
1877 |
|
|
1879 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1880 |
|
*/ |
1881 |
|
public void testTimedInvokeAll6() throws Exception { |
1882 |
< |
ExecutorService e = |
1882 |
> |
final ExecutorService e = |
1883 |
|
new ThreadPoolExecutor(2, 2, |
1884 |
|
LONG_DELAY_MS, MILLISECONDS, |
1885 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1886 |
< |
try { |
1886 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1887 |
|
for (long timeout = timeoutMillis();;) { |
1888 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1889 |
|
tasks.add(new StringTask("0")); |
1907 |
|
fail("expected exactly one task to be cancelled"); |
1908 |
|
} |
1909 |
|
} |
1943 |
– |
} finally { |
1944 |
– |
joinPool(e); |
1910 |
|
} |
1911 |
|
} |
1912 |
|
|
1920 |
|
LONG_DELAY_MS, MILLISECONDS, |
1921 |
|
new LinkedBlockingQueue<Runnable>(), |
1922 |
|
new FailingThreadFactory()); |
1923 |
< |
try { |
1923 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1924 |
|
final int TASKS = 100; |
1925 |
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1926 |
|
for (int k = 0; k < TASKS; ++k) |
1929 |
|
done.countDown(); |
1930 |
|
}}); |
1931 |
|
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1967 |
– |
} finally { |
1968 |
– |
joinPool(e); |
1932 |
|
} |
1933 |
|
} |
1934 |
|
|
1940 |
|
new ThreadPoolExecutor(2, 2, |
1941 |
|
1000, MILLISECONDS, |
1942 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1943 |
< |
assertFalse(p.allowsCoreThreadTimeOut()); |
1944 |
< |
joinPool(p); |
1943 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1944 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1945 |
> |
} |
1946 |
|
} |
1947 |
|
|
1948 |
|
/** |
1954 |
|
new ThreadPoolExecutor(2, 10, |
1955 |
|
keepAliveTime, MILLISECONDS, |
1956 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1957 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1958 |
< |
try { |
1957 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1958 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1959 |
|
p.allowCoreThreadTimeOut(true); |
1960 |
|
p.execute(new CheckedRunnable() { |
1961 |
|
public void realRun() { |
1970 |
|
Thread.yield(); |
1971 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1972 |
|
assertEquals(0, p.getPoolSize()); |
2009 |
– |
} finally { |
2010 |
– |
joinPool(p); |
1973 |
|
} |
1974 |
|
} |
1975 |
|
|
1982 |
|
new ThreadPoolExecutor(2, 10, |
1983 |
|
keepAliveTime, MILLISECONDS, |
1984 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1985 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1986 |
< |
try { |
1985 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1986 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1987 |
|
p.allowCoreThreadTimeOut(false); |
1988 |
|
p.execute(new CheckedRunnable() { |
1989 |
|
public void realRun() throws InterruptedException { |
1992 |
|
}}); |
1993 |
|
delay(2 * keepAliveTime); |
1994 |
|
assertTrue(p.getPoolSize() >= 1); |
2033 |
– |
} finally { |
2034 |
– |
joinPool(p); |
1995 |
|
} |
1996 |
|
} |
1997 |
|
|
2010 |
|
new ThreadPoolExecutor(1, 30, |
2011 |
|
60, SECONDS, |
2012 |
|
new ArrayBlockingQueue(30)); |
2013 |
< |
try { |
2013 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
2014 |
|
for (int i = 0; i < nTasks; ++i) { |
2015 |
|
for (;;) { |
2016 |
|
try { |
2022 |
|
} |
2023 |
|
// enough time to run all tasks |
2024 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2065 |
– |
} finally { |
2066 |
– |
joinPool(p); |
2025 |
|
} |
2026 |
|
} |
2027 |
|
|
2033 |
|
new ThreadPoolExecutor(1, 1, |
2034 |
|
LONG_DELAY_MS, MILLISECONDS, |
2035 |
|
new LinkedBlockingQueue<Runnable>()); |
2036 |
< |
try { |
2036 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
2037 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2038 |
|
final CountDownLatch done = new CountDownLatch(1); |
2039 |
|
final List<Future<?>> futures = new ArrayList<>(); |
2060 |
|
assertTrue(future.isDone()); |
2061 |
|
} |
2062 |
|
done.countDown(); |
2105 |
– |
} finally { |
2106 |
– |
joinPool(e); |
2063 |
|
} |
2064 |
|
} |
2065 |
|
|