Page 1 of 1

SFSApi - getNewScheduler

Posted: 02 Oct 2019, 04:31
by tobaonghiep
Hi Lapo, Bax

Currently I just discovered in SFSApi got expose this function

Code: Select all

TaskScheduler	getNewScheduler(int threadPoolSize)
which is I could get from MyExtension class.
I would like to ask some questions:
1. What is the difference between SFSApi.getNewSchedule(int poolSize), SFSApi.getSystemScheduler() and SmartFoxServer.getInstance().getTaskScheduler()?
2. When should I use them, in what cases?
3. And suppose my server have multiple room extension (ex: 1000 rooms) which is will be created when the users in and deleted when empty. Each room will handle different logic. What is the best practice for SFSApi.getNewSchedule(int poolSize), esp the poolSize number as I worry that the global Scheduler will not enough or too busy if all rooms point to use it?

ps: I got an ideal that if I just wanna do something concurrently and no need to wait for them I could use TaskScheduler with the delayTime=0 (ex: storing result scores to database) and the extThread will be release faster. is it a good practice?

Thankyou.

Re: SFSApi - getNewScheduler

Posted: 02 Oct 2019, 07:37
by Lapo
Hello,
1. What is the difference between SFSApi.getNewSchedule(int poolSize), SFSApi.getSystemScheduler() and SmartFoxServer.getInstance().getTaskScheduler()?
SmartFoxServer 2X runs a global Task Scheduler whose thread pool can be configured via AdminTool > ServerConfigurator.
You can use this to run your own delayed tasks. Alternatively you can create one or more Schedulers inside your Extension via the getNewScheduler() call.

SFSApi.getSystemScheduler() and SmartFoxServer.getInstance().getTaskScheduler() are equivalent, they both give you back a reference to the global Scheduler.
When should I use them, in what cases?
It's easier to just use the global Scheduler, so I'd say 95% of the cases use that. And if you need to fine tune the Scheduler's threads, you can reconfigure it as mentioned.
Custom schedulers can be used for more advanced cases, maybe when you're running I/O dependent tasks.
3. And suppose my server have multiple room extension (ex: 1000 rooms) which is will be created when the users in and deleted when empty. Each room will handle different logic. What is the best practice for SFSApi.getNewSchedule(int poolSize), esp the poolSize number as I worry that the global Scheduler will not enough or too busy if all rooms point to use it?
There is a very little difference between 1 Task Scheduler running 100 threads, and 50 Task Schedulers running 2 threads. What is important is essentially the size of the thread pool compared to the volume of tasks that need to be executed.
Unless your tasks are particularly CPU intensive or slow due to external I/O you should not worry, a thread pool with 4-5 threads can handle 1000s of tasks.

Usually all it takes is a bit of testing and tinkering.
Cheers

Re: SFSApi - getNewScheduler

Posted: 02 Oct 2019, 09:15
by balamost
Hi Lapo!

I have a question also.
What is the difference between AdminTool > ServerConfigurator > General > Server task scheduler thread pool size and AdminTool > ServerConfigurator > Thread pools > System thread pool ?

Thanks!

Re: SFSApi - getNewScheduler

Posted: 02 Oct 2019, 09:46
by Lapo
The first one configures the global Scheduler, which is used to run tasks that run in the future either once or repeatedly.
The second one controls the server's internal thread pools. These usually don't need to be changed in most cases. If you want to learn more about these thread pools read here:
http://docs2x.smartfoxserver.com/Extens ... d-concepts

Hope it helps