@Test public void testRunsOldestTaskPolicy() throws Exception { AtomicInteger atomicInteger = new AtomicInteger(); ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_CORE_POOL_SIZE, DEFAULT_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(MAX_QUEUE_SIZE), new NamedThreadFactory("OldestRunsPolicy", DEFAULT_CORE_POOL_SIZE), RejectedPolicies.runsOldestTaskPolicy()); CountDownLatch downLatch1 = new CountDownLatch(1); CountDownLatch downLatch2 = new CountDownLatch(1); CountDownLatch downLatch3 = new CountDownLatch(1); poolExecutor.execute(new Runnable() { @Override public void run() { try { downLatch1.await(); } catch (InterruptedException e) { e.printStackTrace(); } atomicInteger.getAndAdd(1); } }); assertThat(atomicInteger.get()).isEqualTo(0); poolExecutor.execute(new Runnable() { @Override public void run() { atomicInteger.getAndAdd(2); } }); poolExecutor.execute(new Runnable() { @Override public void run() { downLatch2.countDown(); atomicInteger.getAndAdd(3); downLatch3.countDown(); } }); assertThat(atomicInteger.get()).isEqualTo(2); downLatch1.countDown(); downLatch2.await(); downLatch3.await(); assertThat(atomicInteger.get()).isEqualTo(6); }
public static RejectedExecutionHandler runsOldestTaskPolicy() { return new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } BlockingQueue<Runnable> workQueue = executor.getQueue(); Runnable firstWork = workQueue.poll(); boolean newTaskAdd = workQueue.offer(r); if (firstWork != null) { firstWork.run(); } if (!newTaskAdd) { executor.execute(r); } } }; }
RejectedPolicies { public static RejectedExecutionHandler runsOldestTaskPolicy() { return new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } BlockingQueue<Runnable> workQueue = executor.getQueue(); Runnable firstWork = workQueue.poll(); boolean newTaskAdd = workQueue.offer(r); if (firstWork != null) { firstWork.run(); } if (!newTaskAdd) { executor.execute(r); } } }; } }
RejectedPolicies { public static RejectedExecutionHandler runsOldestTaskPolicy() { return new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } BlockingQueue<Runnable> workQueue = executor.getQueue(); Runnable firstWork = workQueue.poll(); boolean newTaskAdd = workQueue.offer(r); if (firstWork != null) { firstWork.run(); } if (!newTaskAdd) { executor.execute(r); } } }; }  }
RejectedPolicies { public static RejectedExecutionHandler runsOldestTaskPolicy() { return new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } BlockingQueue<Runnable> workQueue = executor.getQueue(); Runnable firstWork = workQueue.poll(); boolean newTaskAdd = workQueue.offer(r); if (firstWork != null) { firstWork.run(); } if (!newTaskAdd) { executor.execute(r); } } }; }  static RejectedExecutionHandler runsOldestTaskPolicy(); }
RejectedPolicies { public static RejectedExecutionHandler runsOldestTaskPolicy() { return new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { if (executor.isShutdown()) { return; } BlockingQueue<Runnable> workQueue = executor.getQueue(); Runnable firstWork = workQueue.poll(); boolean newTaskAdd = workQueue.offer(r); if (firstWork != null) { firstWork.run(); } if (!newTaskAdd) { executor.execute(r); } } }; }  static RejectedExecutionHandler runsOldestTaskPolicy();  }