Class TaskScheduler
java.lang.Object
net.flectone.pulse.execution.scheduler.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
-
Constructor Summary
ConstructorsConstructorDescriptionTaskScheduler(FLogger fLogger, com.google.inject.Provider<FPlayerService> fPlayerServiceProvider) -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks whether the scheduler is currently disabled.voidonTick()Advances the scheduler by one tick and processes all tasks scheduled for the current tick.voidreload()Reloads the scheduler by processing all pending tasks at the current tick, clearing all scheduled tasks, and resetting the tick counter to zero.runAsync(SchedulerRunnable runnable) Schedules a task to run asynchronously on the thread pool.runAsync(SchedulerRunnable runnable, boolean independent) Schedules a task to run asynchronously with control over independent execution.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.protected RunnablewrapExceptionRunnable(SchedulerRunnable runnable, CompletableFuture<Void> future)
-
Constructor Details
-
TaskScheduler
@Inject public TaskScheduler(FLogger fLogger, com.google.inject.Provider<FPlayerService> fPlayerServiceProvider)
-
-
Method Details
-
reload
public void reload()Reloads the scheduler by processing all pending tasks at the current tick, clearing all scheduled tasks, and resetting the tick counter to zero. -
start
public void start()Starts the scheduler by creating and initializing the executor service with the configured thread pool settings. -
shutdown
public 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 with control over independent execution. If not independent and already on an async thread, runs immediately to avoid unnecessary threading.- Parameters:
runnable- the task to executeindependent- if true, always schedules even if already on async thread; if false, may run immediately- 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 that completes when the task is first executed
-
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 that completes when the task is first executed
-
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 that completes when the task is first executed
-
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 that completes when the task is first executed
-
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 that completes when the task is first executed
-
runPlayerAsyncTimer
public 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 that completes when the task is first executed
-
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
public boolean isDisabled()Checks whether the scheduler is currently disabled.- Returns:
- true if the scheduler is disabled, false otherwise
-
onTick
public 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. -
wrapExceptionRunnable
protected Runnable wrapExceptionRunnable(SchedulerRunnable runnable, CompletableFuture<Void> future) -
getExecutorService
-