Class ConcurrentHandler
java.lang.Object
com.datdeveloper.datmoddingapi.concurrentTask.ConcurrentHandler
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Future
<T> callConcurrentTask
(Callable<T> task) Queue a concurrent task with a return valuestatic void
Set up the Concurrent handler, expects the config to be loadedstatic void
onTick
(net.neoforged.neoforge.event.tick.ServerTickEvent.Post event) A Server tick event used to handle any Runnables that have been passed from a Concurrent thread to be executed on the main threadstatic void
runConcurrentTask
(Runnable task) Queue an concurrent taskstatic void
runOnMainThread
(Runnable task) Queue a runnable to be executed on the server thread.static ScheduledFuture
<?> scheduleConcurrentTask
(long delay, TimeUnit unit, Runnable task) Schedule a concurrent taskstatic <T> ScheduledFuture
<T> scheduleConcurrentTask
(long delay, TimeUnit unit, Callable<T> task) Schedule a task with a return value to run after the given delaystatic ScheduledFuture
<?> scheduleFixedRateConcurrentTask
(long initialDelay, long period, TimeUnit unit, Runnable task) Schedule a task to run repeatedly with a fixed period after an initial delay
-
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.neoforged.neoforge.event.tick.ServerTickEvent.Post event) A Server tick event used to handle any Runnables that have been passed from a Concurrent thread to be executed on the main thread- Parameters:
event
- The tick event
-
runOnMainThread
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
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
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 taskunit
- The units of the delaytask
- The task to queue- Returns:
- A ScheduledFuture representing the task
-
scheduleConcurrentTask
Schedule a concurrent task- Parameters:
delay
- The delay before running the taskunit
- The units of the delaytask
- 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 taskperiod
- the period between repeat executionsunit
- the units of the period and delaytask
- the task to queue- Returns:
- A scheduledFuture representing the task (This is used to stop the event, do not lose it)
-