--- jsr166/src/test/tck/ThreadPoolExecutorTest.java 2003/12/28 21:56:18 1.16 +++ jsr166/src/test/tck/ThreadPoolExecutorTest.java 2009/11/16 05:30:08 1.26 @@ -2,22 +2,23 @@ * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ import java.util.concurrent.*; +import java.util.concurrent.atomic.*; import junit.framework.*; import java.util.*; public class ThreadPoolExecutorTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { return new TestSuite(ThreadPoolExecutorTest.class); } - + static class ExtendedTPE extends ThreadPoolExecutor { volatile boolean beforeCalled = false; volatile boolean afterCalled = false; @@ -36,6 +37,15 @@ public class ThreadPoolExecutorTest exte } } + static class FailingThreadFactory implements ThreadFactory { + int calls = 0; + public Thread newThread(Runnable r) { + if (++calls > 1) return null; + return new Thread(r); + } + } + + /** * execute successfully executes a runnable */ @@ -46,15 +56,15 @@ public class ThreadPoolExecutorTest exte public void run() { try { Thread.sleep(SHORT_DELAY_MS); - } catch(InterruptedException e){ + } catch (InterruptedException e) { threadUnexpectedException(); } } }); Thread.sleep(SMALL_DELAY_MS); - } catch(InterruptedException e){ + } catch (InterruptedException e) { unexpectedException(); - } + } joinPool(p1); } @@ -68,7 +78,7 @@ public class ThreadPoolExecutorTest exte p2.execute(new MediumRunnable()); try { Thread.sleep(SHORT_DELAY_MS); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); } assertEquals(1, p2.getActiveCount()); @@ -102,7 +112,7 @@ public class ThreadPoolExecutorTest exte assertEquals(2, p2.getPoolSize()); joinPool(p2); } - + /** * getCompletedTaskCount increases, but doesn't overestimate, * when tasks complete @@ -113,14 +123,14 @@ public class ThreadPoolExecutorTest exte p2.execute(new ShortRunnable()); try { Thread.sleep(SMALL_DELAY_MS); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); } assertEquals(1, p2.getCompletedTaskCount()); - p2.shutdown(); + try { p2.shutdown(); } catch (SecurityException ok) { return; } joinPool(p2); } - + /** * getCorePoolSize returns size given in constructor if not otherwise set */ @@ -129,7 +139,7 @@ public class ThreadPoolExecutorTest exte assertEquals(1, p1.getCorePoolSize()); joinPool(p1); } - + /** * getKeepAliveTime returns value given in constructor if not otherwise set */ @@ -140,18 +150,17 @@ public class ThreadPoolExecutorTest exte } - /** + /** * getThreadFactory returns factory in constructor if not set */ public void testGetThreadFactory() { ThreadFactory tf = new SimpleThreadFactory(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10), tf, new NoOpREHandler()); assertSame(tf, p.getThreadFactory()); - p.shutdown(); joinPool(p); } - /** + /** * setThreadFactory sets the thread factory returned by getThreadFactory */ public void testSetThreadFactory() { @@ -159,12 +168,11 @@ public class ThreadPoolExecutorTest exte ThreadFactory tf = new SimpleThreadFactory(); p.setThreadFactory(tf); assertSame(tf, p.getThreadFactory()); - p.shutdown(); joinPool(p); } - /** + /** * setThreadFactory(null) throws NPE */ public void testSetThreadFactoryNull() { @@ -178,18 +186,17 @@ public class ThreadPoolExecutorTest exte } } - /** + /** * getRejectedExecutionHandler returns handler in constructor if not set */ public void testGetRejectedExecutionHandler() { RejectedExecutionHandler h = new NoOpREHandler(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10), h); assertSame(h, p.getRejectedExecutionHandler()); - p.shutdown(); joinPool(p); } - /** + /** * setRejectedExecutionHandler sets the handler returned by * getRejectedExecutionHandler */ @@ -198,12 +205,11 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new NoOpREHandler(); p.setRejectedExecutionHandler(h); assertSame(h, p.getRejectedExecutionHandler()); - p.shutdown(); joinPool(p); } - /** + /** * setRejectedExecutionHandler(null) throws NPE */ public void testSetRejectedExecutionHandlerNull() { @@ -217,7 +223,7 @@ public class ThreadPoolExecutorTest exte } } - + /** * getLargestPoolSize increases, but doesn't overestimate, when * multiple threads active @@ -230,12 +236,12 @@ public class ThreadPoolExecutorTest exte p2.execute(new MediumRunnable()); Thread.sleep(SHORT_DELAY_MS); assertEquals(2, p2.getLargestPoolSize()); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); - } + } joinPool(p2); } - + /** * getMaximumPoolSize returns value given in constructor if not * otherwise set @@ -245,7 +251,7 @@ public class ThreadPoolExecutorTest exte assertEquals(2, p2.getMaximumPoolSize()); joinPool(p2); } - + /** * getPoolSize increases, but doesn't overestimate, when threads * become active @@ -257,7 +263,7 @@ public class ThreadPoolExecutorTest exte assertEquals(1, p1.getPoolSize()); joinPool(p1); } - + /** * getTaskCount increases, but doesn't overestimate, when tasks submitted */ @@ -268,25 +274,25 @@ public class ThreadPoolExecutorTest exte p1.execute(new MediumRunnable()); Thread.sleep(SHORT_DELAY_MS); assertEquals(1, p1.getTaskCount()); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); - } + } joinPool(p1); } - + /** * isShutDown is false before shutdown, true after */ public void testIsShutdown() { - + ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); assertFalse(p1.isShutdown()); - p1.shutdown(); + try { p1.shutdown(); } catch (SecurityException ok) { return; } assertTrue(p1.isShutdown()); joinPool(p1); } - + /** * isTerminated is false before termination, true after */ @@ -296,14 +302,14 @@ public class ThreadPoolExecutorTest exte try { p1.execute(new MediumRunnable()); } finally { - p1.shutdown(); + try { p1.shutdown(); } catch (SecurityException ok) { return; } } try { assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); assertTrue(p1.isTerminated()); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); - } + } } /** @@ -316,15 +322,15 @@ public class ThreadPoolExecutorTest exte p1.execute(new SmallRunnable()); assertFalse(p1.isTerminating()); } finally { - p1.shutdown(); + try { p1.shutdown(); } catch (SecurityException ok) { return; } } try { assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); assertTrue(p1.isTerminated()); assertFalse(p1.isTerminating()); - } catch(Exception e){ + } catch (Exception e) { unexpectedException(); - } + } } /** @@ -334,7 +340,7 @@ public class ThreadPoolExecutorTest exte BlockingQueue q = new ArrayBlockingQueue(10); ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); FutureTask[] tasks = new FutureTask[5]; - for(int i = 0; i < 5; i++){ + for (int i = 0; i < 5; i++) { tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); p1.execute(tasks[i]); } @@ -344,8 +350,10 @@ public class ThreadPoolExecutorTest exte assertSame(q, wq); assertFalse(wq.contains(tasks[0])); assertTrue(wq.contains(tasks[4])); + for (int i = 1; i < 5; ++i) + tasks[i].cancel(true); p1.shutdownNow(); - } catch(Exception e) { + } catch (Exception e) { unexpectedException(); } finally { joinPool(p1); @@ -359,7 +367,7 @@ public class ThreadPoolExecutorTest exte BlockingQueue q = new ArrayBlockingQueue(10); ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); FutureTask[] tasks = new FutureTask[5]; - for(int i = 0; i < 5; i++){ + for (int i = 0; i < 5; i++) { tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); p1.execute(tasks[i]); } @@ -374,8 +382,7 @@ public class ThreadPoolExecutorTest exte assertTrue(q.contains(tasks[3])); assertTrue(p1.remove(tasks[3])); assertFalse(q.contains(tasks[3])); - p1.shutdownNow(); - } catch(Exception e) { + } catch (Exception e) { unexpectedException(); } finally { joinPool(p1); @@ -388,7 +395,7 @@ public class ThreadPoolExecutorTest exte public void testPurge() { ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); FutureTask[] tasks = new FutureTask[5]; - for(int i = 0; i < 5; i++){ + for (int i = 0; i < 5; i++) { tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); p1.execute(tasks[i]); } @@ -397,7 +404,6 @@ public class ThreadPoolExecutorTest exte p1.purge(); long count = p1.getTaskCount(); assertTrue(count >= 2 && count < 5); - p1.shutdownNow(); joinPool(p1); } @@ -408,154 +414,157 @@ public class ThreadPoolExecutorTest exte ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); List l; try { - for(int i = 0; i < 5; i++) + for (int i = 0; i < 5; i++) p1.execute(new MediumPossiblyInterruptedRunnable()); } finally { - l = p1.shutdownNow(); + try { + l = p1.shutdownNow(); + } catch (SecurityException ok) { return; } + } assertTrue(p1.isShutdown()); assertTrue(l.size() <= 4); } // Exception Tests - - /** - * Constructor throws if corePoolSize argument is less than zero + + /** + * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor1() { try { new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - - /** - * Constructor throws if maximumPoolSize is less than zero + + /** + * Constructor throws if maximumPoolSize is less than zero */ public void testConstructor2() { try { new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - - /** - * Constructor throws if maximumPoolSize is equal to zero + + /** + * Constructor throws if maximumPoolSize is equal to zero */ public void testConstructor3() { try { new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero */ public void testConstructor4() { try { new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize */ public void testConstructor5() { try { new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - - /** - * Constructor throws if workQueue is set to null + + /** + * Constructor throws if workQueue is set to null */ public void testConstructorNullPointerException() { try { new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success) {} } - - - /** - * Constructor throws if corePoolSize argument is less than zero + + + /** + * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor6() { try { new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); shouldThrow(); - } catch (IllegalArgumentException success){} + } catch (IllegalArgumentException success) {} } - - /** - * Constructor throws if maximumPoolSize is less than zero + + /** + * Constructor throws if maximumPoolSize is less than zero */ public void testConstructor7() { try { new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero */ public void testConstructor8() { try { new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero */ public void testConstructor9() { try { new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize */ public void testConstructor10() { try { new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if workQueue is set to null + /** + * Constructor throws if workQueue is set to null */ public void testConstructorNullPointerException2() { try { new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success) {} } - /** - * Constructor throws if threadFactory is set to null + /** + * Constructor throws if threadFactory is set to null */ public void testConstructorNullPointerException3() { try { @@ -563,78 +572,78 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success) {} } - - - /** - * Constructor throws if corePoolSize argument is less than zero + + + /** + * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor11() { try { new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if maximumPoolSize is less than zero + /** + * Constructor throws if maximumPoolSize is less than zero */ public void testConstructor12() { try { new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero */ public void testConstructor13() { try { new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero */ public void testConstructor14() { try { new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize */ public void testConstructor15() { try { new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if workQueue is set to null + /** + * Constructor throws if workQueue is set to null */ public void testConstructorNullPointerException4() { try { new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success) {} } - /** - * Constructor throws if handler is set to null + /** + * Constructor throws if handler is set to null */ public void testConstructorNullPointerException5() { try { @@ -642,78 +651,78 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),r); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success) {} } - - /** - * Constructor throws if corePoolSize argument is less than zero + + /** + * Constructor throws if corePoolSize argument is less than zero */ public void testConstructor16() { try { new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if maximumPoolSize is less than zero + /** + * Constructor throws if maximumPoolSize is less than zero */ public void testConstructor17() { try { new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if maximumPoolSize is equal to zero + /** + * Constructor throws if maximumPoolSize is equal to zero */ public void testConstructor18() { try { new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if keepAliveTime is less than zero + /** + * Constructor throws if keepAliveTime is less than zero */ public void testConstructor19() { try { new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if corePoolSize is greater than the maximumPoolSize + /** + * Constructor throws if corePoolSize is greater than the maximumPoolSize */ public void testConstructor20() { try { new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10),new SimpleThreadFactory(),new NoOpREHandler()); shouldThrow(); } - catch (IllegalArgumentException success){} + catch (IllegalArgumentException success) {} } - /** - * Constructor throws if workQueue is set to null + /** + * Constructor throws if workQueue is set to null */ public void testConstructorNullPointerException6() { try { new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success) {} } - /** - * Constructor throws if handler is set to null + /** + * Constructor throws if handler is set to null */ public void testConstructorNullPointerException7() { try { @@ -721,11 +730,11 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),new SimpleThreadFactory(),r); shouldThrow(); } - catch (NullPointerException success){} + catch (NullPointerException success) {} } - /** - * Constructor throws if ThreadFactory is set top null + /** + * Constructor throws if ThreadFactory is set top null */ public void testConstructorNullPointerException8() { try { @@ -733,9 +742,9 @@ public class ThreadPoolExecutorTest exte new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10),f,new NoOpREHandler()); shouldThrow(); } - catch (NullPointerException successdn8){} + catch (NullPointerException successdn8) {} } - + /** * execute throws RejectedExecutionException @@ -744,12 +753,12 @@ public class ThreadPoolExecutorTest exte public void testSaturatedExecute() { ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1)); try { - - for(int i = 0; i < 5; ++i){ + + for (int i = 0; i < 5; ++i) { p.execute(new MediumRunnable()); } shouldThrow(); - } catch(RejectedExecutionException success){} + } catch (RejectedExecutionException success) {} joinPool(p); } @@ -760,21 +769,21 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); try { - + TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; - for(int i = 0; i < 5; ++i){ + for (int i = 0; i < 5; ++i) { tasks[i] = new TrackedNoOpRunnable(); } TrackedLongRunnable mr = new TrackedLongRunnable(); p.execute(mr); - for(int i = 0; i < 5; ++i){ + for (int i = 0; i < 5; ++i) { p.execute(tasks[i]); } - for(int i = 1; i < 5; ++i) { + for (int i = 1; i < 5; ++i) { assertTrue(tasks[i].done); } - p.shutdownNow(); - } catch(RejectedExecutionException ex){ + try { p.shutdownNow(); } catch (SecurityException ok) { return; } + } catch (RejectedExecutionException ex) { unexpectedException(); } finally { joinPool(p); @@ -788,20 +797,20 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); try { - + TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; - for(int i = 0; i < 5; ++i){ + for (int i = 0; i < 5; ++i) { tasks[i] = new TrackedNoOpRunnable(); } p.execute(new TrackedLongRunnable()); - for(int i = 0; i < 5; ++i){ + for (int i = 0; i < 5; ++i) { p.execute(tasks[i]); } - for(int i = 0; i < 5; ++i){ + for (int i = 0; i < 5; ++i) { assertFalse(tasks[i].done); } - p.shutdownNow(); - } catch(RejectedExecutionException ex){ + try { p.shutdownNow(); } catch (SecurityException ok) { return; } + } catch (RejectedExecutionException ex) { unexpectedException(); } finally { joinPool(p); @@ -823,8 +832,8 @@ public class ThreadPoolExecutorTest exte p.execute(r3); assertFalse(p.getQueue().contains(r2)); assertTrue(p.getQueue().contains(r3)); - p.shutdownNow(); - } catch(RejectedExecutionException ex){ + try { p.shutdownNow(); } catch (SecurityException ok) { return; } + } catch (RejectedExecutionException ex) { unexpectedException(); } finally { joinPool(p); @@ -835,14 +844,14 @@ public class ThreadPoolExecutorTest exte * execute throws RejectedExecutionException if shutdown */ public void testRejectedExecutionExceptionOnShutdown() { - ThreadPoolExecutor tpe = + ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(1)); - tpe.shutdown(); + try { tpe.shutdown(); } catch (SecurityException ok) { return; } try { tpe.execute(new NoOpRunnable()); shouldThrow(); - } catch(RejectedExecutionException success){} - + } catch (RejectedExecutionException success) {} + joinPool(tpe); } @@ -853,12 +862,12 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); - p.shutdown(); + try { p.shutdown(); } catch (SecurityException ok) { return; } try { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); assertFalse(r.done); - } catch(RejectedExecutionException success){ + } catch (RejectedExecutionException success) { unexpectedException(); } finally { joinPool(p); @@ -872,12 +881,12 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); - p.shutdown(); + try { p.shutdown(); } catch (SecurityException ok) { return; } try { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); assertFalse(r.done); - } catch(RejectedExecutionException success){ + } catch (RejectedExecutionException success) { unexpectedException(); } finally { joinPool(p); @@ -892,12 +901,12 @@ public class ThreadPoolExecutorTest exte RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(1), h); - p.shutdown(); + try { p.shutdown(); } catch (SecurityException ok) { return; } try { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); assertFalse(r.done); - } catch(RejectedExecutionException success){ + } catch (RejectedExecutionException success) { unexpectedException(); } finally { joinPool(p); @@ -914,11 +923,11 @@ public class ThreadPoolExecutorTest exte tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); tpe.execute(null); shouldThrow(); - } catch(NullPointerException success){} - + } catch (NullPointerException success) {} + joinPool(tpe); } - + /** * setCorePoolSize of negative value throws IllegalArgumentException */ @@ -926,36 +935,36 @@ public class ThreadPoolExecutorTest exte ThreadPoolExecutor tpe = null; try { tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); - } catch(Exception e){} + } catch (Exception e) {} try { tpe.setCorePoolSize(-1); shouldThrow(); - } catch(IllegalArgumentException success){ + } catch (IllegalArgumentException success) { } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch (SecurityException ok) { return; } } joinPool(tpe); - } + } /** * setMaximumPoolSize(int) throws IllegalArgumentException if * given a value less the core pool size - */ + */ public void testMaximumPoolSizeIllegalArgumentException() { ThreadPoolExecutor tpe = null; try { tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); - } catch(Exception e){} + } catch (Exception e) {} try { tpe.setMaximumPoolSize(1); shouldThrow(); - } catch(IllegalArgumentException success){ + } catch (IllegalArgumentException success) { } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch (SecurityException ok) { return; } } joinPool(tpe); } - + /** * setMaximumPoolSize throws IllegalArgumentException * if given a negative value @@ -964,17 +973,17 @@ public class ThreadPoolExecutorTest exte ThreadPoolExecutor tpe = null; try { tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); - } catch(Exception e){} + } catch (Exception e) {} try { tpe.setMaximumPoolSize(-1); shouldThrow(); - } catch(IllegalArgumentException success){ + } catch (IllegalArgumentException success) { } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch (SecurityException ok) { return; } } joinPool(tpe); } - + /** * setKeepAliveTime throws IllegalArgumentException @@ -984,14 +993,14 @@ public class ThreadPoolExecutorTest exte ThreadPoolExecutor tpe = null; try { tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue(10)); - } catch(Exception e){} - + } catch (Exception e) {} + try { tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); shouldThrow(); - } catch(IllegalArgumentException success){ + } catch (IllegalArgumentException success) { } finally { - tpe.shutdown(); + try { tpe.shutdown(); } catch (SecurityException ok) { return; } } joinPool(tpe); } @@ -1001,7 +1010,7 @@ public class ThreadPoolExecutorTest exte */ public void testTerminated() { ExtendedTPE tpe = new ExtendedTPE(); - tpe.shutdown(); + try { tpe.shutdown(); } catch (SecurityException ok) { return; } assertTrue(tpe.terminatedCalled); joinPool(tpe); } @@ -1018,9 +1027,9 @@ public class ThreadPoolExecutorTest exte assertTrue(r.done); assertTrue(tpe.beforeCalled); assertTrue(tpe.afterCalled); - tpe.shutdown(); + try { tpe.shutdown(); } catch (SecurityException ok) { return; } } - catch(Exception ex) { + catch (Exception ex) { unexpectedException(); } finally { joinPool(tpe); @@ -1099,7 +1108,7 @@ public class ThreadPoolExecutorTest exte try { e.invokeAny(null); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1114,7 +1123,7 @@ public class ThreadPoolExecutorTest exte try { e.invokeAny(new ArrayList>()); } catch (IllegalArgumentException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1132,7 +1141,7 @@ public class ThreadPoolExecutorTest exte l.add(null); e.invokeAny(l); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1149,7 +1158,7 @@ public class ThreadPoolExecutorTest exte l.add(new NPETask()); e.invokeAny(l); } catch (ExecutionException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1168,7 +1177,7 @@ public class ThreadPoolExecutorTest exte String result = e.invokeAny(l); assertSame(TEST_STRING, result); } catch (ExecutionException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1183,7 +1192,7 @@ public class ThreadPoolExecutorTest exte try { e.invokeAll(null); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1198,7 +1207,7 @@ public class ThreadPoolExecutorTest exte try { List> r = e.invokeAll(new ArrayList>()); assertTrue(r.isEmpty()); - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1216,7 +1225,7 @@ public class ThreadPoolExecutorTest exte l.add(null); e.invokeAll(l); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1233,10 +1242,10 @@ public class ThreadPoolExecutorTest exte l.add(new NPETask()); List> result = e.invokeAll(l); assertEquals(1, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) + for (Iterator> it = result.iterator(); it.hasNext();) it.next().get(); - } catch(ExecutionException success) { - } catch(Exception ex) { + } catch (ExecutionException success) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1254,10 +1263,10 @@ public class ThreadPoolExecutorTest exte l.add(new StringTask()); List> result = e.invokeAll(l); assertEquals(2, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) + for (Iterator> it = result.iterator(); it.hasNext();) assertSame(TEST_STRING, it.next().get()); } catch (ExecutionException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1274,7 +1283,7 @@ public class ThreadPoolExecutorTest exte try { e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1291,7 +1300,7 @@ public class ThreadPoolExecutorTest exte l.add(new StringTask()); e.invokeAny(l, MEDIUM_DELAY_MS, null); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1306,7 +1315,7 @@ public class ThreadPoolExecutorTest exte try { e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } catch (IllegalArgumentException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1324,7 +1333,7 @@ public class ThreadPoolExecutorTest exte l.add(null); e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { ex.printStackTrace(); unexpectedException(); } finally { @@ -1341,8 +1350,8 @@ public class ThreadPoolExecutorTest exte ArrayList> l = new ArrayList>(); l.add(new NPETask()); e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); - } catch(ExecutionException success) { - } catch(Exception ex) { + } catch (ExecutionException success) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1361,7 +1370,7 @@ public class ThreadPoolExecutorTest exte String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); assertSame(TEST_STRING, result); } catch (ExecutionException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1376,7 +1385,7 @@ public class ThreadPoolExecutorTest exte try { e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1393,7 +1402,7 @@ public class ThreadPoolExecutorTest exte l.add(new StringTask()); e.invokeAll(l, MEDIUM_DELAY_MS, null); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1408,7 +1417,7 @@ public class ThreadPoolExecutorTest exte try { List> r = e.invokeAll(new ArrayList>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); assertTrue(r.isEmpty()); - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1426,7 +1435,7 @@ public class ThreadPoolExecutorTest exte l.add(null); e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); } catch (NullPointerException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1443,10 +1452,10 @@ public class ThreadPoolExecutorTest exte l.add(new NPETask()); List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); assertEquals(1, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) + for (Iterator> it = result.iterator(); it.hasNext();) it.next().get(); - } catch(ExecutionException success) { - } catch(Exception ex) { + } catch (ExecutionException success) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1464,10 +1473,10 @@ public class ThreadPoolExecutorTest exte l.add(new StringTask()); List> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); assertEquals(2, result.size()); - for (Iterator> it = result.iterator(); it.hasNext();) + for (Iterator> it = result.iterator(); it.hasNext();) assertSame(TEST_STRING, it.next().get()); } catch (ExecutionException success) { - } catch(Exception ex) { + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); @@ -1486,7 +1495,7 @@ public class ThreadPoolExecutorTest exte l.add(new StringTask()); List> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); assertEquals(3, result.size()); - Iterator> it = result.iterator(); + Iterator> it = result.iterator(); Future f1 = it.next(); Future f2 = it.next(); Future f3 = it.next(); @@ -1495,12 +1504,108 @@ public class ThreadPoolExecutorTest exte assertTrue(f3.isDone()); assertFalse(f1.isCancelled()); assertTrue(f2.isCancelled()); - } catch(Exception ex) { + } catch (Exception ex) { + unexpectedException(); + } finally { + joinPool(e); + } + } + + /** + * Execution continues if there is at least one thread even if + * thread factory fails to create more + */ + public void testFailingThreadFactory() { + ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new FailingThreadFactory()); + try { + ArrayList> l = new ArrayList>(); + for (int k = 0; k < 100; ++k) { + e.execute(new NoOpRunnable()); + } + Thread.sleep(LONG_DELAY_MS); + } catch (Exception ex) { unexpectedException(); } finally { joinPool(e); } } + /** + * allowsCoreThreadTimeOut is by default false. + */ + public void testAllowsCoreThreadTimeOut() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + assertFalse(tpe.allowsCoreThreadTimeOut()); + joinPool(tpe); + } + + /** + * allowCoreThreadTimeOut(true) causes idle threads to time out + */ + public void testAllowCoreThreadTimeOut_true() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + tpe.allowCoreThreadTimeOut(true); + tpe.execute(new NoOpRunnable()); + try { + Thread.sleep(MEDIUM_DELAY_MS); + assertEquals(0, tpe.getPoolSize()); + } catch (InterruptedException e) { + unexpectedException(); + } finally { + joinPool(tpe); + } + } + + /** + * allowCoreThreadTimeOut(false) causes idle threads not to time out + */ + public void testAllowCoreThreadTimeOut_false() { + ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10)); + tpe.allowCoreThreadTimeOut(false); + tpe.execute(new NoOpRunnable()); + try { + Thread.sleep(MEDIUM_DELAY_MS); + assertTrue(tpe.getPoolSize() >= 1); + } catch (InterruptedException e) { + unexpectedException(); + } finally { + joinPool(tpe); + } + } + + /** + * execute allows the same task to be submitted multiple times, even + * if rejected + */ + public void testRejectedRecycledTask() { + final int nTasks = 1000; + final AtomicInteger nRun = new AtomicInteger(0); + final Runnable recycledTask = new Runnable() { + public void run() { + nRun.getAndIncrement(); + } }; + final ThreadPoolExecutor p = + new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, + new ArrayBlockingQueue(30)); + try { + for (int i = 0; i < nTasks; ++i) { + for (;;) { + try { + p.execute(recycledTask); + break; + } + catch (RejectedExecutionException ignore) { + } + } + } + Thread.sleep(5000); // enough time to run all tasks + assertEquals(nRun.get(), nTasks); + } catch (Exception ex) { + ex.printStackTrace(); + unexpectedException(); + } finally { + p.shutdown(); + } + } }