Interface IExecutor

All Known Implementing Classes:
ExecutorSpigot

public interface IExecutor
Represents an interface that allows scheduling (a)sync tasks.
  • Method Details

    • getVirtualExecutor

      ExecutorService getVirtualExecutor()
      A virtual executor that creates a new virtual thread for each task.

      This should be used for all tasks that spend a lot of time waiting (e.g. I/O operations).

      This executor will be shut down and replaced when the plugin restarts. This means that all tasks that are still running when the plugin restarts will be cancelled. If this is not desired, a custom executor should be created.

      Returns:
      The virtual executor.
    • scheduleOnMainThread

      <T> CompletableFuture<T> scheduleOnMainThread(Supplier<T> supplier)
      Schedules a task to be run on the main thread.
      Parameters:
      supplier - A function returning the value to be used to complete the returned IMainThreadExecutor.
      Returns:
      The result of the action.
    • runOnMainThread

      default <T> CompletableFuture<T> runOnMainThread(Supplier<T> supplier)
      Ensures a supplier is run on the main thread.

      When called from the main thread, it will be executed immediately. When called from another thread, the supplier is scheduled to run on the next tick of the main thread instead.

      Returns:
      The result of the action.
    • composeOnMainThread

      default <T> CompletableFuture<T> composeOnMainThread(Supplier<CompletableFuture<T>> supplier)
      Ensures a supplier is run on the main thread.

      When called from the main thread, it will be executed immediately. When called from another thread, the supplier is scheduled to run on the next tick of the main thread instead.

      Type Parameters:
      T - The type of the result.
      Parameters:
      supplier - The supplier to run.
      Returns:
      The result of the action.
    • runOnMainThreadWithResponse

      default CompletableFuture<Void> runOnMainThreadWithResponse(Runnable runnable)
      Ensures a runnable is run on the main thread.

      When called from the main thread, it will be executed immediately. When called from another thread, the supplier is scheduled to run on the next tick of the main thread instead.

      Returns:
      The CompletableFuture that is completed when the runnable has finished running.
    • scheduleOnMainThread

      void scheduleOnMainThread(Runnable runnable)
      Schedules an action to be run on the main thread.
    • runOnMainThread

      default void runOnMainThread(Runnable runnable)
      Ensures a runnable is run on the main thread.

      When called from the main thread, it will be executed immediately. When called from another thread, the supplier is scheduled to run on the next tick of the main thread instead.

    • supplyAsync

      <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier)
      Schedules a task to be run on the main thread.
      Parameters:
      supplier - A function returning the value to be used to complete the returned IMainThreadExecutor.
      Returns:
      The result of the action.
    • runAsync

      int runAsync(Runnable runnable)
      Schedules an action to be run asynchronously.
      Parameters:
      runnable - The action to run.
      Returns:
      The ID of the task.
    • runSync

      int runSync(Runnable runnable)
      Schedules an action to be run on the main thread.
      Parameters:
      runnable - The action to run.
      Returns:
      The ID of the task.
    • runAsyncRepeated

      int runAsyncRepeated(TimerTask timerTask, long delay, long period)
      Schedules a repeated TimerTask to be run asynchronously.
      Parameters:
      timerTask - The task to run.
      delay - The delay in milliseconds before the task is to be executed.
      period - The time in milliseconds between successive task executions.
      Returns:
      The ID of the task.
    • runAsyncRepeated

      int runAsyncRepeated(Runnable runnable, long delay, long period)
      Schedules a repeated Runnable to be run asynchronously.
      Parameters:
      runnable - The task to run.
      delay - The delay in milliseconds before the task is to be executed.
      period - The time in milliseconds between successive task executions.
      Returns:
      The ID of the task.
    • runSyncRepeated

      int runSyncRepeated(TimerTask timerTask, long delay, long period)
      Schedules a repeated TimerTask to be run on the main thread.
      Parameters:
      timerTask - The task to run.
      delay - The delay in milliseconds before the task is to be executed.
      period - The time in milliseconds between successive task executions.
      Returns:
      The ID of the task.
    • runSyncRepeated

      int runSyncRepeated(Runnable runnable, long delay, long period)
      Schedules a repeated Runnable to be run on the main thread.
      Parameters:
      runnable - The task to run.
      delay - The delay in milliseconds before the task is to be executed.
      period - The time in milliseconds between successive task executions.
      Returns:
      The ID of the task.
    • runAsyncLater

      void runAsyncLater(TimerTask timerTask, long delay)
      Schedules a repeated TimerTask to be run asynchronously.
      Parameters:
      timerTask - The task to run.
      delay - The delay in milliseconds before the task is to be executed.
    • runAsyncLater

      void runAsyncLater(Runnable runnable, long delay)
      Schedules a repeated Runnable to be run asynchronously.
      Parameters:
      runnable - The task to run.
      delay - The delay in milliseconds before the task is to be executed.
    • runSyncLater

      void runSyncLater(TimerTask timerTask, long delay)
      Schedules a repeated TimerTask to be run on the main thread.
      Parameters:
      timerTask - The task to run.
      delay - The delay in milliseconds before the task is to be executed.
    • runSyncLater

      void runSyncLater(Runnable runnable, long delay)
      Schedules a repeated Runnable to be run on the main thread.
      Parameters:
      runnable - The task to run.
      delay - The delay in milliseconds before the task is to be executed.
    • cancel

      void cancel(TimerTask timerTask, int taskID)
      Cancels a task.
      Parameters:
      timerTask - The task that is to be cancelled.
      taskID - The ID assigned to the task.
    • isMainThread

      boolean isMainThread(long threadId)
      Checks if a thread is the main thread.
      Parameters:
      threadId - The ID of the thread to compare.
      Returns:
      True if the thread is the main thread.
    • isMainThread

      default boolean isMainThread()
      Checks if the current thread is the main thread.
      Returns:
      True if the current thread is the main thread.
    • assertMainThread

      default void assertMainThread(String message)
      Asserts that this method was called from the main thread. See isMainThread().
      Parameters:
      message - The error message for the exception.
      Throws:
      IllegalStateException - When called from any thread that is not that main thread.
    • assertMainThread

      default void assertMainThread()
      Asserts that this method was called from the main thread. See isMainThread().
      Throws:
      IllegalStateException - When called from any thread that is not that main thread.
    • assertNotMainThread

      default void assertNotMainThread(String message)
      Asserts that this method was not called from the main thread. See isMainThread().
      Parameters:
      message - The error message for the exception.
      Throws:
      IllegalStateException - When called from the main thread.
    • assertNotMainThread

      default void assertNotMainThread()
      Asserts that this method was not called from the main thread. See isMainThread().
      Throws:
      IllegalStateException - When called from the main thread.