Package com.smartfoxserver.util
Class TaskScheduler
java.lang.Object
com.smartfoxserver.util.TaskScheduler
- All Implemented Interfaces:
com.smartfoxserver.bitswarm.service.IService
Overview
The TaskScheduler class allows to handle multiple interval-based tasks or delayed tasks in an efficient and convenient way. The class uses the Java's ScheduledThreadPoolExecutor backed by virtual threads to manage concurrent tasks.In SmartFoxServer 3 all Extension's task should be submitted to the main TaskScheduler, there's no need to create new ones.
NOTE: Exceptions can stop the execution of the task silently. Always wrap your Task code inside a try/catch block.
Example:
getApi().getScheduler().schedule(() -> {
try
{
// Task logic here...
}
catch(Exception ex)
{
trace("An error occurred: ", ex.toString());
}
}, 2, TimeUnit.SECONDS);
-
Constructor Summary
ConstructorsConstructorDescriptionTaskScheduler(int threadPoolSize) Instead of creating a new instance use the API's scheduler -
Method Summary
Modifier and TypeMethodDescriptionvoidgetName()voidhandleMessage(Object params) voidSchedule a Task to be executed in the future, once.scheduleAtFixedRate(Runnable task, int initialDelay, int period, TimeUnit unit) Schedule Task to be executed periodically.void
-
Constructor Details
-
TaskScheduler
public TaskScheduler(int threadPoolSize) Instead of creating a new instance use the API's schedulergetApi().getScheduler();- Internal
-
-
Method Details
-
init
- Specified by:
initin interfacecom.smartfoxserver.bitswarm.service.IService- Internal
-
destroy
- Specified by:
destroyin interfacecom.smartfoxserver.bitswarm.service.IService- Internal
-
getName
- Specified by:
getNamein interfacecom.smartfoxserver.bitswarm.service.IService- Internal
-
handleMessage
- Specified by:
handleMessagein interfacecom.smartfoxserver.bitswarm.service.IService- Internal
-
setName
- Specified by:
setNamein interfacecom.smartfoxserver.bitswarm.service.IService- Internal
-
schedule
Schedule a Task to be executed in the future, once.- Parameters:
task- the task to be executeddelay- the amount of delay before the executionunit- the time scale- Returns:
- the
ScheduledFuturerepresenting the task - See Also:
-
scheduleAtFixedRate
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, int initialDelay, int period, TimeUnit unit) Schedule Task to be executed periodically.- Parameters:
task- the taskinitialDelay- the initial delayperiod- the interval at which the task should be executedunit- the time unit- Returns:
- the
ScheduledFuturerepresenting the task - See Also:
-