Class TaskScheduler

java.lang.Object
net.flectone.pulse.execution.scheduler.TaskScheduler

public class TaskScheduler extends Object
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
  • Constructor Details

    • TaskScheduler

      @Inject public TaskScheduler(FLogger fLogger, com.google.inject.Provider<FPlayerService> fPlayerServiceProvider)
  • Method Details

    • reload

      public void reload()
      Reloads the scheduler by processing all pending tasks at the current tick, clearing all scheduled tasks, and resetting the tick counter to zero.
    • start

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

      public 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

      public CompletableFuture<Void> runAsync(SchedulerRunnable runnable)
      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

      public CompletableFuture<Void> runAsync(SchedulerRunnable runnable, boolean independent)
      Schedules a task to run asynchronously with control over independent execution. If not independent and already on an async thread, runs immediately to avoid unnecessary threading.
      Parameters:
      runnable - the task to execute
      independent - if true, always schedules even if already on async thread; if false, may run immediately
      Returns:
      a CompletableFuture that completes when the task finishes
    • runAsyncLater

      public 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

      public 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

      public 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 that completes when the task is first executed
    • runAsyncTimer

      public 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 that completes when the task is first executed
    • runSync

      public CompletableFuture<Void> runSync(SchedulerRunnable runnable)
      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

      public 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

      public 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 that completes when the task is first executed
    • runSyncTimer

      public 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 that completes when the task is first executed
    • runRegion

      public 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

      public 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 that completes when the task is first executed
    • runPlayerAsyncTimer

      public 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 that completes when the task is first executed
    • runImmediately

      public 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

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

      public 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.
    • wrapExceptionRunnable

      protected Runnable wrapExceptionRunnable(SchedulerRunnable runnable, CompletableFuture<Void> future)
    • getExecutorService

      public ExecutorService getExecutorService()