Package com.petarmc.lib.task
Class TaskScheduler
java.lang.Object
com.petarmc.lib.task.TaskScheduler
Schedules and executes tasks async or with a delay.
Provides safe handling of exceptions and logging.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final PLoggerprivate final ScheduledExecutorService -
Constructor Summary
ConstructorsConstructorDescriptionTaskScheduler(int threads) Creates a TaskScheduler with a fixed number of threads. -
Method Summary
Modifier and TypeMethodDescriptionvoidRuns a task asynchronously.voidrunDelayed(Runnable r, long delayMs) Runs a task after a specified delay in ms.voidshutdown()Shuts down the scheduler, waiting up to 5 seconds before forcing shutdown.
-
Field Details
-
log
-
scheduler
-
-
Constructor Details
-
TaskScheduler
public TaskScheduler(int threads) Creates a TaskScheduler with a fixed number of threads.- Parameters:
threads- the number of threads for asynchronous tasks; minimum 1
-
-
Method Details
-
runAsync
Runs a task asynchronously. Exceptions thrown by the task are caught and logged.- Parameters:
r- the Runnable task to execute
-
runDelayed
Runs a task after a specified delay in ms. Exceptions thrown by the task are caught and logged.- Parameters:
r- the runnable task to executedelayMs- the delay in ms before executing the task
-
shutdown
public void shutdown()Shuts down the scheduler, waiting up to 5 seconds before forcing shutdown. Logs shutdown progress.
-