Package civitas.celestis.task.lifecycle
Class AtomicScheduler
java.lang.Object
civitas.celestis.task.lifecycle.AtomicScheduler
An asynchronous multithreaded scheduler with multiple threads, which each have
their own list of tasks and map of execution times. Atomic schedulers are atomized
in that the failure of one thread will not interfere with the operation of other threads.
While atomic schedulers can be resilient compared to other asynchronous schedulers, the added overhead of managing a queue of scheduler threads can make it slower than other asynchronous implementations, especially if tasks are registered frequently.
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a new atomic scheduler with the default thread count.AtomicScheduler(int n) Creates a new atomic scheduler withnthreads.protectedAtomicScheduler(Deque<SchedulerThread> threads) Creates a new atomic scheduler by directly assigning the queue of threads. -
Method Summary
Modifier and TypeMethodDescriptionvoidInitializes this module.voidInterrupts this module, instructing it to stop operating as soon as possible.protected SchedulerThreadPolls a thread from the queue, stores a reference to it temporarily, adds it back to the end of the queue, then returns the stored reference.voidRegisters a task to this scheduler, instructing it to start executing it as soon as possible.voidregisterAsync(Iterable<? extends Task> tasks) Registers multiple tasks asynchronously to this scheduler, instructing it to distribute the tasks across multiple threads however it sees fit.voidregisterSync(Iterable<? extends Task> tasks) Registers multiple tasks synchronously to this scheduler, instructing it to put every provided task into a single thread to ensure sequential execution.voidstart()Starts this module, instructing it to start operating.voidForcefully stops this module, instructing it to interrupt all threads, and stop operating immediately regardless of its current state.voidunregister(Task task) Unregisters a task from this scheduler, instructing it to stop executing it.voidunregister(Iterable<? extends Task> tasks) Unregisters multiple tasks from this scheduler, instructing it to stop executing every task within the provided iterable object of tasks.
-
Constructor Details
-
AtomicScheduler
public AtomicScheduler()Creates a new atomic scheduler with the default thread count. -
AtomicScheduler
public AtomicScheduler(int n) Creates a new atomic scheduler withnthreads.- Parameters:
n- The number of threads to initialize
-
AtomicScheduler
Creates a new atomic scheduler by directly assigning the queue of threads. This is a dangerous constructor, and only should ever be used by subclasses of this class.- Parameters:
threads- The queue of threads to directly assign
-
-
Method Details
-
nextThread
Polls a thread from the queue, stores a reference to it temporarily, adds it back to the end of the queue, then returns the stored reference. This is the equivalent of a++operator on a circular queue.- Returns:
- The next thread in the queue
-
register
Registers a task to this scheduler, instructing it to start executing it as soon as possible. -
registerSync
Registers multiple tasks synchronously to this scheduler, instructing it to put every provided task into a single thread to ensure sequential execution.- Specified by:
registerSyncin interfaceScheduler- Parameters:
tasks- The iterable object containing the tasks to register
-
registerAsync
Registers multiple tasks asynchronously to this scheduler, instructing it to distribute the tasks across multiple threads however it sees fit. Sequential execution is not guaranteed, and this is equivalent to iteratively callingScheduler.register(Task)for each task.- Specified by:
registerAsyncin interfaceScheduler- Parameters:
tasks- The iterable object containing the tasks to register
-
unregister
Unregisters a task from this scheduler, instructing it to stop executing it.- Specified by:
unregisterin interfaceScheduler- Parameters:
task- The task to unregister from this scheduler
-
unregister
Unregisters multiple tasks from this scheduler, instructing it to stop executing every task within the provided iterable object of tasks.- Specified by:
unregisterin interfaceScheduler- Parameters:
tasks- The iterable object containing the tasks to unregister
-
initialize
public void initialize()Initializes this module. This is expected to be called before any other method of this module is called.- Specified by:
initializein interfaceModule
-
start
public void start()Starts this module, instructing it to start operating. -
interrupt
public void interrupt()Interrupts this module, instructing it to stop operating as soon as possible. -
terminate
public void terminate()Forcefully stops this module, instructing it to interrupt all threads, and stop operating immediately regardless of its current state.
-