public class TaskScheduler
extends java.lang.Object
implements com.smartfoxserver.bitswarm.service.IService
In general it is recommended to use one TaskScheduler per application/Zone. You can easily maintain a Zone-based reference to the scheduler and access it not only from the Zone-level extension but also from any other Extension(s) attached to Rooms in the same Zone.
It's also advisable to avoid spawning very large thread pools (50+ threads) unless there are specific reasons for doing so. A setting of 1-4 threads is usually sufficient to handle any number of concurrent delayed events. The only exception to this is when the tasks to be executed are long-running such as accessing a database, a remote web-service or http server etc... Finally it is possible to resize the thread pool at runtime without affecting the execution of the Tasks. ( of course with exceptions: if the number of threads is reduced drastically when running lots of concurrent tasks this could actually have an impact on the execution )
NOTE: a runtime exception will stop the execution of the task. You should add your own try/catch block if you want to prevent this from happening, where appropriate.
Constructor and Description |
---|
TaskScheduler(int threadPoolSize) |
Modifier and Type | Method and Description |
---|---|
void |
destroy(java.lang.Object o)
Never accessed directly
|
java.lang.String |
getName()
Get the service name
|
int |
getThreadPoolSize()
Obtain the size of the thread pool for this Scheduler
|
void |
handleMessage(java.lang.Object arg0)
Not used in this service
|
void |
init(java.lang.Object o)
Never accessed directly
|
void |
resizeThreadPool(int threadPoolSize)
Resize the thread pool by changing the amount of worker threads executing the tasks.
|
java.util.concurrent.ScheduledFuture<?> |
schedule(java.lang.Runnable task,
int delay,
java.util.concurrent.TimeUnit unit)
Schedule a new task to be executed in the future, once.
|
java.util.concurrent.ScheduledFuture<?> |
scheduleAtFixedRate(java.lang.Runnable task,
int initialDelay,
int period,
java.util.concurrent.TimeUnit unit)
Schedule a new task to be executed periodically.
|
void |
setName(java.lang.String arg0)
Not used in this service
|
public TaskScheduler(int threadPoolSize)
threadPoolSize
- the size of thread pool handling the Taskspublic void init(java.lang.Object o)
init
in interface com.smartfoxserver.bitswarm.service.IService
o
- custom parameterspublic void destroy(java.lang.Object o)
destroy
in interface com.smartfoxserver.bitswarm.service.IService
o
- custom parameterspublic java.lang.String getName()
getName
in interface com.smartfoxserver.bitswarm.service.IService
public void handleMessage(java.lang.Object arg0)
handleMessage
in interface com.smartfoxserver.bitswarm.service.IService
arg0
- the messagepublic void setName(java.lang.String arg0)
setName
in interface com.smartfoxserver.bitswarm.service.IService
arg0
- the service namepublic java.util.concurrent.ScheduledFuture<?> schedule(java.lang.Runnable task, int delay, java.util.concurrent.TimeUnit unit)
task
- the task to be executeddelay
- the amount of delay before the executionunit
- the time unitTimeUnit
,
ScheduledFuture
public java.util.concurrent.ScheduledFuture<?> scheduleAtFixedRate(java.lang.Runnable task, int initialDelay, int period, java.util.concurrent.TimeUnit unit)
task
- the taskinitialDelay
- the initial delayperiod
- the interval at which the task should be executedunit
- the time unitTimeUnit
,
ScheduledFuture
public void resizeThreadPool(int threadPoolSize)
threadPoolSize
- public int getThreadPoolSize()