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; |
23 |
|
import java.util.concurrent.Future; |
24 |
|
import java.util.concurrent.FutureTask; |
25 |
|
import java.util.concurrent.LinkedBlockingQueue; |
24 |
– |
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 |
|
|
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) |
653 |
|
*/ |
654 |
|
public void testGetQueue() throws InterruptedException { |
655 |
|
final CountDownLatch done = new CountDownLatch(1); |
656 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
656 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
657 |
|
final ThreadPoolExecutor p = |
658 |
|
new CustomTPE(1, 1, |
659 |
|
LONG_DELAY_MS, MILLISECONDS, |
662 |
|
final CountDownLatch threadStarted = 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()); |
685 |
|
*/ |
686 |
|
public void testRemove() throws InterruptedException { |
687 |
|
final CountDownLatch done = new CountDownLatch(1); |
688 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
688 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
689 |
|
final ThreadPoolExecutor p = |
690 |
|
new CustomTPE(1, 1, |
691 |
|
LONG_DELAY_MS, MILLISECONDS, |
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 |
|
await(done); |
1131 |
|
} |
1132 |
|
|
1133 |
|
/** |
1134 |
< |
* execute throws RejectedExecutionException if saturated. |
1134 |
> |
* Submitted tasks are rejected when saturated or shutdown |
1135 |
|
*/ |
1136 |
< |
public void testSaturatedExecute() { |
1135 |
< |
final CountDownLatch done = new CountDownLatch(1); |
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, done)) { |
1142 |
< |
Runnable task = new CheckedRunnable() { |
1143 |
< |
public void realRun() throws InterruptedException { |
1144 |
< |
await(done); |
1145 |
< |
}}; |
1146 |
< |
for (int i = 0; i < 2; ++i) |
1147 |
< |
p.execute(task); |
1147 |
< |
for (int i = 0; i < 2; ++i) { |
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 |
< |
p.execute(task); |
1150 |
< |
shouldThrow(); |
1151 |
< |
} catch (RejectedExecutionException success) {} |
1152 |
< |
assertTrue(p.getTaskCount() <= 2); |
1153 |
< |
} |
1154 |
< |
} |
1155 |
< |
} |
1149 |
> |
done.await(); |
1150 |
> |
return; |
1151 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1152 |
> |
}}; |
1153 |
> |
final Callable<Boolean> c = () -> { |
1154 |
> |
threadsStarted.countDown(); |
1155 |
> |
for (;;) { |
1156 |
> |
try { |
1157 |
> |
done.await(); |
1158 |
> |
return Boolean.TRUE; |
1159 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1160 |
> |
}}; |
1161 |
> |
final boolean shutdownNow = rnd.nextBoolean(); |
1162 |
|
|
1157 |
– |
/** |
1158 |
– |
* executor using CallerRunsPolicy runs task if saturated. |
1159 |
– |
*/ |
1160 |
– |
public void testSaturatedExecute2() { |
1161 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1162 |
– |
final ThreadPoolExecutor p = |
1163 |
– |
new CustomTPE(1, 1, |
1164 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1165 |
– |
new ArrayBlockingQueue<Runnable>(1), |
1166 |
– |
new CustomTPE.CallerRunsPolicy()); |
1163 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1164 |
< |
Runnable blocker = new CheckedRunnable() { |
1165 |
< |
public void realRun() throws InterruptedException { |
1166 |
< |
await(done); |
1167 |
< |
}}; |
1168 |
< |
p.execute(blocker); |
1169 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1170 |
< |
for (int i = 0; i < tasks.length; i++) |
1171 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1172 |
< |
for (int i = 0; i < tasks.length; i++) |
1177 |
< |
p.execute(tasks[i]); |
1178 |
< |
for (int i = 1; i < tasks.length; i++) |
1179 |
< |
assertTrue(tasks[i].done); |
1180 |
< |
assertFalse(tasks[0].done); // waiting in queue |
1181 |
< |
} |
1182 |
< |
} |
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 |
> |
} |
1173 |
|
|
1174 |
< |
/** |
1175 |
< |
* executor using DiscardPolicy drops task if saturated. |
1176 |
< |
*/ |
1177 |
< |
public void testSaturatedExecute3() { |
1178 |
< |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1179 |
< |
for (int i = 0; i < tasks.length; ++i) |
1180 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1181 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1182 |
< |
final ThreadPoolExecutor p = |
1183 |
< |
new CustomTPE(1, 1, |
1184 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1185 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1186 |
< |
new CustomTPE.DiscardPolicy()); |
1197 |
< |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1198 |
< |
p.execute(awaiter(done)); |
1174 |
> |
await(threadsStarted); |
1175 |
> |
assertTaskSubmissionsAreRejected(p); |
1176 |
> |
|
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 |
> |
done.countDown(); // release blocking tasks |
1186 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
1187 |
|
|
1188 |
< |
for (TrackedNoOpRunnable task : tasks) |
1189 |
< |
p.execute(task); |
1190 |
< |
for (int i = 1; i < tasks.length; i++) |
1191 |
< |
assertFalse(tasks[i].done); |
1192 |
< |
} |
1205 |
< |
for (int i = 1; i < tasks.length; i++) |
1206 |
< |
assertFalse(tasks[i].done); |
1207 |
< |
assertTrue(tasks[0].done); // was waiting in queue |
1188 |
> |
assertTaskSubmissionsAreRejected(p); |
1189 |
> |
} |
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()); |
1207 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1208 |
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1209 |
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1210 |
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1222 |
|
} |
1223 |
|
|
1224 |
|
/** |
1240 |
– |
* execute throws RejectedExecutionException if shutdown |
1241 |
– |
*/ |
1242 |
– |
public void testRejectedExecutionExceptionOnShutdown() { |
1243 |
– |
final ThreadPoolExecutor p = |
1244 |
– |
new CustomTPE(1, 1, |
1245 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1246 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1247 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1248 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1249 |
– |
try { |
1250 |
– |
p.execute(new NoOpRunnable()); |
1251 |
– |
shouldThrow(); |
1252 |
– |
} catch (RejectedExecutionException success) {} |
1253 |
– |
} |
1254 |
– |
} |
1255 |
– |
|
1256 |
– |
/** |
1257 |
– |
* execute using CallerRunsPolicy drops task on shutdown |
1258 |
– |
*/ |
1259 |
– |
public void testCallerRunsOnShutdown() { |
1260 |
– |
final ThreadPoolExecutor p = |
1261 |
– |
new CustomTPE(1, 1, |
1262 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1263 |
– |
new ArrayBlockingQueue<Runnable>(1), |
1264 |
– |
new CustomTPE.CallerRunsPolicy()); |
1265 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1266 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1267 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1268 |
– |
p.execute(r); |
1269 |
– |
assertFalse(r.done); |
1270 |
– |
} |
1271 |
– |
} |
1272 |
– |
|
1273 |
– |
/** |
1274 |
– |
* execute using DiscardPolicy drops task on shutdown |
1275 |
– |
*/ |
1276 |
– |
public void testDiscardOnShutdown() { |
1277 |
– |
final ThreadPoolExecutor p = |
1278 |
– |
new CustomTPE(1, 1, |
1279 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1280 |
– |
new ArrayBlockingQueue<Runnable>(1), |
1281 |
– |
new CustomTPE.DiscardPolicy()); |
1282 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1283 |
– |
try (PoolCleaner cleaner = cleaner(p)) { |
1284 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1285 |
– |
p.execute(r); |
1286 |
– |
assertFalse(r.done); |
1287 |
– |
} |
1288 |
– |
} |
1289 |
– |
|
1290 |
– |
/** |
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 { |
1318 |
< |
p.execute(null); |
1319 |
< |
shouldThrow(); |
1320 |
< |
} 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>>(), |
1691 |
< |
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(), MILLISECONDS); |
1646 |
|
shouldThrow(); |
1647 |
|
} catch (NullPointerException success) {} |
1648 |
|
latch.countDown(); |
1659 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1660 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1661 |
|
long startTime = System.nanoTime(); |
1662 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1662 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1663 |
|
l.add(new NPETask()); |
1664 |
|
try { |
1665 |
|
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1681 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1682 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1683 |
|
long startTime = System.nanoTime(); |
1684 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1684 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1685 |
|
l.add(new StringTask()); |
1686 |
|
l.add(new StringTask()); |
1687 |
|
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
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, LONG_DELAY_MS, MILLISECONDS); |
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 = |
1862 |
|
public void realRun() { |
1863 |
|
done.countDown(); |
1864 |
|
}}); |
1865 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1865 |
> |
await(done); |
1866 |
|
} |
1867 |
|
} |
1868 |
|
|
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 |
|
} |