public class NoThreadScheduler extends AbstractPriorityScheduler
The tasks in this scheduler are only progressed forward with calls to
tick(ExceptionHandler)
. Since it is running on the calling thread, calls to
Object.wait()
and Thread.sleep()
from sub tasks will block (possibly forever).
The call to tick(ExceptionHandler)
will not unblock till there is no more work for the
scheduler to currently handle.
Constructor and Description |
---|
NoThreadScheduler()
Constructs a new
NoThreadScheduler scheduler. |
NoThreadScheduler(TaskPriority defaultPriority,
long maxWaitForLowPriorityInMs)
Constructs a new
NoThreadScheduler scheduler with specified default priority behavior. |
Modifier and Type | Method and Description |
---|---|
int |
blockingTick(ExceptionHandler exceptionHandler)
This is similar to
tick(ExceptionHandler) , except that it will block until there are
tasks ready to run, or until cancelTick() is invoked. |
void |
cancelTick()
Call to cancel current or the next tick call.
|
java.util.List<java.lang.Runnable> |
clearTasks()
Removes any tasks waiting to be run.
|
int |
getActiveTaskCount()
Call to check how many tasks are currently being executed in this scheduler.
|
long |
getDelayTillNextTask()
Checks how long till the next task will be ready to execute.
|
boolean |
hasTaskReadyToRun()
Checks if there are tasks ready to be run on the scheduler.
|
boolean |
isShutdown()
Function to check if the thread pool is currently accepting and handling tasks.
|
void |
scheduleAtFixedRate(java.lang.Runnable task,
long initialDelay,
long period,
TaskPriority priority)
Schedule a fixed rate recurring task to run.
|
void |
scheduleWithFixedDelay(java.lang.Runnable task,
long initialDelay,
long recurringDelay,
TaskPriority priority)
Schedule a fixed delay recurring task to run.
|
int |
tick(ExceptionHandler exceptionHandler)
Invoking this will run any tasks which are ready to be run.
|
execute, getDefaultPriority, getMaxWaitForLowPriority, getQueuedTaskCount, getQueuedTaskCount, getWaitingForExecutionTaskCount, getWaitingForExecutionTaskCount, remove, remove, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, setMaxWaitForLowPriority, submit, submit, submitScheduled, submitScheduled
schedule, submitScheduled, submitScheduled
execute, submit, submit
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
submit, submitScheduled
schedule, submitScheduled, submitScheduled, submitScheduled
submit, submit, submit
public NoThreadScheduler()
NoThreadScheduler
scheduler.public NoThreadScheduler(TaskPriority defaultPriority, long maxWaitForLowPriorityInMs)
NoThreadScheduler
scheduler with specified default priority behavior.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 runpublic void cancelTick()
tick(ExceptionHandler)
call (weather blocking waiting for tasks, or currently running
tasks), this will call the tick(ExceptionHandler)
to return. If a task is currently
running it will finish the current task before returning. If not currently in a
tick(ExceptionHandler)
call, the next tick call will return immediately without
running anything.public int tick(ExceptionHandler exceptionHandler)
tick(ExceptionHandler)
OR blockingTick(ExceptionHandler)
. While
this class is in general thread safe, if multiple threads invoke either function at the same
time, it is possible a given task may run more than once. In order to maintain high
performance, threadly does not guard against this condition.
This call allows you to specify an ExceptionHandler
. If provided, if any tasks throw
an exception, this will be called to communicate the exception. This allows you to ensure
that you get a returned task count (meaning if provided, no exceptions will be thrown from
this invocation). If null
is provided for the exception handler, than any tasks
which throw a RuntimeException
, will throw out of this invocation.
This call is NOT thread safe, calling tick(ExceptionHandler)
or
blockingTick(ExceptionHandler)
in parallel could cause the same task to be
run multiple times in parallel. Invoking in parallel will also make the behavior of
getActiveTaskCount()
non-deterministic and inaccurate.
exceptionHandler
- Exception handler implementation to call if any tasks throw an
exception, or null to have exceptions thrown out of this callpublic int blockingTick(ExceptionHandler exceptionHandler) throws java.lang.InterruptedException
tick(ExceptionHandler)
, except that it will block until there are
tasks ready to run, or until cancelTick()
is invoked.
Once there are tasks ready to run, this will continue to block as it runs as many tasks that are ready to run.
It is CRITICAL that only one thread at a time calls the tick(ExceptionHandler)
OR
blockingTick(ExceptionHandler)
.
This call allows you to specify an ExceptionHandler
. If provided, if any tasks throw
an exception, this will be called to communicate the exception. This allows you to ensure
that you get a returned task count (meaning if provided, no exceptions will be thrown from
this invocation). If null
is provided for the exception handler, than any tasks
which throw a RuntimeException
, will throw out of this invocation.
This call is NOT thread safe, calling tick(ExceptionHandler)
or
blockingTick(ExceptionHandler)
in parallel could cause the same task to be
run multiple times in parallel. Invoking in parallel will also make the behavior of
getActiveTaskCount()
non-deterministic and inaccurate.
exceptionHandler
- Exception handler implementation to call if any tasks throw an
exception, or null to have exceptions thrown out of this calljava.lang.InterruptedException
- thrown if thread is interrupted waiting for task to runpublic void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay, TaskPriority priority)
PrioritySchedulerService
recurringDelay + runtime
for the provided task.
Unlike ScheduledExecutorService
if 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).
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 onpublic void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period, TaskPriority priority)
PrioritySchedulerService
Unlike ScheduledExecutorService
if 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).
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 onpublic int getActiveTaskCount()
SchedulerService
public boolean isShutdown()
SchedulerService
true
if thread pool is runningpublic boolean hasTaskReadyToRun()
tick(ExceptionHandler)
(but does not have to be).
If tick(ExceptionHandler)
is not currently being called, this call indicates if the
next tick(ExceptionHandler)
will have at least one task to run. If
tick(ExceptionHandler)
is currently being invoked, this call will do a best attempt
to indicate if there is at least one more task to run (not including the task which may
currently be running). It's a best attempt as it will try not to block the thread invoking
tick(ExceptionHandler)
to prevent it from accepting additional work.true
if there are task waiting to runpublic long getDelayTillNextTask()
Long.MAX_VALUE
will be returned. If there is a task ready
to execute this will return a value less than or equal to zero. If the task is past its
desired point of execution the result will be a negative amount of milliseconds past that
point in time.
Generally this is called from the same thread that would invoke
tick(ExceptionHandler)
(but does not have to be). Since this does not block or lock
if being invoked in parallel with tick(ExceptionHandler)
, the results may be no
longer accurate by the time this invocation has returned.
If a recurring task is currently running this will return a very large number. This will
remain until the task has rescheduled itself. To avoid this just ensure this is never
invoked in parallel with tick(ExceptionHandler)
.
This can be useful if you want to know how long you can block on something, ASSUMING you can detect that something has been added to the scheduler, and interrupt that blocking in order to handle tasks.
public java.util.List<java.lang.Runnable> clearTasks()
tick(ExceptionHandler)
is being called. But will avoid additional tasks from being
run on the current tick(ExceptionHandler)
call.
If tasks are added concurrently during this invocation they may or may not be removed.