Class Scheduler

java.lang.Object
fr.hytale.loader.scheduler.Scheduler

public class Scheduler extends Object
HytaleLoader task scheduler for executing tasks synchronously and asynchronously.

This scheduler provides methods to run tasks immediately, after a delay, or repeatedly. Tasks are executed using a thread pool executor.

Usage Examples:

Scheduler scheduler = new Scheduler();

// Run immediately
scheduler.runTask(() -> {
    System.out.println("Immediate task");
});

// Run after 5 seconds
scheduler.runTaskLater(() -> {
    System.out.println("Delayed task");
}, 5000);

// Run every 1 second, starting after 2 seconds
ScheduledTask task = scheduler.runTaskTimer(() -> {
    System.out.println("Repeating task");
}, 2000, 1000);

// Cancel the repeating task later
task.cancel();
Since:
1.0.3
Version:
1.0.4
Author:
HytaleLoader
  • Constructor Details

    • Scheduler

      public Scheduler()
      Creates a new scheduler with default thread pool sizes.
    • Scheduler

      public Scheduler(int corePoolSize, int asyncPoolSize)
      Creates a new scheduler with custom thread pool sizes.
      Parameters:
      corePoolSize - the number of threads to keep in the pool
      asyncPoolSize - the number of threads for async tasks
  • Method Details

    • runTask

      public ScheduledTask runTask(Runnable task)
      Runs a task immediately (on the next available thread).
      Parameters:
      task - the task to run
      Returns:
      a ScheduledTask that can be used to cancel the task
    • runTaskLater

      public ScheduledTask runTaskLater(Runnable task, long delayMillis)
      Runs a task after a specified delay.
      Parameters:
      task - the task to run
      delayMillis - the delay in milliseconds before running the task
      Returns:
      a ScheduledTask that can be used to cancel the task
    • runTaskTimer

      public ScheduledTask runTaskTimer(Runnable task, long initialDelayMillis, long periodMillis)
      Runs a task repeatedly with a fixed delay between executions.
      Parameters:
      task - the task to run
      initialDelayMillis - the delay before the first execution
      periodMillis - the period between successive executions
      Returns:
      a ScheduledTask that can be used to cancel the task
    • runTaskAsync

      public CompletableFuture<Void> runTaskAsync(Runnable task)
      Runs a task asynchronously on a separate thread pool.

      Async tasks do not block the main server thread and are useful for I/O operations, database queries, or other long-running operations.

      Parameters:
      task - the task to run asynchronously
      Returns:
      a CompletableFuture representing the async task
    • runTaskAsync

      public <T> CompletableFuture<T> runTaskAsync(Callable<T> task)
      Runs a task asynchronously and returns a result.
      Type Parameters:
      T - the type of the result
      Parameters:
      task - the task to run
      Returns:
      a CompletableFuture representing the async task result
    • shutdown

      public void shutdown()
      Cancels all scheduled tasks and shuts down the scheduler.

      This should be called when the plugin is disabled.

    • isShutdown

      public boolean isShutdown()
      Checks if the scheduler has been shut down.
      Returns:
      true if the scheduler is shut down