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, int delay, TimeUnit unit)
      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
      delay - 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.
    • runRepeating

      public void runRepeating(Runnable r, int period, int initialDelay, TimeUnit unit)
      Schedules a repeating task with an initial delay and period. Exceptions thrown by the task are caught and logged.
      Parameters:
      r - the runnable task to execute
      period - the period between subsequent executions (in the given unit)
      initialDelay - the delay before the first execution (in the given unit)
      unit - the time unit for initialDelay and period