Class TaskManager

java.lang.Object
com.plotsquared.core.util.task.TaskManager

public abstract class TaskManager extends Object
Task manager that handles scheduling of tasks. Synchronous methods make no guarantee of being scheduled on the server thread, instead they guarantee that no two synchronous operations happen at the same time. Implementations of the task manager might make other guarantees. All asynchronous operations will happen on another thread, no matter where they're scheduled from.
  • Field Details

  • Constructor Details

    • TaskManager

      public TaskManager()
  • Method Details

    • addToTeleportQueue

      public static void addToTeleportQueue(@NonNull String string)
      Add a string to the teleport queue
      Parameters:
      string - String to add
    • removeFromTeleportQueue

      public static boolean removeFromTeleportQueue(@NonNull String string)
      Remove a string from the teleport queue
      Parameters:
      string - String to remove return true if the value was stored in the map, or false
      Returns:
      if string was actually removed
    • addTask

      public static void addTask(@NonNull PlotSquaredTask task, int id)
      Add a task to the task map
      Parameters:
      task - Task
      id - Task ID
    • removeTask

      public static @Nullable PlotSquaredTask removeTask(int id)
      Remove a task from the task map and return the stored value
      Parameters:
      id - Task ID
      Returns:
      Task if stored, or null
    • runTaskRepeat

      public static @NonNull PlotSquaredTask runTaskRepeat(@Nullable Runnable runnable, @NonNull TaskTime taskTime)
      Run a repeating synchronous task. If using a platform scheduler, this is guaranteed to run on the server thread
      Parameters:
      runnable - Task to run
      taskTime - Task interval
      Returns:
      Created task object, can be used to cancel the task
    • runTaskAsync

      public static void runTaskAsync(@Nullable Runnable runnable)
      Run an asynchronous task. This will never run on the server thread
      Parameters:
      runnable - Task to run
    • runTask

      public static void runTask(@Nullable Runnable runnable)
      Run a synchronous task. If using a platform scheduler, this is guaranteed to run on the server thread
      Parameters:
      runnable - Task to run
    • runTaskLater

      public static void runTaskLater(@Nullable Runnable runnable, @NonNull TaskTime taskTime)
      Run a synchronous task after a given delay. If using a platform scheduler, this is guaranteed to run on the server thread
      Parameters:
      runnable - Task to run
      taskTime - Task delay
    • runTaskLaterAsync

      public static void runTaskLaterAsync(@Nullable Runnable runnable, @NonNull TaskTime taskTime)
      Run an asynchronous task after a given delay. This will never run on the server thread
      Parameters:
      runnable - Task to run
      taskTime - Task delay
    • getPlatformImplementation

      public static @Nullable TaskManager getPlatformImplementation()
    • setPlatformImplementation

      public static void setPlatformImplementation(@NonNull TaskManager implementation)
    • objectTask

      public <T> CompletableFuture<Void> objectTask(@NonNull Collection<T> objects, @NonNull RunnableVal<T> task)
      Break up a series of tasks so that they can run without lagging the server
      Type Parameters:
      T - Object type
      Parameters:
      objects - Objects to perform the task on
      task - Task to perform
      Returns:
      Future that completes when the tasks are done
    • sync

      public <T> T sync(@NonNull Callable<T> function) throws Exception
      Make a synchronous method call and return the result
      Type Parameters:
      T - Return type
      Parameters:
      function - Method to call
      Returns:
      Method result
      Throws:
      Exception - If the call fails
    • sync

      public abstract <T> T sync(@NonNull Callable<T> function, int timeout) throws Exception
      Make a synchronous method call and return the result
      Type Parameters:
      T - Return type
      Parameters:
      function - Method to call
      timeout - Timeout (ms)
      Returns:
      Method result
      Throws:
      Exception - If the call fails
    • callMethodSync

      public abstract <T> Future<T> callMethodSync(@NonNull Callable<T> method)
      Call a method synchronously and return a future with the result of the result
      Type Parameters:
      T - Return type
      Parameters:
      method - Method to be ran synchronously
      Returns:
      Future completing with the result
    • taskRepeat

      public abstract PlotSquaredTask taskRepeat(@NonNull Runnable runnable, @NonNull TaskTime taskTime)
      Run a repeating synchronous task. If using a platform scheduler, this is guaranteed to run on the server thread
      Parameters:
      runnable - Task to run
      taskTime - Task interval
      Returns:
      Created task object, can be used to cancel the task
    • taskRepeatAsync

      public abstract PlotSquaredTask taskRepeatAsync(@NonNull Runnable runnable, @NonNull TaskTime taskTime)
      Run a repeating asynchronous task. This will never run on the server thread
      Parameters:
      runnable - Task to run
      taskTime - Task interval
      Returns:
      Created task object, can be used to cancel the task
    • taskAsync

      public abstract void taskAsync(@NonNull Runnable runnable)
      Run an asynchronous task. This will never run on the server thread
      Parameters:
      runnable - Task to run
    • task

      public abstract void task(@NonNull Runnable runnable)
      Run a synchronous task. If using a platform scheduler, this is guaranteed to run on the server thread
      Parameters:
      runnable - Task to run
    • taskLater

      public abstract void taskLater(@NonNull Runnable runnable, @NonNull TaskTime taskTime)
      Run a synchronous task after a given delay. If using a platform scheduler, this is guaranteed to run on the server thread
      Parameters:
      runnable - Task to run
      taskTime - Task delay
    • taskLaterAsync

      public abstract void taskLaterAsync(@NonNull Runnable runnable, @NonNull TaskTime taskTime)
      Run an asynchronous task after a given delay. This will never run on the server thread
      Parameters:
      runnable - Task to run
      taskTime - Task delay