public class TestableScheduler extends AbstractPriorityScheduler
NoThreadScheduler
in that time is ONLY
advanced via the tick calls. That means that if you schedule a task, it will be scheduled off
of either the creation time, or the last tick time, what ever the most recent point is. This
allows you to progress time forward faster than it could in real time, having tasks execute
faster, etc, etc.
The tasks in this scheduler are only progressed forward with calls to tick()
. 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()
will not unblock
till there is no more work for the scheduler to currently handle.
Constructor and Description |
---|
TestableScheduler()
Constructs a new
TestableScheduler scheduler. |
TestableScheduler(TaskPriority defaultPriority,
long maxWaitForLowPriorityInMs)
Constructs a new
TestableScheduler scheduler. |
Modifier and Type | Method and Description |
---|---|
int |
advance(long timeInMillis)
This is to provide a convince when advancing the scheduler forward an explicit amount of time.
|
int |
advance(long timeInMillis,
ExceptionHandler exceptionHandler)
This is to provide a convince when advancing the scheduler forward an explicit amount of time.
|
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.
|
long |
getLastTickTime()
Returns the last provided time to the tick call.
|
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()
Progresses tasks for the current time.
|
int |
tick(ExceptionHandler exceptionHandler)
Progresses tasks for the current time.
|
int |
tick(long currentTime)
This progresses tasks based off the time provided.
|
int |
tick(long currentTime,
ExceptionHandler exceptionHandler)
This progresses tasks based off the time provided.
|
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 TestableScheduler()
TestableScheduler
scheduler.public TestableScheduler(TaskPriority defaultPriority, long maxWaitForLowPriorityInMs)
TestableScheduler
scheduler.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 boolean isShutdown()
SchedulerService
true
if thread pool is runningpublic int getActiveTaskCount()
SchedulerService
public 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 long getLastTickTime()
public int advance(long timeInMillis)
timeInMillis
- amount in milliseconds to advance the scheduler forwardpublic int advance(long timeInMillis, ExceptionHandler exceptionHandler)
This call allows you to specify an ExceptionHandler
. If provided, if any tasks throw
an exception, this will be called to inform them of the exception. This allows you to ensure
that you get a returned task count (meaning if provided, no exceptions except a possible
InterruptedException
can be thrown). If null
is provided for the exception
handler, than any tasks which throw a RuntimeException
, will throw out of this
invocation.
timeInMillis
- amount in milliseconds to advance the scheduler forwardexceptionHandler
- Exception handler implementation to call if any tasks throw an
exception, or null to have exceptions thrown out of this callpublic int tick()
If any tasks throw a RuntimeException
, they will be bubbled up to this tick call.
Any tasks past that task will not run till the next call to tick. So it is important that
the implementor handle those exceptions.
public int tick(ExceptionHandler exceptionHandler)
This call allows you to specify an ExceptionHandler
. If provided, if any tasks throw
an exception, this will be called to inform them of the exception. This allows you to ensure
that you get a returned task count (meaning if provided, no exceptions except a possible
InterruptedException
can be thrown). If null
is provided for the exception
handler, than any tasks which throw a RuntimeException
, will throw out of this
invocation.
exceptionHandler
- Exception handler implementation to call if any tasks throw an
exception, or null to have exceptions thrown out of this callpublic int tick(long currentTime)
If any tasks throw a RuntimeException
, they will be bubbled up to this tick call.
Any tasks past that task will not run till the next call to tick. So it is important that
the implementor handle those exceptions.
This call accepts the absolute time in milliseconds. If you want to advance the scheduler a specific amount of time forward, look at the "advance" call.
currentTime
- Absolute time to provide for looking at task run timepublic int tick(long currentTime, ExceptionHandler exceptionHandler)
This call allows you to specify an ExceptionHandler
. If provided, if any tasks throw
an exception, this will be called to inform them of the exception. This allows you to ensure
that you get a returned task count (meaning if provided, no exceptions except a possible
InterruptedException
can be thrown). If null
is provided for the exception
handler, than any tasks which throw a RuntimeException
, will throw out of this
invocation.
This call accepts the absolute time in milliseconds. If you want to advance the scheduler a specific amount of time forward, look at the "advance" call.
currentTime
- Absolute time to provide for looking at task run timeexceptionHandler
- Exception handler implementation to call if any tasks throw an
exception, or null to have exceptions thrown out of this callpublic boolean hasTaskReadyToRun()
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.
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.