Class TaskScheduler

java.lang.Object
com.petarmc.petarlib.task.TaskScheduler

public class TaskScheduler extends Object
Schedules and executes tasks async or with a delay. It runs tasks on a separate thread and not on the main server thread. Never call Bukkit/Spigot/Paper API methods from tasks scheduled with this class, because it can cause crashes and/or data corruption. Use Bukkit's scheduler (BukkitScheduler.runTask(org.bukkit.plugin.Plugin, java.lang.Runnable)) for tasks that need to interact with the server API.
  • Constructor Details

    • TaskScheduler

      public TaskScheduler(int threads)
      Creates a TaskScheduler with a fixed number of threads.
      Parameters:
      threads - the number of threads for asynchronous tasks; minimum 1
  • Method Details

    • runAsync

      public void runAsync(Runnable r)
      Runs a task asynchronously. Exceptions thrown by the task are caught and logged.
      Parameters:
      r - the Runnable task to execute
    • runDelayed

      public void runDelayed(Runnable r, long delayMs)
      Runs a task after a specified delay in ms. Exceptions thrown by the task are caught and logged.
      Parameters:
      r - the runnable task to execute
      delayMs - the delay in ms before executing the task
    • shutdown

      public void shutdown()
      Shuts down the scheduler, waiting up to 5 seconds before forcing shutdown. Logs shutdown progress.