/** * Block until the thread pool has shutdown and all threads have been stopped. If neither * {@link #shutdown()} or {@link #shutdownNow()} is invoked, then this will block forever. * * @throws InterruptedException Thrown if blocking thread is interrupted waiting for shutdown */ public void awaitTermination() throws InterruptedException { awaitTermination(Long.MAX_VALUE); }
@Override public boolean isTerminated() { return scheduler.isShutdown() && pScheduler.getCurrentPoolSize() == 0; }
@Override public boolean isShutdown() { return MASTER_SCHEDULER.isShutdown(); }
/** * This call will stop the processor as quick as possible. Any tasks which are awaiting * execution will be canceled and returned as a result to this call. * <p> * Unlike {@link java.util.concurrent.ExecutorService} implementation there is no attempt to * stop any currently execution tasks. * <p> * This method does not wait for actively executing tasks toterminate. Use * {@link #awaitTermination awaitTermination} to do that. * * @return list of tasks that never commenced execution */ @Override public List<Runnable> shutdownNow() { return pScheduler.shutdownNow(); }
@Test public void awaitTerminationTest() throws InterruptedException { PrioritySchedulerServiceFactory factory = getPrioritySchedulerFactory(); try { PriorityScheduler scheduler = factory.makePriorityScheduler(1); TestRunnable tr = new TestRunnable(DELAY_TIME * 2); long start = Clock.accurateForwardProgressingMillis(); scheduler.execute(tr); tr.blockTillStarted(); scheduler.shutdown(); scheduler.awaitTermination(); long stop = Clock.accurateForwardProgressingMillis(); assertTrue(stop - start >= (DELAY_TIME * 2) - 10); } finally { factory.shutdown(); } }
@Test public void isShutdownTest() { PrioritySchedulerServiceFactory factory = getPrioritySchedulerFactory(); try { PriorityScheduler scheduler = factory.makePriorityScheduler(1); assertFalse(scheduler.isShutdown()); scheduler.shutdown(); assertTrue(scheduler.isShutdown()); scheduler = factory.makePriorityScheduler(1); scheduler.shutdownNow(); assertTrue(scheduler.isShutdown()); } finally { factory.shutdown(); } }
@Test public void shutdownRecurringTest() { PrioritySchedulerServiceFactory factory = getPrioritySchedulerFactory(); try { final PriorityScheduler scheduler = factory.makePriorityScheduler(1); TestRunnable tr = new TestRunnable(); scheduler.scheduleWithFixedDelay(tr, 0, 0); tr.blockTillStarted(); scheduler.shutdown(); new TestCondition(() -> scheduler.workerPool.isShutdownFinished() && scheduler.getCurrentPoolSize() == 0).blockTillTrue(); } finally { factory.shutdown(); } }
/** * Used for gaining compatibility with java.util.concurrent when a {@link Delayed} object is * needed. * * @param pScheduler Scheduler to submit task to * @param task Task to be submitted * @param priority Priority for task to be submitted at * @param initialDelay initial delay for task to execute * @param periodInMillis recurring delay for task to execute * @return Delayed implementation which corresponds to executed task */ public static Delayed doScheduleAtFixedRateAndGetDelayed(PriorityScheduler pScheduler, Runnable task, TaskPriority priority, long initialDelay, long periodInMillis) { QueueSet queueSet = pScheduler.taskQueueManager.getQueueSet(priority); RecurringRateTaskWrapper rrtw = new RecurringRateTaskWrapper(task, queueSet, Clock.accurateForwardProgressingMillis() + initialDelay, periodInMillis); pScheduler.addToScheduleQueue(queueSet, rrtw); return new DelayedTaskWrapper(rrtw); }
PriorityScheduler scheduler = factory.makePriorityScheduler(1); scheduler.shutdownNow(); scheduler.execute(DoNothingRunnable.instance()); fail("Execption should have been thrown"); } catch (RejectedExecutionException e) { scheduler.schedule(DoNothingRunnable.instance(), 1000, null); fail("Execption should have been thrown"); } catch (RejectedExecutionException e) { scheduler.scheduleWithFixedDelay(DoNothingRunnable.instance(), 100, 100); fail("Execption should have been thrown"); } catch (RejectedExecutionException e) {
PriorityScheduler scheduler = factory.makePriorityScheduler(1); scheduler.shutdown(); scheduler.execute(DoNothingRunnable.instance()); fail("Execption should have been thrown"); } catch (RejectedExecutionException e) { scheduler.schedule(DoNothingRunnable.instance(), 1000, null); fail("Execption should have been thrown"); } catch (RejectedExecutionException e) { scheduler.scheduleWithFixedDelay(DoNothingRunnable.instance(), 100, 100); fail("Execption should have been thrown"); } catch (RejectedExecutionException e) {
@Override protected void finalize() { // shutdown the thread pool so we don't leak threads if garbage collected shutdown(); }
@Override protected OneTimeTaskWrapper doSchedule(Runnable task, long delayInMillis, TaskPriority priority) { QueueSet queueSet = taskQueueManager.getQueueSet(priority); OneTimeTaskWrapper result; if (delayInMillis == 0) { addToExecuteQueue(queueSet, (result = new ImmediateTaskWrapper(task, queueSet.executeQueue))); } else { addToScheduleQueue(queueSet, (result = new OneTimeTaskWrapper(task, queueSet.scheduleQueue, Clock.accurateForwardProgressingMillis() + delayInMillis))); } return result; }
@Test public void getWithTimeoutResultTest() throws InterruptedException, ExecutionException, TimeoutException { final String testResult = StringUtils.makeRandomString(5); PriorityScheduler scheduler = new StrictPriorityScheduler(1); try { scheduler.prestartAllThreads(); scheduler.schedule(new Runnable() { @Override public void run() { slf.setResult(testResult); } }, DELAY_TIME); assertTrue(slf.get(DELAY_TIME + (SLOW_MACHINE ? 2000 : 1000), TimeUnit.MILLISECONDS) == testResult); } finally { scheduler.shutdownNow(); } }
@Test public void executeLimitTest() throws InterruptedException, TimeoutException { PriorityScheduler ps = new PriorityScheduler(16); ps.prestartAllThreads(); try { Executor limitedExecutor = new SingleThreadSchedulerSubPool(ps); ps.shutdownNow();
@Override public void scheduleWithFixedDelay(Runnable task, long initialDelay, long recurringDelay, TaskPriority priority) { super.scheduleWithFixedDelay(wrap(task, priority), initialDelay, recurringDelay, priority); }
scheduler.execute(btr); btr.blockTillStarted(); TestRunnable tr = new TestRunnable(); expectedRunnables.add(tr); scheduler.execute(tr, i % 2 == 0 ? TaskPriority.High : TaskPriority.Low); List<Runnable> canceledRunnables = scheduler.shutdownNow();
ListenableFuture<?> future = scheduler.submit(tr); assertEquals(1, scheduler.getCurrentPoolSize()); scheduler.execute(new Runnable() { @Override public void run() {
@Test public void shutdownNowIgnoreCanceledFuturesTest() { PrioritySchedulerServiceFactory factory = getPrioritySchedulerFactory(); try { PriorityScheduler scheduler = factory.makePriorityScheduler(1); Runnable nonCanceledRunnable = new TestRunnable(); scheduler.submitScheduled(nonCanceledRunnable, 1000 * 60 * 60); ListenableFuture<?> future = scheduler.submitScheduled(DoNothingRunnable.instance(), 1000 * 60 * 60); future.cancel(false); List<Runnable> result = scheduler.shutdownNow(); assertEquals(1, result.size()); // only canceled task removed } finally { factory.shutdown(); } }
@Test public void getResultTest() throws InterruptedException, ExecutionException { final String testResult = StringUtils.makeRandomString(5); PriorityScheduler scheduler = new StrictPriorityScheduler(1); try { scheduler.schedule(new Runnable() { @Override public void run() { slf.setResult(testResult); } }, DELAY_TIME); assertTrue(slf.get() == testResult); } finally { scheduler.shutdownNow(); } }
@Test public void getCurrentPoolSizeTest() { PrioritySchedulerServiceFactory factory = getPrioritySchedulerFactory(); PriorityScheduler scheduler = factory.makePriorityScheduler(1); try { // verify nothing at the start assertEquals(0, scheduler.getCurrentPoolSize()); TestRunnable tr = new TestRunnable(); scheduler.execute(tr); tr.blockTillStarted(); // wait for execution assertEquals(1, scheduler.getCurrentPoolSize()); } finally { factory.shutdown(); } }