1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
1260 |
|
} catch(Exception ex) { |
1261 |
|
unexpectedException(); |
1262 |
|
} finally { |
1263 |
+ |
joinPool(e); |
1264 |
+ |
} |
1265 |
+ |
} |
1266 |
+ |
|
1267 |
+ |
|
1268 |
+ |
|
1269 |
+ |
/** |
1270 |
+ |
* timed invokeAny(null) throws NPE |
1271 |
+ |
*/ |
1272 |
+ |
public void testTimedInvokeAny1() { |
1273 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1274 |
+ |
try { |
1275 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1276 |
+ |
} catch (NullPointerException success) { |
1277 |
+ |
} catch(Exception ex) { |
1278 |
+ |
unexpectedException(); |
1279 |
+ |
} finally { |
1280 |
+ |
joinPool(e); |
1281 |
+ |
} |
1282 |
+ |
} |
1283 |
+ |
|
1284 |
+ |
/** |
1285 |
+ |
* timed invokeAny(,,null) throws NPE |
1286 |
+ |
*/ |
1287 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
1288 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1289 |
+ |
try { |
1290 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1291 |
+ |
l.add(new StringTask()); |
1292 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1293 |
+ |
} catch (NullPointerException success) { |
1294 |
+ |
} catch(Exception ex) { |
1295 |
+ |
unexpectedException(); |
1296 |
+ |
} finally { |
1297 |
+ |
joinPool(e); |
1298 |
+ |
} |
1299 |
+ |
} |
1300 |
+ |
|
1301 |
+ |
/** |
1302 |
+ |
* timed invokeAny(empty collection) throws IAE |
1303 |
+ |
*/ |
1304 |
+ |
public void testTimedInvokeAny2() { |
1305 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1306 |
+ |
try { |
1307 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1308 |
+ |
} catch (IllegalArgumentException success) { |
1309 |
+ |
} catch(Exception ex) { |
1310 |
+ |
unexpectedException(); |
1311 |
+ |
} finally { |
1312 |
+ |
joinPool(e); |
1313 |
+ |
} |
1314 |
+ |
} |
1315 |
+ |
|
1316 |
+ |
/** |
1317 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
1318 |
+ |
*/ |
1319 |
+ |
public void testTimedInvokeAny3() { |
1320 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1321 |
+ |
try { |
1322 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1323 |
+ |
l.add(new StringTask()); |
1324 |
+ |
l.add(null); |
1325 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1326 |
+ |
} catch (NullPointerException success) { |
1327 |
+ |
} catch(Exception ex) { |
1328 |
+ |
ex.printStackTrace(); |
1329 |
+ |
unexpectedException(); |
1330 |
+ |
} finally { |
1331 |
+ |
joinPool(e); |
1332 |
+ |
} |
1333 |
+ |
} |
1334 |
+ |
|
1335 |
+ |
/** |
1336 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
1337 |
+ |
*/ |
1338 |
+ |
public void testTimedInvokeAny4() { |
1339 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1340 |
+ |
try { |
1341 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1342 |
+ |
l.add(new NPETask()); |
1343 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1344 |
+ |
} catch(ExecutionException success) { |
1345 |
+ |
} catch(Exception ex) { |
1346 |
+ |
unexpectedException(); |
1347 |
+ |
} finally { |
1348 |
+ |
joinPool(e); |
1349 |
+ |
} |
1350 |
+ |
} |
1351 |
+ |
|
1352 |
+ |
/** |
1353 |
+ |
* timed invokeAny(c) returns result of some task |
1354 |
+ |
*/ |
1355 |
+ |
public void testTimedInvokeAny5() { |
1356 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1357 |
+ |
try { |
1358 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1359 |
+ |
l.add(new StringTask()); |
1360 |
+ |
l.add(new StringTask()); |
1361 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1362 |
+ |
assertSame(TEST_STRING, result); |
1363 |
+ |
} catch (ExecutionException success) { |
1364 |
+ |
} catch(Exception ex) { |
1365 |
+ |
unexpectedException(); |
1366 |
+ |
} finally { |
1367 |
+ |
joinPool(e); |
1368 |
+ |
} |
1369 |
+ |
} |
1370 |
+ |
|
1371 |
+ |
/** |
1372 |
+ |
* timed invokeAll(null) throws NPE |
1373 |
+ |
*/ |
1374 |
+ |
public void testTimedInvokeAll1() { |
1375 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1376 |
+ |
try { |
1377 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1378 |
+ |
} catch (NullPointerException success) { |
1379 |
+ |
} catch(Exception ex) { |
1380 |
+ |
unexpectedException(); |
1381 |
+ |
} finally { |
1382 |
+ |
joinPool(e); |
1383 |
+ |
} |
1384 |
+ |
} |
1385 |
+ |
|
1386 |
+ |
/** |
1387 |
+ |
* timed invokeAll(,,null) throws NPE |
1388 |
+ |
*/ |
1389 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
1390 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1391 |
+ |
try { |
1392 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1393 |
+ |
l.add(new StringTask()); |
1394 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1395 |
+ |
} catch (NullPointerException success) { |
1396 |
+ |
} catch(Exception ex) { |
1397 |
+ |
unexpectedException(); |
1398 |
+ |
} finally { |
1399 |
+ |
joinPool(e); |
1400 |
+ |
} |
1401 |
+ |
} |
1402 |
+ |
|
1403 |
+ |
/** |
1404 |
+ |
* timed invokeAll(empty collection) returns empty collection |
1405 |
+ |
*/ |
1406 |
+ |
public void testTimedInvokeAll2() { |
1407 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1408 |
+ |
try { |
1409 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1410 |
+ |
assertTrue(r.isEmpty()); |
1411 |
+ |
} catch(Exception ex) { |
1412 |
+ |
unexpectedException(); |
1413 |
+ |
} finally { |
1414 |
+ |
joinPool(e); |
1415 |
+ |
} |
1416 |
+ |
} |
1417 |
+ |
|
1418 |
+ |
/** |
1419 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
1420 |
+ |
*/ |
1421 |
+ |
public void testTimedInvokeAll3() { |
1422 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1423 |
+ |
try { |
1424 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1425 |
+ |
l.add(new StringTask()); |
1426 |
+ |
l.add(null); |
1427 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1428 |
+ |
} catch (NullPointerException success) { |
1429 |
+ |
} catch(Exception ex) { |
1430 |
+ |
unexpectedException(); |
1431 |
+ |
} finally { |
1432 |
+ |
joinPool(e); |
1433 |
+ |
} |
1434 |
+ |
} |
1435 |
+ |
|
1436 |
+ |
/** |
1437 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1438 |
+ |
*/ |
1439 |
+ |
public void testTimedInvokeAll4() { |
1440 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1441 |
+ |
try { |
1442 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1443 |
+ |
l.add(new NPETask()); |
1444 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1445 |
+ |
assertEquals(1, result.size()); |
1446 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1447 |
+ |
it.next().get(); |
1448 |
+ |
} catch(ExecutionException success) { |
1449 |
+ |
} catch(Exception ex) { |
1450 |
+ |
unexpectedException(); |
1451 |
+ |
} finally { |
1452 |
+ |
joinPool(e); |
1453 |
+ |
} |
1454 |
+ |
} |
1455 |
+ |
|
1456 |
+ |
/** |
1457 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1458 |
+ |
*/ |
1459 |
+ |
public void testTimedInvokeAll5() { |
1460 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1461 |
+ |
try { |
1462 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1463 |
+ |
l.add(new StringTask()); |
1464 |
+ |
l.add(new StringTask()); |
1465 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1466 |
+ |
assertEquals(2, result.size()); |
1467 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1468 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1469 |
+ |
} catch (ExecutionException success) { |
1470 |
+ |
} catch(Exception ex) { |
1471 |
+ |
unexpectedException(); |
1472 |
+ |
} finally { |
1473 |
+ |
joinPool(e); |
1474 |
+ |
} |
1475 |
+ |
} |
1476 |
+ |
|
1477 |
+ |
/** |
1478 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1479 |
+ |
*/ |
1480 |
+ |
public void testTimedInvokeAll6() { |
1481 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1482 |
+ |
try { |
1483 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1484 |
+ |
l.add(new StringTask()); |
1485 |
+ |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1486 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1487 |
+ |
assertEquals(2, result.size()); |
1488 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1489 |
+ |
Future<String> f1 = it.next(); |
1490 |
+ |
Future<String> f2 = it.next(); |
1491 |
+ |
assertTrue(f1.isDone()); |
1492 |
+ |
assertFalse(f1.isCancelled()); |
1493 |
+ |
assertTrue(f2.isDone()); |
1494 |
+ |
assertTrue(f2.isCancelled()); |
1495 |
+ |
} catch(Exception ex) { |
1496 |
+ |
unexpectedException(); |
1497 |
+ |
} finally { |
1498 |
|
joinPool(e); |
1499 |
|
} |
1500 |
|
} |