Class Scheduler
java.lang.Object
fr.hytale.loader.scheduler.Scheduler
HytaleLoader task scheduler for executing tasks synchronously and
asynchronously.
This scheduler provides methods to run tasks immediately, after a delay, or repeatedly. Tasks are executed using a thread pool executor.
Usage Examples:
Scheduler scheduler = new Scheduler();
// Run immediately
scheduler.runTask(() -> {
System.out.println("Immediate task");
});
// Run after 5 seconds
scheduler.runTaskLater(() -> {
System.out.println("Delayed task");
}, 5000);
// Run every 1 second, starting after 2 seconds
ScheduledTask task = scheduler.runTaskTimer(() -> {
System.out.println("Repeating task");
}, 2000, 1000);
// Cancel the repeating task later
task.cancel();
- Since:
- 1.0.3
- Version:
- 1.0.4
- Author:
- HytaleLoader
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks if the scheduler has been shut down.Runs a task immediately (on the next available thread).runTaskAsync(Runnable task) Runs a task asynchronously on a separate thread pool.<T> CompletableFuture<T> runTaskAsync(Callable<T> task) Runs a task asynchronously and returns a result.runTaskLater(Runnable task, long delayMillis) Runs a task after a specified delay.runTaskTimer(Runnable task, long initialDelayMillis, long periodMillis) Runs a task repeatedly with a fixed delay between executions.voidshutdown()Cancels all scheduled tasks and shuts down the scheduler.
-
Constructor Details
-
Scheduler
public Scheduler()Creates a new scheduler with default thread pool sizes. -
Scheduler
public Scheduler(int corePoolSize, int asyncPoolSize) Creates a new scheduler with custom thread pool sizes.- Parameters:
corePoolSize- the number of threads to keep in the poolasyncPoolSize- the number of threads for async tasks
-
-
Method Details
-
runTask
Runs a task immediately (on the next available thread).- Parameters:
task- the task to run- Returns:
- a ScheduledTask that can be used to cancel the task
-
runTaskLater
Runs a task after a specified delay.- Parameters:
task- the task to rundelayMillis- the delay in milliseconds before running the task- Returns:
- a ScheduledTask that can be used to cancel the task
-
runTaskTimer
Runs a task repeatedly with a fixed delay between executions.- Parameters:
task- the task to runinitialDelayMillis- the delay before the first executionperiodMillis- the period between successive executions- Returns:
- a ScheduledTask that can be used to cancel the task
-
runTaskAsync
Runs a task asynchronously on a separate thread pool.Async tasks do not block the main server thread and are useful for I/O operations, database queries, or other long-running operations.
- Parameters:
task- the task to run asynchronously- Returns:
- a CompletableFuture representing the async task
-
runTaskAsync
Runs a task asynchronously and returns a result.- Type Parameters:
T- the type of the result- Parameters:
task- the task to run- Returns:
- a CompletableFuture representing the async task result
-
shutdown
public void shutdown()Cancels all scheduled tasks and shuts down the scheduler.This should be called when the plugin is disabled.
-
isShutdown
public boolean isShutdown()Checks if the scheduler has been shut down.- Returns:
- true if the scheduler is shut down
-