/** * executor using DiscardOldestPolicy drops oldest task if saturated. */ public void testSaturatedExecute_DiscardOldestPolicy() { final CountDownLatch done = new CountDownLatch(1); LatchAwaiter r1 = awaiter(done); LatchAwaiter r2 = awaiter(done); LatchAwaiter r3 = awaiter(done); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), new DiscardOldestPolicy()); try (PoolCleaner cleaner = cleaner(p, done)) { assertEquals(LatchAwaiter.NEW, r1.state); assertEquals(LatchAwaiter.NEW, r2.state); assertEquals(LatchAwaiter.NEW, r3.state); p.execute(r1); p.execute(r2); assertTrue(p.getQueue().contains(r2)); p.execute(r3); assertFalse(p.getQueue().contains(r2)); assertTrue(p.getQueue().contains(r3)); } assertEquals(LatchAwaiter.DONE, r1.state); assertEquals(LatchAwaiter.NEW, r2.state); assertEquals(LatchAwaiter.DONE, r3.state); }
/** * execute using DiscardOldestPolicy drops task on shutdown */ public void testDiscardOldestOnShutdown() { final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), new DiscardOldestPolicy()); try { p.shutdown(); } catch (SecurityException ok) { return; } try (PoolCleaner cleaner = cleaner(p)) { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); assertFalse(r.done); } }
@Override RejectedExecutionHandler getHandler() { return new ThreadPoolExecutor.DiscardOldestPolicy(); }