Package org.threadly.concurrent
Class SingleThreadScheduler
- java.lang.Object
-
- org.threadly.concurrent.AbstractSubmitterExecutor
-
- org.threadly.concurrent.AbstractSubmitterScheduler
-
- org.threadly.concurrent.AbstractPriorityScheduler
-
- org.threadly.concurrent.SingleThreadScheduler
-
- All Implemented Interfaces:
java.util.concurrent.Executor,PrioritySchedulerService,SchedulerService,SubmitterExecutor,SubmitterScheduler
- Direct Known Subclasses:
SingleThreadSchedulerStatisticTracker
public class SingleThreadScheduler extends AbstractPriorityScheduler
A simple and light weight implementation of theSchedulerService. This is designed to be a lighter weight option to thePriorityScheduler, for when multiple threads are either not needed, or not desired.- Since:
- 2.0.0
-
-
Constructor Summary
Constructors Constructor Description SingleThreadScheduler()Constructs a newSingleThreadScheduler.SingleThreadScheduler(boolean daemonThread)Constructs a newSingleThreadScheduler.SingleThreadScheduler(java.util.concurrent.ThreadFactory threadFactory)Constructs a newSingleThreadScheduler.SingleThreadScheduler(TaskPriority defaultPriority, long maxWaitForLowPriorityInMs)Constructs a newSingleThreadScheduler.SingleThreadScheduler(TaskPriority defaultPriority, long maxWaitForLowPriorityInMs, boolean daemonThread)Constructs a newSingleThreadScheduler.SingleThreadScheduler(TaskPriority defaultPriority, long maxWaitForLowPriorityInMs, java.util.concurrent.ThreadFactory threadFactory)Constructs a newSingleThreadScheduler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidawaitTermination()Block until the thread pool has shutdown and all threads have been stopped.booleanawaitTermination(long timeoutMillis)Block until the thread pool has shutdown and all threads have been stopped.intgetActiveTaskCount()Call to check how many tasks are currently being executed in this scheduler.booleanisShutdown()Function to check if the thread pool is currently accepting and handling tasks.booleanisTerminated()One step beyondisShutdown(), atruehere indicates that not only has a shutdown on the pool been requested, but that the thread running tasks has completed.voidprestartExecutionThread()Start thread for execution if not already started.voidprestartExecutionThread(boolean blockTillStarted)Start thread for execution if not already started.voidscheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period, TaskPriority priority)Schedule a fixed rate recurring task to run.voidscheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay, TaskPriority priority)Schedule a fixed delay recurring task to run.voidshutdown()Stops any new tasks from being submitted to the pool.java.util.List<java.lang.Runnable>shutdownNow()Stops any new tasks from being submitted to the pool.-
Methods inherited from class org.threadly.concurrent.AbstractPriorityScheduler
execute, getDefaultPriority, getMaxWaitForLowPriority, getQueuedTaskCount, getQueuedTaskCount, getWaitingForExecutionTaskCount, getWaitingForExecutionTaskCount, remove, remove, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, setMaxWaitForLowPriority, submit, submit, submitScheduled, submitScheduled
-
Methods inherited from class org.threadly.concurrent.AbstractSubmitterScheduler
schedule, submitScheduled, submitScheduled
-
Methods inherited from class org.threadly.concurrent.AbstractSubmitterExecutor
execute, submit, submit
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.threadly.concurrent.PrioritySchedulerService
submit, submitScheduled
-
Methods inherited from interface org.threadly.concurrent.SubmitterExecutor
submit, submit, submit
-
Methods inherited from interface org.threadly.concurrent.SubmitterScheduler
schedule, submitScheduled, submitScheduled, submitScheduled
-
-
-
-
Constructor Detail
-
SingleThreadScheduler
public SingleThreadScheduler()
Constructs a newSingleThreadScheduler. No threads will start until the first task is provided. This defaults to using a daemon thread for the scheduler.
-
SingleThreadScheduler
public SingleThreadScheduler(TaskPriority defaultPriority, long maxWaitForLowPriorityInMs)
Constructs a newSingleThreadScheduler. No threads will start until the first task is provided. This defaults to using a daemon thread for the scheduler.- Parameters:
defaultPriority- Default priority for tasks which are submitted without any specified prioritymaxWaitForLowPriorityInMs- time low priority tasks to wait if there are high priority tasks ready to run
-
SingleThreadScheduler
public SingleThreadScheduler(boolean daemonThread)
Constructs a newSingleThreadScheduler. No threads will start until the first task is provided.- Parameters:
daemonThread-trueif scheduler thread should be a daemon thread
-
SingleThreadScheduler
public SingleThreadScheduler(TaskPriority defaultPriority, long maxWaitForLowPriorityInMs, boolean daemonThread)
Constructs a newSingleThreadScheduler. No threads will start until the first task is provided.- Parameters:
defaultPriority- Default priority for tasks which are submitted without any specified prioritymaxWaitForLowPriorityInMs- time low priority tasks to wait if there are high priority tasks ready to rundaemonThread-trueif scheduler thread should be a daemon thread
-
SingleThreadScheduler
public SingleThreadScheduler(java.util.concurrent.ThreadFactory threadFactory)
Constructs a newSingleThreadScheduler. No threads will start until the first task is provided.- Parameters:
threadFactory- factory to make thread for scheduler
-
SingleThreadScheduler
public SingleThreadScheduler(TaskPriority defaultPriority, long maxWaitForLowPriorityInMs, java.util.concurrent.ThreadFactory threadFactory)
Constructs a newSingleThreadScheduler. No threads will start until the first task is provided.- Parameters:
defaultPriority- Default priority for tasks which are submitted without any specified prioritymaxWaitForLowPriorityInMs- time low priority tasks to wait if there are high priority tasks ready to runthreadFactory- factory to make thread for scheduler
-
-
Method Detail
-
shutdown
public void shutdown()
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. This call will not block to wait for the shutdown of the scheduler to finish. Ifshutdown()orshutdownNow()has already been called, this will have no effect.If you wish to not want to run any queued tasks you should use
shutdownNow().
-
shutdownNow
public java.util.List<java.lang.Runnable> shutdownNow()
Stops any new tasks from being submitted to the pool. If any tasks are waiting for execution they will be prevented from being run. If a task is currently running it will be allowed to finish (though this call will not block waiting for it to finish).- Returns:
- returns a list of runnables which were waiting in the queue to be run at time of shutdown
-
awaitTermination
public void awaitTermination() throws java.lang.InterruptedExceptionBlock until the thread pool has shutdown and all threads have been stopped. If neithershutdown()orshutdownNow()is invoked, then this will block forever.- Throws:
java.lang.InterruptedException- Thrown if blocking thread is interrupted waiting for shutdown
-
awaitTermination
public boolean awaitTermination(long timeoutMillis) throws java.lang.InterruptedExceptionBlock until the thread pool has shutdown and all threads have been stopped. If neithershutdown()orshutdownNow()is invoked, then this will block until the timeout is reached.- Parameters:
timeoutMillis- time to block and wait for thread pool to shutdown- Returns:
trueif the pool has shutdown, false if timeout was reached- Throws:
java.lang.InterruptedException- Thrown if blocking thread is interrupted waiting for shutdown
-
isShutdown
public boolean isShutdown()
Description copied from interface:SchedulerServiceFunction to check if the thread pool is currently accepting and handling tasks.- Returns:
trueif thread pool is running
-
isTerminated
public boolean isTerminated()
One step beyondisShutdown(), atruehere indicates that not only has a shutdown on the pool been requested, but that the thread running tasks has completed.- Returns:
trueif this scheduler is no longer processing any tasks
-
prestartExecutionThread
public void prestartExecutionThread()
Start thread for execution if not already started. This can avoid the minor delay of the thread starting later. This will not block till the thread is running, instead letting it start in the background (usually fine).
-
prestartExecutionThread
public void prestartExecutionThread(boolean blockTillStarted)
Start thread for execution if not already started. This can avoid the minor delay of the thread starting later.- Parameters:
blockTillStarted- Iftruethis will not return till the scheduler thread has started
-
scheduleWithFixedDelay
public void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay, TaskPriority priority)Description copied from interface:PrioritySchedulerServiceSchedule a fixed delay recurring task to run. The recurring delay time will be from the point where execution has finished. So the execution frequency is therecurringDelay + runtimefor the provided task.Unlike
ScheduledExecutorServiceif the task throws an exception, subsequent executions are NOT suppressed or prevented. So if the task throws an exception on every run, the task will continue to be executed at the provided recurring delay (possibly throwing an exception on each execution).- Parameters:
task- runnable to be executedinitialDelay- delay in milliseconds until first runrecurringDelay- delay in milliseconds for running task after last finishpriority- priority for task to get available thread to run on
-
scheduleAtFixedRate
public void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period, TaskPriority priority)Description copied from interface:PrioritySchedulerServiceSchedule a fixed rate recurring task to run. The recurring delay will be the same, regardless of how long task execution takes. A given runnable will not run concurrently (unless it is submitted to the scheduler multiple times). Instead of execution takes longer than the period, the next run will occur immediately (given thread availability in the pool).Unlike
ScheduledExecutorServiceif the task throws an exception, subsequent executions are NOT suppressed or prevented. So if the task throws an exception on every run, the task will continue to be executed at the provided recurring delay (possibly throwing an exception on each execution).- Parameters:
task- runnable to be executedinitialDelay- delay in milliseconds until first runperiod- amount of time in milliseconds between the start of recurring executionspriority- priority for task to get available thread to run on
-
getActiveTaskCount
public int getActiveTaskCount()
Description copied from interface:SchedulerServiceCall to check how many tasks are currently being executed in this scheduler.- Returns:
- current number of running tasks
-
-