Class SchedulerThread

java.lang.Object
java.lang.Thread
civitas.celestis.task.lifecycle.SchedulerThread
All Implemented Interfaces:
Scheduler, Module, Runnable
Direct Known Subclasses:
SyncScheduler

public class SchedulerThread extends Thread implements Scheduler
A thread which handles the registration and execution of tasks. This is a strict scheduler, meaning it has no delay between each iteration of looping through the list of tasks allocated to this thread.
  • Constructor Details

    • SchedulerThread

      public SchedulerThread()
      Creates a new scheduler thread.
    • SchedulerThread

      public SchedulerThread(@Nonnull String name)
      Creates a new scheduler thread.
      Parameters:
      name - The name of this thread
    • SchedulerThread

      public SchedulerThread(@Nonnull String name, @Nonnull PrintStream printStream)
      Creates a new scheduler thread.
      Parameters:
      name - The name of this thread
      printStream - The print stream to print error messages to
    • SchedulerThread

      protected SchedulerThread(@Nonnull String name, @Nonnull List<Task> tasks, @Nonnull Map<Task,Long> executionTimes, @Nonnull PrintStream printStream)
      Creates a new scheduler thread. If this scheduler shares the list of tasks and/or the map of execution times with another thread, the list and map instances must be thread-safe. They must also be mutable.
      Parameters:
      name - The name of this thread
      tasks - The list of tasks allocated to this thread
      executionTimes - The map of recent execution times
      printStream - The print stream to print error messages to
  • Method Details

    • 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
    • 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