Interface MainThread

All Superinterfaces:
Executor

public interface MainThread extends Executor
Runs a mod's code on the server's main thread, which owns every game object. Tasks from other threads are queued and run during the next ticks, within a time budget per tick (forge.toml); a task is never interrupted. Called on the main thread itself, execute(Runnable) and submit(Callable) run the task immediately.

Callbacks of the returned futures run on the thread that completes them, which is the main thread: submit(...).thenAcceptAsync(result -> ..., modThread()) moves the follow-up work back. The main thread must never wait for another thread's result.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Runs task on the main thread.
    boolean
    Whether the calling thread is the main thread.
    void
    runLater(int ticks, Runnable task)
    Queues task to run on the main thread ticks frames from now, also when called on the main thread.
    submit(Callable<T> task)
    Runs task on the main thread; the future completes with its result or exception.
  • Method Details

    • execute

      void execute(Runnable task)
      Runs task on the main thread. An exception it throws is logged with the mod's id.
      Specified by:
      execute in interface Executor
      Throws:
      RejectedExecutionException - after the server started stopping
    • submit

      <T> CompletableFuture<T> submit(Callable<T> task)
      Runs task on the main thread; the future completes with its result or exception.
      Throws:
      RejectedExecutionException - after the server started stopping
    • runLater

      void runLater(int ticks, Runnable task)
      Queues task to run on the main thread ticks frames from now, also when called on the main thread.
      Throws:
      IllegalArgumentException - when ticks is less than 1
    • isMainThread

      boolean isMainThread()
      Whether the calling thread is the main thread.