Interface TaskScheduler


public interface TaskScheduler
A task scheduler that manages asynchronous and synchronous task execution with tick-based scheduling support.

This scheduler provides methods for running tasks immediately, with delays, or on repeating intervals. It supports both async (thread pool) and sync (current thread) execution modes. Tasks are scheduled using a tick-based system where each tick represents a scheduling unit.

Since:
0.5.3
  • Field Details

  • Method Details

    • getExecutorService

      ExecutorService getExecutorService()
      The pool the async tasks run on.
      Returns:
      the executor
    • await

      <T> T await(CompletableFuture<T> future, T fallback, Duration timeout, String description)
      Waits for a task running elsewhere and hands back a fallback if it does not answer in time.
      Type Parameters:
      T - the result type
      Parameters:
      future - what to wait for
      fallback - what to return when it does not answer, fails or is interrupted
      timeout - how long to wait
      description - what is being waited for, for the warning in the console
      Returns:
      the result, or the fallback
    • await

      default <T> T await(CompletableFuture<T> future, T fallback, String description)
      Waits for a task running elsewhere for DEFAULT_AWAIT_TIMEOUT.
      Type Parameters:
      T - the result type
      Parameters:
      future - what to wait for
      fallback - what to return when it does not answer, fails or is interrupted
      description - what is being waited for, for the warning in the console
      Returns:
      the result, or the fallback
    • reload

      void reload()
      Runs the pending tasks and clears the queue.
    • start

      void start()
      Starts the scheduler by creating and initializing the executor service with the configured thread pool settings.
    • shutdown

      void shutdown()
      Shuts down the scheduler gracefully by disabling new task submissions, processing remaining tasks at the current tick, and waiting for executing tasks to complete within the configured timeout period. If tasks don't complete within the timeout, forces immediate shutdown.
    • runAsync

      Schedules a task to run asynchronously on the thread pool.
      Parameters:
      runnable - the task to execute
      Returns:
      a CompletableFuture that completes when the task finishes
    • runAsync

      CompletableFuture<Void> runAsync(ModuleName moduleName, SchedulerRunnable runnable)
      Schedules a task to run asynchronously and associates it with a specific module. Tasks for the same module are executed sequentially in a chain.
      Parameters:
      moduleName - the module to associate with this task
      runnable - the task to execute
      Returns:
      a CompletableFuture that completes when the task finishes
    • runAsyncLater

      CompletableFuture<Void> runAsyncLater(SchedulerRunnable runnable)
      Schedules a task to run asynchronously after a default delay of 20 ticks.
      Parameters:
      runnable - the task to execute
      Returns:
      a CompletableFuture that completes when the task finishes
    • runAsyncLater

      CompletableFuture<Void> runAsyncLater(SchedulerRunnable runnable, long delay)
      Schedules a task to run asynchronously after the specified delay.
      Parameters:
      runnable - the task to execute
      delay - the number of ticks to wait before executing the task
      Returns:
      a CompletableFuture that completes when the task finishes
    • runAsyncTimer

      CompletableFuture<Void> runAsyncTimer(SchedulerRunnable runnable, long period)
      Schedules a task to run asynchronously on a repeating interval with a default initial delay of 5 ticks.
      Parameters:
      runnable - the task to execute repeatedly
      period - the number of ticks between consecutive executions
      Returns:
      a CompletableFuture used to stop the task, it never completes on its own
    • runAsyncTimer

      CompletableFuture<Void> runAsyncTimer(SchedulerRunnable runnable, long delay, long period)
      Schedules a task to run asynchronously on a repeating interval with custom delay and period.
      Parameters:
      runnable - the task to execute repeatedly
      delay - the number of ticks to wait before the first execution
      period - the number of ticks between consecutive executions
      Returns:
      a CompletableFuture used to stop the task, it never completes on its own
    • runSync

      Schedules a task to run synchronously on the current thread.
      Parameters:
      runnable - the task to execute
      Returns:
      a CompletableFuture that completes when the task finishes
    • runSyncLater

      CompletableFuture<Void> runSyncLater(SchedulerRunnable runnable, long delay)
      Schedules a task to run synchronously on the current thread after a specified delay.
      Parameters:
      runnable - the task to execute
      delay - the number of ticks to wait before executing the task
      Returns:
      a CompletableFuture that completes when the task finishes
    • runSyncTimer

      CompletableFuture<Void> runSyncTimer(SchedulerRunnable runnable, long period)
      Schedules a task to run synchronously on a repeating interval with a default initial delay of 5 ticks.
      Parameters:
      runnable - the task to execute repeatedly
      period - the number of ticks between consecutive executions
      Returns:
      a CompletableFuture used to stop the task, it never completes on its own
    • runSyncTimer

      CompletableFuture<Void> runSyncTimer(SchedulerRunnable runnable, long delay, long period)
      Schedules a task to run synchronously on a repeating interval with custom delay and period.
      Parameters:
      runnable - the task to execute repeatedly
      delay - the number of ticks to wait before the first execution
      period - the number of ticks between consecutive executions
      Returns:
      a CompletableFuture used to stop the task, it never completes on its own
    • runRegion

      CompletableFuture<Void> runRegion(FPlayer fPlayer, SchedulerRunnable runnable)
      Schedules a region-related task to run asynchronously.
      Parameters:
      fPlayer - the player associated with the region (currently unused)
      runnable - the task to execute
      Returns:
      a CompletableFuture that completes when the task finishes
    • runPlayerAsyncTimer

      CompletableFuture<Void> runPlayerAsyncTimer(Consumer<FPlayer> fPlayerConsumer, long period)
      Schedules a task to run asynchronously on a repeating interval for all players, with a default initial delay of 5 ticks. The consumer receives each platform player.
      Parameters:
      fPlayerConsumer - the consumer to apply to each platform player
      period - the number of ticks between consecutive executions
      Returns:
      a CompletableFuture used to stop the task, it never completes on its own
    • runPlayerAsyncTimer

      CompletableFuture<Void> runPlayerAsyncTimer(Consumer<FPlayer> fPlayerConsumer, long delay, long period)
      Schedules a task to run asynchronously on a repeating interval for all players with custom delay and period. The consumer receives each platform player.
      Parameters:
      fPlayerConsumer - the consumer to apply to each platform player
      delay - the number of ticks to wait before the first execution
      period - the number of ticks between consecutive executions
      Returns:
      a CompletableFuture used to stop the task, it never completes on its own
    • runImmediately

      CompletableFuture<Void> runImmediately(SchedulerRunnable runnable)
      Executes a task immediately on the current thread without scheduling.
      Parameters:
      runnable - the task to execute immediately
      Returns:
      a CompletableFuture that completes when the task finishes
    • isDisabled

      boolean isDisabled()
      Checks whether the scheduler is currently disabled.
      Returns:
      true if the scheduler is disabled, false otherwise
    • onTick

      void onTick()
      Advances the scheduler by one tick and processes all tasks scheduled for the current tick. This method should be called regularly by the game loop or timing mechanism.