/** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */ public void testStandardRejectedExecutionHandlers() { final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1)); final AtomicReference<Thread> thread = new AtomicReference<>(); final Runnable r = new Runnable() { public void run() { thread.set(Thread.currentThread()); }}; try { new AbortPolicy().rejectedExecution(r, p); shouldThrow(); } catch (RejectedExecutionException success) {} assertNull(thread.get()); new DiscardPolicy().rejectedExecution(r, p); assertNull(thread.get()); new CallerRunsPolicy().rejectedExecution(r, p); assertSame(Thread.currentThread(), thread.get()); // check that pool was not perturbed by handlers assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); assertEquals(0, p.getTaskCount()); assertTrue(p.getQueue().isEmpty()); }
@Override RejectedExecutionHandler getHandler() { return new ThreadPoolExecutor.DiscardPolicy(); }