Class InMemoryTaskScheduler
java.lang.Object
io.fluxzero.common.InMemoryTaskScheduler
- All Implemented Interfaces:
TaskScheduler
An in-memory implementation of the
TaskScheduler interface, enabling scheduling and execution of tasks in a
virtualized time environment. This class is suitable for deterministic testing and scenarios where precise control
over task execution is required.
This scheduler uses a ScheduledExecutorService to periodically check for and execute tasks that have passed
their deadlines. Tasks can be submitted for immediate execution or scheduled for execution at a specified time.
The scheduler also allows for asynchronous task execution using a provided ExecutorService or a default
direct executor if one is not provided.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionInMemoryTaskScheduler(int delay) InMemoryTaskScheduler(int delay, String threadName) InMemoryTaskScheduler(int delay, String threadName, Clock clock) InMemoryTaskScheduler(int delay, String threadName, Clock clock, ExecutorService workerPool) InMemoryTaskScheduler(int delay, Clock clock) InMemoryTaskScheduler(String threadName) InMemoryTaskScheduler(String threadName, Clock clock) InMemoryTaskScheduler(String threadName, Clock clock, ExecutorService workerPool) InMemoryTaskScheduler(String threadName, ExecutorService workerPool) InMemoryTaskScheduler(Clock clock) InMemoryTaskScheduler(ExecutorService workerPool) -
Method Summary
Modifier and TypeMethodDescriptionclock()Returns the clock associated with this scheduler.voidExecutes any scheduled tasks that are due according to the scheduler's current time.voidschedule(long deadline, ThrowingRunnable task) Schedules a task to be executed at the given epoch millisecond timestamp.voidshutdown()Shuts down the scheduler and stops execution of any pending or future tasks.voidsubmit(ThrowingRunnable task) Immediately schedules a task for execution.protected voidMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface TaskScheduler
orTimeout, schedule, schedule
-
Field Details
-
defaultDelay
public static int defaultDelay -
defaultclock
-
defaultThreadName
-
-
Constructor Details
-
InMemoryTaskScheduler
public InMemoryTaskScheduler() -
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
public InMemoryTaskScheduler(int delay) -
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
-
Method Details
-
executeExpiredTasks
public void executeExpiredTasks()Description copied from interface:TaskSchedulerExecutes any scheduled tasks that are due according to the scheduler's current time.Useful for test scenarios or manual triggering of scheduled behavior.
- Specified by:
executeExpiredTasksin interfaceTaskScheduler
-
executeExpiredTasksAsync
public void executeExpiredTasksAsync() -
tryRunTask
-
submit
Description copied from interface:TaskSchedulerImmediately schedules a task for execution.- Specified by:
submitin interfaceTaskScheduler- Parameters:
task- the task to execute
-
schedule
Description copied from interface:TaskSchedulerSchedules a task to be executed at the given epoch millisecond timestamp.- Specified by:
schedulein interfaceTaskScheduler- Parameters:
deadline- epoch milliseconds (UTC) representing the execution timetask- the task to execute- Returns:
- a
Registrationto cancel the task if needed
-
clock
Description copied from interface:TaskSchedulerReturns the clock associated with this scheduler. This clock is used to determine the current time for scheduling.In test scenarios, the clock may be a mock or virtualized instance to support deterministic test behavior.
- Specified by:
clockin interfaceTaskScheduler- Returns:
- the
Clockused by this scheduler
-
shutdown
public void shutdown()Description copied from interface:TaskSchedulerShuts down the scheduler and stops execution of any pending or future tasks.In production use, this ensures proper resource cleanup. In test environments, this may reset scheduler state.
- Specified by:
shutdownin interfaceTaskScheduler
-