SFS TaskScheduler

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

dhruv0002
Posts: 1
Joined: 08 Jun 2021, 12:13

SFS TaskScheduler

Postby dhruv0002 » 08 Jun 2021, 13:01

While using "scheduleAtFixedRate" method in com.smartfoxserver.v2.util.TaskScheduler, my scheduled tasks are getting overlapped as my first task is taking more than 2 seconds to complete and in the mean time my second task is getting started. The period argument in above mentioned method is set to "2 seconds" which executes the second task 2 seconds after first task is triggered no matter the first task is completed or not.

To prevent overlapping of tasks, fixed delay scheduler could help but com.smartfoxserver.v2.util.TaskScheduler class has a method only for fixed rate scheduler with name "scheduleAtFixedRate". Kindly help me by suggesting a possible solution to my problem which is compatible with SFS2X.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFS TaskScheduler

Postby Lapo » 08 Jun 2021, 13:48

Hello,
dhruv0002 wrote:While using "scheduleAtFixedRate" method in com.smartfoxserver.v2.util.TaskScheduler, my scheduled tasks are getting overlapped as my first task is taking more than 2 seconds to complete and in the mean time my second task is getting started. The period argument in above mentioned method is set to "2 seconds" which executes the second task 2 seconds after first task is triggered no matter the first task is completed or not.

This is the expected behavior.
A task that is set to run after two seconds will be run exactly after two seconds. The scheduler is not supposed to run tasks in sequence, waiting for one task to complete before starting a new one.
It is essentially just a thread pool that runs tasks independently at the specified interval.

If you need to run tasks in sequence you have to schedule the first task and at the very end of that Task (in your Runnable class), schedule the next task that is supposed to be executed after the current one.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 39 guests