Interface TaskScheduler
public interface TaskScheduler
A task scheduler that manages asynchronous and synchronous task execution
with tick-based scheduling support.
This scheduler provides methods for running tasks immediately, with delays, or on repeating intervals. It supports both async (thread pool) and sync (current thread) execution modes. Tasks are scheduled using a tick-based system where each tick represents a scheduling unit.
- Since:
- 0.5.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationHow longawait(CompletableFuture, Object, String)gives a task before it gives up. -
Method Summary
Modifier and TypeMethodDescriptiondefault <T> Tawait(CompletableFuture<T> future, T fallback, String description) Waits for a task running elsewhere forDEFAULT_AWAIT_TIMEOUT.<T> Tawait(CompletableFuture<T> future, T fallback, Duration timeout, String description) Waits for a task running elsewhere and hands back a fallback if it does not answer in time.The pool the async tasks run on.booleanChecks whether the scheduler is currently disabled.voidonTick()Advances the scheduler by one tick and processes all tasks scheduled for the current tick.voidreload()Runs the pending tasks and clears the queue.runAsync(ModuleName moduleName, SchedulerRunnable runnable) Schedules a task to run asynchronously and associates it with a specific module.runAsync(SchedulerRunnable runnable) Schedules a task to run asynchronously on the thread pool.runAsyncLater(SchedulerRunnable runnable) Schedules a task to run asynchronously after a default delay of 20 ticks.runAsyncLater(SchedulerRunnable runnable, long delay) Schedules a task to run asynchronously after the specified delay.runAsyncTimer(SchedulerRunnable runnable, long period) Schedules a task to run asynchronously on a repeating interval with a default initial delay of 5 ticks.runAsyncTimer(SchedulerRunnable runnable, long delay, long period) Schedules a task to run asynchronously on a repeating interval with custom delay and period.runImmediately(SchedulerRunnable runnable) Executes a task immediately on the current thread without scheduling.runPlayerAsyncTimer(Consumer<FPlayer> fPlayerConsumer, long period) Schedules a task to run asynchronously on a repeating interval for all players, with a default initial delay of 5 ticks.runPlayerAsyncTimer(Consumer<FPlayer> fPlayerConsumer, long delay, long period) Schedules a task to run asynchronously on a repeating interval for all players with custom delay and period.runRegion(FPlayer fPlayer, SchedulerRunnable runnable) Schedules a region-related task to run asynchronously.runSync(SchedulerRunnable runnable) Schedules a task to run synchronously on the current thread.runSyncLater(SchedulerRunnable runnable, long delay) Schedules a task to run synchronously on the current thread after a specified delay.runSyncTimer(SchedulerRunnable runnable, long period) Schedules a task to run synchronously on a repeating interval with a default initial delay of 5 ticks.runSyncTimer(SchedulerRunnable runnable, long delay, long period) Schedules a task to run synchronously on a repeating interval with custom delay and period.voidshutdown()Shuts down the scheduler gracefully by disabling new task submissions, processing remaining tasks at the current tick, and waiting for executing tasks to complete within the configured timeout period.voidstart()Starts the scheduler by creating and initializing the executor service with the configured thread pool settings.
-
Field Details
-
DEFAULT_AWAIT_TIMEOUT
How longawait(CompletableFuture, Object, String)gives a task before it gives up.
-
-
Method Details
-
getExecutorService
-
await
Waits for a task running elsewhere and hands back a fallback if it does not answer in time.- Type Parameters:
T- the result type- Parameters:
future- what to wait forfallback- what to return when it does not answer, fails or is interruptedtimeout- how long to waitdescription- what is being waited for, for the warning in the console- Returns:
- the result, or the fallback
-
await
Waits for a task running elsewhere forDEFAULT_AWAIT_TIMEOUT.- Type Parameters:
T- the result type- Parameters:
future- what to wait forfallback- what to return when it does not answer, fails or is interrupteddescription- what is being waited for, for the warning in the console- Returns:
- the result, or the fallback
-
reload
void reload()Runs the pending tasks and clears the queue. -
start
void start()Starts the scheduler by creating and initializing the executor service with the configured thread pool settings. -
shutdown
void shutdown()Shuts down the scheduler gracefully by disabling new task submissions, processing remaining tasks at the current tick, and waiting for executing tasks to complete within the configured timeout period. If tasks don't complete within the timeout, forces immediate shutdown. -
runAsync
Schedules a task to run asynchronously on the thread pool.- Parameters:
runnable- the task to execute- Returns:
- a CompletableFuture that completes when the task finishes
-
runAsync
Schedules a task to run asynchronously and associates it with a specific module. Tasks for the same module are executed sequentially in a chain.- Parameters:
moduleName- the module to associate with this taskrunnable- the task to execute- Returns:
- a CompletableFuture that completes when the task finishes
-
runAsyncLater
Schedules a task to run asynchronously after a default delay of 20 ticks.- Parameters:
runnable- the task to execute- Returns:
- a CompletableFuture that completes when the task finishes
-
runAsyncLater
Schedules a task to run asynchronously after the specified delay.- Parameters:
runnable- the task to executedelay- the number of ticks to wait before executing the task- Returns:
- a CompletableFuture that completes when the task finishes
-
runAsyncTimer
Schedules a task to run asynchronously on a repeating interval with a default initial delay of 5 ticks.- Parameters:
runnable- the task to execute repeatedlyperiod- the number of ticks between consecutive executions- Returns:
- a CompletableFuture used to stop the task, it never completes on its own
-
runAsyncTimer
Schedules a task to run asynchronously on a repeating interval with custom delay and period.- Parameters:
runnable- the task to execute repeatedlydelay- the number of ticks to wait before the first executionperiod- the number of ticks between consecutive executions- Returns:
- a CompletableFuture used to stop the task, it never completes on its own
-
runSync
Schedules a task to run synchronously on the current thread.- Parameters:
runnable- the task to execute- Returns:
- a CompletableFuture that completes when the task finishes
-
runSyncLater
Schedules a task to run synchronously on the current thread after a specified delay.- Parameters:
runnable- the task to executedelay- the number of ticks to wait before executing the task- Returns:
- a CompletableFuture that completes when the task finishes
-
runSyncTimer
Schedules a task to run synchronously on a repeating interval with a default initial delay of 5 ticks.- Parameters:
runnable- the task to execute repeatedlyperiod- the number of ticks between consecutive executions- Returns:
- a CompletableFuture used to stop the task, it never completes on its own
-
runSyncTimer
Schedules a task to run synchronously on a repeating interval with custom delay and period.- Parameters:
runnable- the task to execute repeatedlydelay- the number of ticks to wait before the first executionperiod- the number of ticks between consecutive executions- Returns:
- a CompletableFuture used to stop the task, it never completes on its own
-
runRegion
Schedules a region-related task to run asynchronously.- Parameters:
fPlayer- the player associated with the region (currently unused)runnable- the task to execute- Returns:
- a CompletableFuture that completes when the task finishes
-
runPlayerAsyncTimer
Schedules a task to run asynchronously on a repeating interval for all players, with a default initial delay of 5 ticks. The consumer receives each platform player.- Parameters:
fPlayerConsumer- the consumer to apply to each platform playerperiod- the number of ticks between consecutive executions- Returns:
- a CompletableFuture used to stop the task, it never completes on its own
-
runPlayerAsyncTimer
CompletableFuture<Void> runPlayerAsyncTimer(Consumer<FPlayer> fPlayerConsumer, long delay, long period) Schedules a task to run asynchronously on a repeating interval for all players with custom delay and period. The consumer receives each platform player.- Parameters:
fPlayerConsumer- the consumer to apply to each platform playerdelay- the number of ticks to wait before the first executionperiod- the number of ticks between consecutive executions- Returns:
- a CompletableFuture used to stop the task, it never completes on its own
-
runImmediately
Executes a task immediately on the current thread without scheduling.- Parameters:
runnable- the task to execute immediately- Returns:
- a CompletableFuture that completes when the task finishes
-
isDisabled
boolean isDisabled()Checks whether the scheduler is currently disabled.- Returns:
- true if the scheduler is disabled, false otherwise
-
onTick
void onTick()Advances the scheduler by one tick and processes all tasks scheduled for the current tick. This method should be called regularly by the game loop or timing mechanism.
-