ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.12 by dl, Mon Dec 22 00:48:56 2003 UTC vs.
Revision 1.22 by dl, Mon May 9 17:15:27 2005 UTC

# Line 1 | Line 1
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.*;
# Line 35 | Line 36 | public class ThreadPoolExecutorTest exte
36          }
37      }
38  
39 +    static class FailingThreadFactory implements ThreadFactory{
40 +        int calls = 0;
41 +        public Thread newThread(Runnable r){
42 +            if (++calls > 1) return null;
43 +            return new Thread(r);
44 +        }  
45 +    }
46 +    
47 +
48      /**
49       *  execute successfully executes a runnable
50       */
# Line 116 | Line 126 | public class ThreadPoolExecutorTest exte
126              unexpectedException();
127          }
128          assertEquals(1, p2.getCompletedTaskCount());
129 <        p2.shutdown();
129 >        try { p2.shutdown(); } catch(SecurityException ok) { return; }
130          joinPool(p2);
131      }
132      
# Line 146 | Line 156 | public class ThreadPoolExecutorTest exte
156          ThreadFactory tf = new SimpleThreadFactory();
157          ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
158          assertSame(tf, p.getThreadFactory());
149        p.shutdown();
159          joinPool(p);
160      }
161  
# Line 158 | Line 167 | public class ThreadPoolExecutorTest exte
167          ThreadFactory tf = new SimpleThreadFactory();
168          p.setThreadFactory(tf);
169          assertSame(tf, p.getThreadFactory());
161        p.shutdown();
170          joinPool(p);
171      }
172  
# Line 184 | Line 192 | public class ThreadPoolExecutorTest exte
192          RejectedExecutionHandler h = new NoOpREHandler();
193          ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
194          assertSame(h, p.getRejectedExecutionHandler());
187        p.shutdown();
195          joinPool(p);
196      }
197  
# Line 197 | Line 204 | public class ThreadPoolExecutorTest exte
204          RejectedExecutionHandler h = new NoOpREHandler();
205          p.setRejectedExecutionHandler(h);
206          assertSame(h, p.getRejectedExecutionHandler());
200        p.shutdown();
207          joinPool(p);
208      }
209  
# Line 280 | Line 286 | public class ThreadPoolExecutorTest exte
286          
287          ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
288          assertFalse(p1.isShutdown());
289 <        p1.shutdown();
289 >        try { p1.shutdown(); } catch(SecurityException ok) { return; }
290          assertTrue(p1.isShutdown());
291          joinPool(p1);
292      }
# Line 295 | Line 301 | public class ThreadPoolExecutorTest exte
301          try {
302              p1.execute(new MediumRunnable());
303          } finally {
304 <            p1.shutdown();
304 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
305          }
306          try {
307              assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 315 | Line 321 | public class ThreadPoolExecutorTest exte
321              p1.execute(new SmallRunnable());
322              assertFalse(p1.isTerminating());
323          } finally {
324 <            p1.shutdown();
324 >            try { p1.shutdown(); } catch(SecurityException ok) { return; }
325          }
326          try {
327              assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 343 | Line 349 | public class ThreadPoolExecutorTest exte
349              assertSame(q, wq);
350              assertFalse(wq.contains(tasks[0]));
351              assertTrue(wq.contains(tasks[4]));
352 +            for (int i = 1; i < 5; ++i)
353 +                tasks[i].cancel(true);
354              p1.shutdownNow();
355          } catch(Exception e) {
356              unexpectedException();
# Line 373 | Line 381 | public class ThreadPoolExecutorTest exte
381              assertTrue(q.contains(tasks[3]));
382              assertTrue(p1.remove(tasks[3]));
383              assertFalse(q.contains(tasks[3]));
376            p1.shutdownNow();
384          } catch(Exception e) {
385              unexpectedException();
386          } finally {
# Line 396 | Line 403 | public class ThreadPoolExecutorTest exte
403          p1.purge();
404          long count = p1.getTaskCount();
405          assertTrue(count >= 2 && count < 5);
399        p1.shutdownNow();
406          joinPool(p1);
407      }
408  
# Line 411 | Line 417 | public class ThreadPoolExecutorTest exte
417                  p1.execute(new MediumPossiblyInterruptedRunnable());
418          }
419          finally {
420 <            l = p1.shutdownNow();
420 >            try {
421 >                l = p1.shutdownNow();
422 >            } catch (SecurityException ok) { return; }
423 >            
424          }
425          assertTrue(p1.isShutdown());
426          assertTrue(l.size() <= 4);
# Line 772 | Line 781 | public class ThreadPoolExecutorTest exte
781              for(int i = 1; i < 5; ++i) {
782                  assertTrue(tasks[i].done);
783              }
784 <            p.shutdownNow();
784 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
785          } catch(RejectedExecutionException ex){
786              unexpectedException();
787          } finally {
# Line 799 | Line 808 | public class ThreadPoolExecutorTest exte
808              for(int i = 0; i < 5; ++i){
809                  assertFalse(tasks[i].done);
810              }
811 <            p.shutdownNow();
811 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
812          } catch(RejectedExecutionException ex){
813              unexpectedException();
814          } finally {
# Line 822 | Line 831 | public class ThreadPoolExecutorTest exte
831              p.execute(r3);
832              assertFalse(p.getQueue().contains(r2));
833              assertTrue(p.getQueue().contains(r3));
834 <            p.shutdownNow();
834 >            try { p.shutdownNow(); } catch(SecurityException ok) { return; }
835          } catch(RejectedExecutionException ex){
836              unexpectedException();
837          } finally {
# Line 836 | Line 845 | public class ThreadPoolExecutorTest exte
845      public void testRejectedExecutionExceptionOnShutdown() {
846          ThreadPoolExecutor tpe =
847              new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
848 <        tpe.shutdown();
848 >        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
849          try {
850              tpe.execute(new NoOpRunnable());
851              shouldThrow();
# Line 852 | Line 861 | public class ThreadPoolExecutorTest exte
861          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
862          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
863  
864 <        p.shutdown();
864 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
865          try {
866              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
867              p.execute(r);
# Line 871 | Line 880 | public class ThreadPoolExecutorTest exte
880          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
881          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
882  
883 <        p.shutdown();
883 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
884          try {
885              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
886              p.execute(r);
# Line 891 | Line 900 | public class ThreadPoolExecutorTest exte
900          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
901          ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
902  
903 <        p.shutdown();
903 >        try { p.shutdown(); } catch(SecurityException ok) { return; }
904          try {
905              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
906              p.execute(r);
# Line 931 | Line 940 | public class ThreadPoolExecutorTest exte
940              shouldThrow();
941          } catch(IllegalArgumentException success){
942          } finally {
943 <            tpe.shutdown();
943 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
944          }
945          joinPool(tpe);
946      }  
# Line 950 | Line 959 | public class ThreadPoolExecutorTest exte
959              shouldThrow();
960          } catch(IllegalArgumentException success){
961          } finally {
962 <            tpe.shutdown();
962 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
963          }
964          joinPool(tpe);
965      }
# Line 969 | Line 978 | public class ThreadPoolExecutorTest exte
978              shouldThrow();
979          } catch(IllegalArgumentException success){
980          } finally {
981 <            tpe.shutdown();
981 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
982          }
983          joinPool(tpe);
984      }
# Line 990 | Line 999 | public class ThreadPoolExecutorTest exte
999              shouldThrow();
1000          } catch(IllegalArgumentException success){
1001          } finally {
1002 <            tpe.shutdown();
1002 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1003          }
1004          joinPool(tpe);
1005      }
# Line 1000 | Line 1009 | public class ThreadPoolExecutorTest exte
1009       */
1010      public void testTerminated() {
1011          ExtendedTPE tpe = new ExtendedTPE();
1012 <        tpe.shutdown();
1012 >        try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1013          assertTrue(tpe.terminatedCalled);
1014          joinPool(tpe);
1015      }
# Line 1017 | Line 1026 | public class ThreadPoolExecutorTest exte
1026              assertTrue(r.done);
1027              assertTrue(tpe.beforeCalled);
1028              assertTrue(tpe.afterCalled);
1029 <            tpe.shutdown();
1029 >            try { tpe.shutdown(); } catch(SecurityException ok) { return; }
1030          }
1031          catch(Exception ex) {
1032              unexpectedException();
# Line 1264 | Line 1273 | public class ThreadPoolExecutorTest exte
1273      }
1274  
1275  
1276 +
1277 +    /**
1278 +     * timed invokeAny(null) throws NPE
1279 +     */
1280 +    public void testTimedInvokeAny1() {
1281 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1282 +        try {
1283 +            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1284 +        } catch (NullPointerException success) {
1285 +        } catch(Exception ex) {
1286 +            unexpectedException();
1287 +        } finally {
1288 +            joinPool(e);
1289 +        }
1290 +    }
1291 +
1292 +    /**
1293 +     * timed invokeAny(,,null) throws NPE
1294 +     */
1295 +    public void testTimedInvokeAnyNullTimeUnit() {
1296 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1297 +        try {
1298 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1299 +            l.add(new StringTask());
1300 +            e.invokeAny(l, MEDIUM_DELAY_MS, null);
1301 +        } catch (NullPointerException success) {
1302 +        } catch(Exception ex) {
1303 +            unexpectedException();
1304 +        } finally {
1305 +            joinPool(e);
1306 +        }
1307 +    }
1308 +
1309 +    /**
1310 +     * timed invokeAny(empty collection) throws IAE
1311 +     */
1312 +    public void testTimedInvokeAny2() {
1313 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1314 +        try {
1315 +            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1316 +        } catch (IllegalArgumentException success) {
1317 +        } catch(Exception ex) {
1318 +            unexpectedException();
1319 +        } finally {
1320 +            joinPool(e);
1321 +        }
1322 +    }
1323 +
1324 +    /**
1325 +     * timed invokeAny(c) throws NPE if c has null elements
1326 +     */
1327 +    public void testTimedInvokeAny3() {
1328 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1329 +        try {
1330 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1331 +            l.add(new StringTask());
1332 +            l.add(null);
1333 +            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1334 +        } catch (NullPointerException success) {
1335 +        } catch(Exception ex) {
1336 +            ex.printStackTrace();
1337 +            unexpectedException();
1338 +        } finally {
1339 +            joinPool(e);
1340 +        }
1341 +    }
1342 +
1343 +    /**
1344 +     * timed invokeAny(c) throws ExecutionException if no task completes
1345 +     */
1346 +    public void testTimedInvokeAny4() {
1347 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1348 +        try {
1349 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1350 +            l.add(new NPETask());
1351 +            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1352 +        } catch(ExecutionException success) {
1353 +        } catch(Exception ex) {
1354 +            unexpectedException();
1355 +        } finally {
1356 +            joinPool(e);
1357 +        }
1358 +    }
1359 +
1360 +    /**
1361 +     * timed invokeAny(c) returns result of some task
1362 +     */
1363 +    public void testTimedInvokeAny5() {
1364 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1365 +        try {
1366 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1367 +            l.add(new StringTask());
1368 +            l.add(new StringTask());
1369 +            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1370 +            assertSame(TEST_STRING, result);
1371 +        } catch (ExecutionException success) {
1372 +        } catch(Exception ex) {
1373 +            unexpectedException();
1374 +        } finally {
1375 +            joinPool(e);
1376 +        }
1377 +    }
1378 +
1379 +    /**
1380 +     * timed invokeAll(null) throws NPE
1381 +     */
1382 +    public void testTimedInvokeAll1() {
1383 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1384 +        try {
1385 +            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1386 +        } catch (NullPointerException success) {
1387 +        } catch(Exception ex) {
1388 +            unexpectedException();
1389 +        } finally {
1390 +            joinPool(e);
1391 +        }
1392 +    }
1393 +
1394 +    /**
1395 +     * timed invokeAll(,,null) throws NPE
1396 +     */
1397 +    public void testTimedInvokeAllNullTimeUnit() {
1398 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1399 +        try {
1400 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1401 +            l.add(new StringTask());
1402 +            e.invokeAll(l, MEDIUM_DELAY_MS, null);
1403 +        } catch (NullPointerException success) {
1404 +        } catch(Exception ex) {
1405 +            unexpectedException();
1406 +        } finally {
1407 +            joinPool(e);
1408 +        }
1409 +    }
1410 +
1411 +    /**
1412 +     * timed invokeAll(empty collection) returns empty collection
1413 +     */
1414 +    public void testTimedInvokeAll2() {
1415 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1416 +        try {
1417 +            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1418 +            assertTrue(r.isEmpty());
1419 +        } catch(Exception ex) {
1420 +            unexpectedException();
1421 +        } finally {
1422 +            joinPool(e);
1423 +        }
1424 +    }
1425 +
1426 +    /**
1427 +     * timed invokeAll(c) throws NPE if c has null elements
1428 +     */
1429 +    public void testTimedInvokeAll3() {
1430 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1431 +        try {
1432 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1433 +            l.add(new StringTask());
1434 +            l.add(null);
1435 +            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1436 +        } catch (NullPointerException success) {
1437 +        } catch(Exception ex) {
1438 +            unexpectedException();
1439 +        } finally {
1440 +            joinPool(e);
1441 +        }
1442 +    }
1443 +
1444 +    /**
1445 +     * get of element of invokeAll(c) throws exception on failed task
1446 +     */
1447 +    public void testTimedInvokeAll4() {
1448 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1449 +        try {
1450 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1451 +            l.add(new NPETask());
1452 +            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1453 +            assertEquals(1, result.size());
1454 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1455 +                it.next().get();
1456 +        } catch(ExecutionException success) {
1457 +        } catch(Exception ex) {
1458 +            unexpectedException();
1459 +        } finally {
1460 +            joinPool(e);
1461 +        }
1462 +    }
1463 +
1464 +    /**
1465 +     * timed invokeAll(c) returns results of all completed tasks
1466 +     */
1467 +    public void testTimedInvokeAll5() {
1468 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1469 +        try {
1470 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1471 +            l.add(new StringTask());
1472 +            l.add(new StringTask());
1473 +            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1474 +            assertEquals(2, result.size());
1475 +            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1476 +                assertSame(TEST_STRING, it.next().get());
1477 +        } catch (ExecutionException success) {
1478 +        } catch(Exception ex) {
1479 +            unexpectedException();
1480 +        } finally {
1481 +            joinPool(e);
1482 +        }
1483 +    }
1484 +
1485 +    /**
1486 +     * timed invokeAll(c) cancels tasks not completed by timeout
1487 +     */
1488 +    public void testTimedInvokeAll6() {
1489 +        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1490 +        try {
1491 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1492 +            l.add(new StringTask());
1493 +            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1494 +            l.add(new StringTask());
1495 +            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1496 +            assertEquals(3, result.size());
1497 +            Iterator<Future<String>> it = result.iterator();
1498 +            Future<String> f1 = it.next();
1499 +            Future<String> f2 = it.next();
1500 +            Future<String> f3 = it.next();
1501 +            assertTrue(f1.isDone());
1502 +            assertTrue(f2.isDone());
1503 +            assertTrue(f3.isDone());
1504 +            assertFalse(f1.isCancelled());
1505 +            assertTrue(f2.isCancelled());
1506 +        } catch(Exception ex) {
1507 +            unexpectedException();
1508 +        } finally {
1509 +            joinPool(e);
1510 +        }
1511 +    }
1512 +
1513 +    /**
1514 +     * Execution continues if there is at least one thread even if
1515 +     * thread factory fails to create more
1516 +     */
1517 +    public void testFailingThreadFactory() {
1518 +        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1519 +        try {
1520 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1521 +            for (int k = 0; k < 100; ++k) {
1522 +                e.execute(new NoOpRunnable());
1523 +            }
1524 +            Thread.sleep(LONG_DELAY_MS);
1525 +        } catch(Exception ex) {
1526 +            unexpectedException();
1527 +        } finally {
1528 +            joinPool(e);
1529 +        }
1530 +    }
1531 +
1532 +    /**
1533 +     * allowsCoreThreadTimeOut is by default false.
1534 +     */
1535 +    public void testAllowsCoreThreadTimeOut() {
1536 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1537 +        assertFalse(tpe.allowsCoreThreadTimeOut());
1538 +        joinPool(tpe);
1539 +    }
1540 +
1541 +    /**
1542 +     * allowCoreThreadTimeOut(true) causes idle threads to time out
1543 +     */
1544 +    public void testAllowCoreThreadTimeOut_true() {
1545 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1546 +        tpe.allowCoreThreadTimeOut(true);
1547 +        tpe.execute(new NoOpRunnable());
1548 +        try {
1549 +            Thread.sleep(MEDIUM_DELAY_MS);
1550 +            assertEquals(0, tpe.getPoolSize());
1551 +        } catch(InterruptedException e){
1552 +            unexpectedException();
1553 +        } finally {
1554 +            joinPool(tpe);
1555 +        }
1556 +    }
1557 +
1558 +    /**
1559 +     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1560 +     */
1561 +    public void testAllowCoreThreadTimeOut_false() {
1562 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1563 +        tpe.allowCoreThreadTimeOut(false);
1564 +        tpe.execute(new NoOpRunnable());
1565 +        try {
1566 +            Thread.sleep(MEDIUM_DELAY_MS);
1567 +            assertTrue(tpe.getPoolSize() >= 1);
1568 +        } catch(InterruptedException e){
1569 +            unexpectedException();
1570 +        } finally {
1571 +            joinPool(tpe);
1572 +        }
1573 +    }
1574 +
1575   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines