Server tasks: Timer v. SFS scheduler

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

Moderators: Lapo, Bax

User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Server tasks: Timer v. SFS scheduler

Postby Flying_Banana » 05 Jul 2015, 13:16

I was looking into a way to execute scheduled tasks. At first I thought about the native java Timer from java.util. However, some times later I found on this page that SFS recommends its own task scheduler.

I'm interested in knowing whether there is any significance difference between the two - are there any optimisation in the SFS scheduler; will that scale, etc.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Server tasks: Timer v. SFS scheduler

Postby Lapo » 06 Jul 2015, 06:56

Scheduler! :)
Timer is old and will not scale.

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Re: Server tasks: Timer v. SFS scheduler

Postby Flying_Banana » 13 Jul 2015, 11:01

I'm just wondering about how generous I should be dispatching new threads (timers).

For example, I can have a periodic thread called unit health regeneration that goes through all user controlled units every 10 seconds or so, and add health to them one by one. Or I can (preferably) have each unit create and manage its own scheduled future object when it gets damaged.

The former features a huge task with a lot of if-statements checking whether unit needs regeneration, the latter breaks that task down into fire-when-necessary bits, but I probably need to have about 100-500 threads running for it...
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Server tasks: Timer v. SFS scheduler

Postby Lapo » 13 Jul 2015, 12:15

Why so many threads?
Is the process blocking?
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Re: Server tasks: Timer v. SFS scheduler

Postby Flying_Banana » 13 Jul 2015, 18:13

Not at all! But a unit timer has to fire every ten seconds until that unit's health is full! I mean the thread won't be doing much but it still takes a thread, right?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Server tasks: Timer v. SFS scheduler

Postby Lapo » 13 Jul 2015, 19:54

Then I am not sure why you need hundreds of those?
The thread in the Scheduler's thread pool executes and returns immediately so it's ready for a new task in a few microseconds, even less than that.

Thread pools can execute a lot of work with just a few threads.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Flying_Banana
Posts: 42
Joined: 04 May 2014, 07:03

Re: Server tasks: Timer v. SFS scheduler

Postby Flying_Banana » 14 Jul 2015, 07:48

Ahh great! I was thinking about the thread might be blocking while waiting.

Just curious, what would be the maximum number of threads you would recommend, if none of them are blocking? I realise this is a very vague question that depends on server and tasks executed, but I want to have a rough estimate on a typical server.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Server tasks: Timer v. SFS scheduler

Postby Lapo » 14 Jul 2015, 08:01

With non blocking tasks (e.g no database or http calls) a setup with 3-6 threads is usually more than enough even for thousands of tasks.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
gabrielvpy
Posts: 69
Joined: 23 Jul 2015, 20:18

Re: Server tasks: Timer v. SFS scheduler

Postby gabrielvpy » 09 Sep 2015, 18:39

Excuse me. Can I ask how do I put a task in the Scheduler threadpool? I'm really lost here. I'm expanding the FPS demo and I need to implement a health regeneration every few seconds.
We made an awesome GPS - MOBA.
Try it out here: Get Fhacktions
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Server tasks: Timer v. SFS scheduler

Postby Lapo » 10 Sep 2015, 07:22

Look at the example here:
http://docs2x.smartfoxserver.com/Gettin ... wtos#item7

If there's anything unclear let us know
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 64 guests