/** * Adds the ready TaskWrapper to the correct execute queue. Using the priority specified in the * task, we pick the correct queue and add it. * <p> * If this is a scheduled or recurring task use {@link #addToScheduleQueue(TaskWrapper)}. * * @param task {@link TaskWrapper} to queue for the scheduler */ protected void addToExecuteQueue(QueueSet queueSet, OneTimeTaskWrapper task) { if (workerPool.isShutdownStarted()) { throw new RejectedExecutionException("Thread pool shutdown"); } queueSet.addExecute(task); }
/** * Adds the ready TaskWrapper to the correct execute queue. Using the priority specified in the * task, we pick the correct queue and add it. * <p> * If this is a scheduled or recurring task use {@link #addToScheduleQueue(TaskWrapper)}. * * @param task {@link TaskWrapper} to queue for the scheduler */ protected void addToExecuteQueue(QueueSet queueSet, OneTimeTaskWrapper task) { if (workerPool.isShutdownStarted()) { throw new RejectedExecutionException("Thread pool shutdown"); } queueSet.addExecute(task); }
@Test public void addExecuteTest() { OneTimeTaskWrapper task = new OneTimeTaskWrapper(DoNothingRunnable.instance(), null, Clock.lastKnownForwardProgressingMillis()); queueSet.addExecute(task); assertEquals(1, queueSet.executeQueue.size()); assertEquals(0, queueSet.scheduleQueue.size()); }
@Test public void getNextReadyTaskExecuteAheadOfScheduledTest() { OneTimeTaskWrapper executeTask = new OneTimeTaskWrapper(DoNothingRunnable.instance(), queueManager.highPriorityQueueSet.executeQueue, Clock.accurateForwardProgressingMillis()); queueManager.highPriorityQueueSet.addExecute(executeTask); TestUtils.blockTillClockAdvances(); TaskWrapper scheduleTask = new OneTimeTaskWrapper(DoNothingRunnable.instance(), queueManager.highPriorityQueueSet.scheduleQueue, Clock.lastKnownForwardProgressingMillis()); queueManager.highPriorityQueueSet.addScheduled(scheduleTask); assertTrue(executeTask == queueManager.getNextTask()); assertTrue(executeTask == queueManager.getNextTask()); // execute task has not been removed yet // this should remove the execute task so we can get the scheduled task assertTrue(executeTask.canExecute(executeTask.getExecuteReference())); assertTrue(scheduleTask == queueManager.getNextTask()); }
/** * When ever pool state is changed that is not checked in the pollTask loop (ie shutdown state, * pool size), a task must be added that is continually added till the desired state is reached. * <p> * The purpose of this task is to break worker threads out of the tight loop for polling tasks, * and instead get them to check the initial logic in {@link #workerIdle(Worker)} again. This * is in contrast to putting the logic in the poll task loop, which only incurs a performance * penalty. */ private void addPoolStateChangeTask(InternalRunnable task) { // add to starvable queue, since we only need these tasks to be consumed and ran when there // is nothing else to run. queueManager.starvablePriorityQueueSet .addExecute(new ImmediateTaskWrapper(task, queueManager.starvablePriorityQueueSet.executeQueue)); }
@Test public void getNextReadyTaskScheduledAheadOfExecuteTest() { TaskWrapper scheduleTask = new OneTimeTaskWrapper(DoNothingRunnable.instance(), queueManager.highPriorityQueueSet.scheduleQueue, Clock.accurateForwardProgressingMillis()); queueManager.highPriorityQueueSet.addScheduled(scheduleTask); TestUtils.blockTillClockAdvances(); OneTimeTaskWrapper executeTask = new OneTimeTaskWrapper(DoNothingRunnable.instance(), queueManager.highPriorityQueueSet.executeQueue, Clock.lastKnownForwardProgressingMillis()); queueManager.highPriorityQueueSet.addExecute(executeTask); assertTrue(scheduleTask == queueManager.getNextTask()); assertTrue(scheduleTask == queueManager.getNextTask()); // schedule task has not been removed yet // this should remove the schedule task so we can get the execute task assertTrue(scheduleTask.canExecute(executeTask.getExecuteReference())); assertTrue(executeTask == queueManager.getNextTask()); }
/** * Stops any new tasks from being submitted to the pool. But allows all tasks which are * submitted to execute, or scheduled (and have elapsed their delay time) to run. If recurring * tasks are present they will also be unable to reschedule. If {@code shutdown()} or * {@link #shutdownNow()} has already been called, this will have no effect. * <p> * If you wish to not want to run any queued tasks you should use {@link #shutdownNow()}. */ public void shutdown() { if (workerPool.startShutdown()) { ShutdownRunnable sr = new ShutdownRunnable(workerPool); taskQueueManager.lowPriorityQueueSet .addExecute(new ImmediateTaskWrapper(sr, taskQueueManager.lowPriorityQueueSet.executeQueue)); } }
/** * Stops any new tasks from being submitted to the pool. But allows all tasks which are * submitted to execute, or scheduled (and have elapsed their delay time) to run. If recurring * tasks are present they will also be unable to reschedule. If {@code shutdown()} or * {@link #shutdownNow()} has already been called, this will have no effect. * <p> * If you wish to not want to run any queued tasks you should use {@link #shutdownNow()}. */ public void shutdown() { if (workerPool.startShutdown()) { ShutdownRunnable sr = new ShutdownRunnable(workerPool); taskQueueManager.lowPriorityQueueSet .addExecute(new ImmediateTaskWrapper(sr, taskQueueManager.lowPriorityQueueSet.executeQueue)); } }
@Override protected OneTimeTaskWrapper doSchedule(Runnable task, long delayInMillis, TaskPriority priority) { QueueSet queueSet = queueManager.getQueueSet(priority); OneTimeTaskWrapper result; if (delayInMillis == 0) { queueSet.addExecute((result = new NoThreadOneTimeTaskWrapper(task, queueSet.executeQueue, nowInMillis(false)))); } else { queueSet.addScheduled((result = new NoThreadOneTimeTaskWrapper(task, queueSet.scheduleQueue, nowInMillis(true) + delayInMillis))); } return result; }
/** * When ever pool state is changed that is not checked in the pollTask loop (ie shutdown state, * pool size), a task must be added that is continually added till the desired state is reached. * <p> * The purpose of this task is to break worker threads out of the tight loop for polling tasks, * and instead get them to check the initial logic in {@link #workerIdle(Worker)} again. This * is in contrast to putting the logic in the poll task loop, which only incurs a performance * penalty. */ private void addPoolStateChangeTask(InternalRunnable task) { // add to starvable queue, since we only need these tasks to be consumed and ran when there // is nothing else to run. queueManager.starvablePriorityQueueSet .addExecute(new ImmediateTaskWrapper(task, queueManager.starvablePriorityQueueSet.executeQueue)); }
@Test public void getNextReadyTaskHighPriorityDelayedTest() { TaskWrapper scheduleTask = new OneTimeTaskWrapper(DoNothingRunnable.instance(), queueManager.highPriorityQueueSet.scheduleQueue, Clock.accurateForwardProgressingMillis() + 1000); queueManager.highPriorityQueueSet.addScheduled(scheduleTask); TestUtils.blockTillClockAdvances(); OneTimeTaskWrapper executeTask = new OneTimeTaskWrapper(DoNothingRunnable.instance(), queueManager.lowPriorityQueueSet.executeQueue, Clock.lastKnownForwardProgressingMillis()); queueManager.lowPriorityQueueSet.addExecute(executeTask); assertTrue(executeTask == queueManager.getNextTask()); }
@Override protected OneTimeTaskWrapper doSchedule(Runnable task, long delayInMillis, TaskPriority priority) { QueueSet queueSet = queueManager.getQueueSet(priority); OneTimeTaskWrapper result; if (delayInMillis == 0) { queueSet.addExecute((result = new NoThreadOneTimeTaskWrapper(task, queueSet.executeQueue, nowInMillis(false)))); } else { queueSet.addScheduled((result = new NoThreadOneTimeTaskWrapper(task, queueSet.scheduleQueue, nowInMillis(true) + delayInMillis))); } return result; }
private void getNextReadyTaskExecuteTest(QueueSet queueSet) { OneTimeTaskWrapper task = new OneTimeTaskWrapper(DoNothingRunnable.instance(), queueSet.executeQueue, Clock.lastKnownForwardProgressingMillis()); queueSet.addExecute(task); assertTrue(task == queueManager.getNextTask()); }