Class AtomicScheduler

java.lang.Object
civitas.celestis.task.lifecycle.AtomicScheduler
All Implemented Interfaces:
Scheduler, Module

public class AtomicScheduler extends Object implements Scheduler
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

    Constructors
    Modifier
    Constructor
    Description
     
    Creates a new atomic scheduler with the default thread count.
     
    Creates a new atomic scheduler with n threads.
    protected
    Creates a new atomic scheduler by directly assigning the queue of threads.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Initializes this module.
    void
    Interrupts this module, instructing it to stop operating as soon as possible.
    protected SchedulerThread
    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.
    void
    register(Task task)
    Registers a task to this scheduler, instructing it to start executing it as soon as possible.
    void
    registerAsync(Iterable<? extends Task> tasks)
    Registers multiple tasks asynchronously to this scheduler, instructing it to distribute the tasks across multiple threads however it sees fit.
    void
    registerSync(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.
    void
    Starts this module, instructing it to start operating.
    void
    Forcefully stops this module, instructing it to interrupt all threads, and stop operating immediately regardless of its current state.
    void
    Unregisters a task from this scheduler, instructing it to stop executing it.
    void
    unregister(Iterable<? extends Task> tasks)
    Unregisters multiple tasks from this scheduler, instructing it to stop executing every task within the provided iterable object of tasks.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 with n threads.
      Parameters:
      n - The number of threads to initialize
    • AtomicScheduler

      protected AtomicScheduler(@Nonnull Deque<SchedulerThread> threads)
      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

      protected SchedulerThread 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

      public void register(@Nonnull Task task)
      Registers a task to this scheduler, instructing it to start executing it as soon as possible.
      Specified by:
      register in interface Scheduler
      Parameters:
      task - The task to register to this scheduler
    • registerSync

      public void registerSync(@Nonnull 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.
      Specified by:
      registerSync in interface Scheduler
      Parameters:
      tasks - The iterable object containing the tasks to register
    • registerAsync

      public void registerAsync(@Nonnull Iterable<? extends Task> tasks)
      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 calling Scheduler.register(Task) for each task.
      Specified by:
      registerAsync in interface Scheduler
      Parameters:
      tasks - The iterable object containing the tasks to register
    • unregister

      public void unregister(@Nonnull Task task)
      Unregisters a task from this scheduler, instructing it to stop executing it.
      Specified by:
      unregister in interface Scheduler
      Parameters:
      task - The task to unregister from this scheduler
    • unregister

      public void unregister(@Nonnull Iterable<? extends Task> tasks)
      Unregisters multiple tasks from this scheduler, instructing it to stop executing every task within the provided iterable object of tasks.
      Specified by:
      unregister in interface Scheduler
      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:
      initialize in interface Module
    • start

      public void start()
      Starts this module, instructing it to start operating.
      Specified by:
      start in interface Module
    • interrupt

      public void interrupt()
      Interrupts this module, instructing it to stop operating as soon as possible.
      Specified by:
      interrupt in interface Module
    • terminate

      public void terminate()
      Forcefully stops this module, instructing it to interrupt all threads, and stop operating immediately regardless of its current state.
      Specified by:
      terminate in interface Module