Class ConcurrentHandler

java.lang.Object
com.datdeveloper.datmoddingapi.concurrentTask.ConcurrentHandler

public class ConcurrentHandler extends Object
A system for running tasks concurrently in another thread, optionally with a delay

Concurrent Tasks do execute in another thread, and therefore can lead to problems with thread unsafety

For executing tasks with a delay but on the server thread (with everything else, nice and thread safely), use DelayedEventsHandler

See Also:
  • Constructor Details

    • ConcurrentHandler

      public ConcurrentHandler()
  • Method Details

    • initialise

      public static void initialise()
      Set up the Concurrent handler, expects the config to be loaded
    • onTick

      public static void onTick(net.minecraftforge.event.TickEvent.ServerTickEvent event)
    • runOnMainThread

      public static void runOnMainThread(Runnable task)
      Queue a runnable to be executed on the server thread.
      This can be used from a Concurrent Task to execute some code, like the result of a calculation, back on the main thread.
      Note this is Non-Blocking
      Parameters:
      task - The task to run on the main thread
    • callConcurrentTask

      public static <T> Future<T> callConcurrentTask(Callable<T> task)
      Queue a concurrent task with a return value
      Type Parameters:
      T - The return type of the task
      Parameters:
      task - The task to queue
      Returns:
      A Future representing the task
    • runConcurrentTask

      public static void runConcurrentTask(Runnable task)
      Queue an concurrent task
      Parameters:
      task - The task to queue
    • scheduleConcurrentTask

      public static <T> ScheduledFuture<T> scheduleConcurrentTask(long delay, TimeUnit unit, Callable<T> task)
      Schedule a task with a return value to run after the given delay
      Type Parameters:
      T - The return type of the task
      Parameters:
      delay - The delay before running the task
      unit - The units of the delay
      task - The task to queue
      Returns:
      A ScheduledFuture representing the task
    • scheduleConcurrentTask

      public static ScheduledFuture<?> scheduleConcurrentTask(long delay, TimeUnit unit, Runnable task)
      Schedule a concurrent task
      Parameters:
      delay - The delay before running the task
      unit - The units of the delay
      task - The task to queue
      Returns:
      A ScheduledFuture representing the task
    • scheduleFixedRateConcurrentTask

      public static ScheduledFuture<?> scheduleFixedRateConcurrentTask(long initialDelay, long period, TimeUnit unit, Runnable task)
      Schedule a task to run repeatedly with a fixed period after an initial delay
      Parameters:
      initialDelay - The delay before starting the task
      period - the period between repeat executions
      unit - the units of the period and delay
      task - the task to queue
      Returns:
      A scheduledFuture representing the task (This is used to stop the event, do not lose it)