Why ScheduledThreadPoolExecutor for SmartFoxServer 2X

March 14th, 2011 by aabhaanjan

Generally In the game logic timers are required to be used in various situations. Instead of Timer SFS2X runs ScheduledThreadPoolExecutor’s instance wrapped in a class called TaskScheduler why ?

Lets discuss :

What is ScheduledThreadPoolExecutor ?

A public class ScheduledThreadPoolExecutor extends to ThreadPoolExecutor and implements ScheduledExecutorService

ThreadPoolExecutor is able to additionally schedule commands to run after a specific delay, or to execute periodically. It  is preferable when multiple worker threads are needed, or when the additional flexibility or capabilities f ThreadPoolExecutor are required.

In this case how the tasks executes ? :

Delayed tasks execute when they are enabled. Tasks scheduled for exactly the same execution time are enabled in first-in-first-out (FIFO) order of submission.

What are differences between Timers and ScheduledThreadPoolExecutor ?

1. Timer can be sensitive to changes in the system clock, ScheduledThreadPoolExecutor  is not.

2. Timer has only one execution thread, so long-running task can delay other tasks.

3. ScheduledThreadPoolExecutor can be configured with any number of threads.

4. ScheduledThreadExecutor not only catches runtime exceptions, but it lets you handle Task which threw exception will be canceled, but other tasks will continue to run.

6  ScheduledThreadExecutor isn’t available in Java 1.4 library