public class TestableScheduler
extends java.lang.Object
implements org.threadly.concurrent.PrioritySchedulerService
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(org.threadly.concurrent.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,
org.threadly.util.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.
|
void |
execute(java.lang.Runnable task) |
void |
execute(java.lang.Runnable task,
org.threadly.concurrent.TaskPriority priority) |
int |
getActiveTaskCount() |
org.threadly.concurrent.TaskPriority |
getDefaultPriority() |
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.
|
long |
getMaxWaitForLowPriority() |
int |
getQueuedTaskCount() |
int |
getQueuedTaskCount(org.threadly.concurrent.TaskPriority priority) |
int |
getWaitingForExecutionTaskCount() |
int |
getWaitingForExecutionTaskCount(org.threadly.concurrent.TaskPriority priority) |
boolean |
hasTaskReadyToRun()
Checks if there are tasks ready to be run on the scheduler.
|
boolean |
isShutdown() |
boolean |
remove(java.util.concurrent.Callable<?> task) |
boolean |
remove(java.lang.Runnable task) |
void |
schedule(java.lang.Runnable task,
long delayInMs) |
void |
schedule(java.lang.Runnable task,
long delayInMs,
org.threadly.concurrent.TaskPriority priority) |
void |
scheduleAtFixedRate(java.lang.Runnable task,
long initialDelay,
long period) |
void |
scheduleAtFixedRate(java.lang.Runnable task,
long initialDelay,
long period,
org.threadly.concurrent.TaskPriority priority) |
void |
scheduleWithFixedDelay(java.lang.Runnable task,
long initialDelay,
long recurringDelay) |
void |
scheduleWithFixedDelay(java.lang.Runnable task,
long initialDelay,
long recurringDelay,
org.threadly.concurrent.TaskPriority priority) |
<T> org.threadly.concurrent.future.ListenableFuture<T> |
submit(java.util.concurrent.Callable<T> task) |
<T> org.threadly.concurrent.future.ListenableFuture<T> |
submit(java.util.concurrent.Callable<T> task,
org.threadly.concurrent.TaskPriority priority) |
<T> org.threadly.concurrent.future.ListenableFuture<T> |
submit(java.lang.Runnable task,
T result) |
<T> org.threadly.concurrent.future.ListenableFuture<T> |
submit(java.lang.Runnable task,
T result,
org.threadly.concurrent.TaskPriority priority) |
<T> org.threadly.concurrent.future.ListenableFuture<T> |
submitScheduled(java.util.concurrent.Callable<T> task,
long delayInMs) |
<T> org.threadly.concurrent.future.ListenableFuture<T> |
submitScheduled(java.util.concurrent.Callable<T> task,
long delayInMs,
org.threadly.concurrent.TaskPriority priority) |
<T> org.threadly.concurrent.future.ListenableFuture<T> |
submitScheduled(java.lang.Runnable task,
T result,
long delayInMs) |
<T> org.threadly.concurrent.future.ListenableFuture<T> |
submitScheduled(java.lang.Runnable task,
T result,
long delayInMs,
org.threadly.concurrent.TaskPriority priority) |
int |
tick()
Progresses tasks for the current time.
|
int |
tick(org.threadly.util.ExceptionHandler exceptionHandler)
Progresses tasks for the current time.
|
int |
tick(long currentTime)
This progresses tasks based off the time provided.
|
int |
tick(long currentTime,
org.threadly.util.ExceptionHandler exceptionHandler)
This progresses tasks based off the time provided.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
public TestableScheduler()
TestableScheduler
scheduler.public TestableScheduler(org.threadly.concurrent.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()
isShutdown
in interface org.threadly.concurrent.SchedulerService
public int getActiveTaskCount()
getActiveTaskCount
in interface org.threadly.concurrent.SchedulerService
public int getQueuedTaskCount()
getQueuedTaskCount
in interface org.threadly.concurrent.SchedulerService
public int getWaitingForExecutionTaskCount()
getWaitingForExecutionTaskCount
in interface org.threadly.concurrent.SchedulerService
public org.threadly.concurrent.TaskPriority getDefaultPriority()
getDefaultPriority
in interface org.threadly.concurrent.PrioritySchedulerService
public long getMaxWaitForLowPriority()
getMaxWaitForLowPriority
in interface org.threadly.concurrent.PrioritySchedulerService
public int getQueuedTaskCount(org.threadly.concurrent.TaskPriority priority)
getQueuedTaskCount
in interface org.threadly.concurrent.PrioritySchedulerService
public int getWaitingForExecutionTaskCount(org.threadly.concurrent.TaskPriority priority)
getWaitingForExecutionTaskCount
in interface org.threadly.concurrent.PrioritySchedulerService
public boolean remove(java.lang.Runnable task)
remove
in interface org.threadly.concurrent.SchedulerService
public boolean remove(java.util.concurrent.Callable<?> task)
remove
in interface org.threadly.concurrent.SchedulerService
public void execute(java.lang.Runnable task)
execute
in interface java.util.concurrent.Executor
public <T> org.threadly.concurrent.future.ListenableFuture<T> submit(java.util.concurrent.Callable<T> task)
submit
in interface org.threadly.concurrent.SubmitterExecutor
public <T> org.threadly.concurrent.future.ListenableFuture<T> submit(java.lang.Runnable task, T result)
submit
in interface org.threadly.concurrent.SubmitterExecutor
public void schedule(java.lang.Runnable task, long delayInMs)
schedule
in interface org.threadly.concurrent.SubmitterScheduler
public <T> org.threadly.concurrent.future.ListenableFuture<T> submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs)
submitScheduled
in interface org.threadly.concurrent.SubmitterScheduler
public <T> org.threadly.concurrent.future.ListenableFuture<T> submitScheduled(java.lang.Runnable task, T result, long delayInMs)
submitScheduled
in interface org.threadly.concurrent.SubmitterScheduler
public void execute(java.lang.Runnable task, org.threadly.concurrent.TaskPriority priority)
execute
in interface org.threadly.concurrent.PrioritySchedulerService
public <T> org.threadly.concurrent.future.ListenableFuture<T> submit(java.util.concurrent.Callable<T> task, org.threadly.concurrent.TaskPriority priority)
submit
in interface org.threadly.concurrent.PrioritySchedulerService
public <T> org.threadly.concurrent.future.ListenableFuture<T> submit(java.lang.Runnable task, T result, org.threadly.concurrent.TaskPriority priority)
submit
in interface org.threadly.concurrent.PrioritySchedulerService
public void schedule(java.lang.Runnable task, long delayInMs, org.threadly.concurrent.TaskPriority priority)
schedule
in interface org.threadly.concurrent.PrioritySchedulerService
public <T> org.threadly.concurrent.future.ListenableFuture<T> submitScheduled(java.util.concurrent.Callable<T> task, long delayInMs, org.threadly.concurrent.TaskPriority priority)
submitScheduled
in interface org.threadly.concurrent.PrioritySchedulerService
public <T> org.threadly.concurrent.future.ListenableFuture<T> submitScheduled(java.lang.Runnable task, T result, long delayInMs, org.threadly.concurrent.TaskPriority priority)
submitScheduled
in interface org.threadly.concurrent.PrioritySchedulerService
public void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay)
scheduleWithFixedDelay
in interface org.threadly.concurrent.SubmitterScheduler
public void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period)
scheduleAtFixedRate
in interface org.threadly.concurrent.SubmitterScheduler
public void scheduleWithFixedDelay(java.lang.Runnable task, long initialDelay, long recurringDelay, org.threadly.concurrent.TaskPriority priority)
scheduleWithFixedDelay
in interface org.threadly.concurrent.PrioritySchedulerService
public void scheduleAtFixedRate(java.lang.Runnable task, long initialDelay, long period, org.threadly.concurrent.TaskPriority priority)
scheduleAtFixedRate
in interface org.threadly.concurrent.PrioritySchedulerService
public long getLastTickTime()
public int advance(long timeInMillis)
timeInMillis
- amount in milliseconds to advance the scheduler forwardpublic int advance(long timeInMillis, org.threadly.util.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(org.threadly.util.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, org.threadly.util.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.