How to pause a task?

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

Moderators: Lapo, Bax

verlout1
Posts: 5
Joined: 10 Jan 2017, 11:14

How to pause a task?

Postby verlout1 » 09 Feb 2017, 06:24

Code: Select all

ScheduledFuture<?> taskHandle = sfs.getTaskScheduler().scheduleAtFixedRate(new Tasker(), 0, 1,TimeUnit.SECONDS);

How to pause "taskHandle" when Client request pause game?
Until the client requests to continue the "taskHandle" again.
:D
Thanks,
verlout1
Posts: 5
Joined: 10 Jan 2017, 11:14

Re: How to pause a task?

Postby verlout1 » 09 Feb 2017, 06:31

That's how I do it.

Code: Select all

boolean IfPauseCountdown = false;
   private class Tasker implements Runnable {
      private int runningCycles = 0;
      public void run() {
         if (IfPauseCountdown)
            return;
         
         ++runningCycles;
         if (runningCycles >= 30) {
            runningCycles = 0;
            taskHandle.cancel(true);
         }
      }
   }

Is there a better way?
User avatar
Lapo
Site Admin
Posts: 21122
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to pause a task?

Postby Lapo » 09 Feb 2017, 08:48

Yes, that's pretty much the way to do it.
You can use a "pause" flag and if it's set to true you just return immediately.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
verlout1
Posts: 5
Joined: 10 Jan 2017, 11:14

Re: How to pause a task?

Postby verlout1 » 09 Feb 2017, 11:15

Lapo wrote:Yes, that's pretty much the way to do it.
You can use a "pause" flag and if it's set to true you just return immediately.

Cheers

:D Thanks
Ber4ber43
Posts: 25
Joined: 30 Apr 2019, 14:35

Re: How to pause a task?

Postby Ber4ber43 » 07 Nov 2019, 19:42

Hi,
The best practice is a boolean variable, or restart the Scheduler?
In a turn-based game, is it worth having a Scheduler for each game, or creating a global Scheduler?

Thank's
User avatar
Lapo
Site Admin
Posts: 21122
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to pause a task?

Postby Lapo » 08 Nov 2019, 08:01

Hi,
It's best to use the least amount of Schedulers. One scheduler per game is typically overkill when you have 1000s of games running. Rather you can use one Scheduler with enough threads to deal with the tasks. If the tasks are not CPU intensive or have to wait for long I/O operations, you normally need just a few of them. 10-20 threads can usually deal with 1000s of tasks, but it might be different if tasks are doing heavy calculations or waiting for something slow (e.g. a database, http calls etc.)

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
Ber4ber43
Posts: 25
Joined: 30 Apr 2019, 14:35

Re: How to pause a task?

Postby Ber4ber43 » 08 Nov 2019, 14:08

I set 10 inside Server Configurator -> General -> Server task scheduler thread pool size

I have to set 10 inside Server Configurator -> Tread pools -> Core threads?

To use the same Schedular throughout the game, do you create the Scheduler in a class and reference it?
If I call many times "SmartFoxServer.getInstance (). GetTaskScheduler (). Schedule (MyClass.GlobalTaskRunner, seconds, TimeUnit.SECONDS);"
each call has its countdown or it's overwritten?
User avatar
Lapo
Site Admin
Posts: 21122
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to pause a task?

Postby Lapo » 09 Nov 2019, 07:42

Hi,
there's one global Scheduler that you can access from your Extensions using:

Code: Select all

TaskScheduler ts = getApi().getSystemScheduler();

and you can submit your tasks to it.
In order to configure the number of threads you can either use the AdminTool ( Server Configurator -> General -> Server task scheduler thread pool size )
or you can change it at runtime using:

Code: Select all

ts.resizeThreadPool(numOfThreads);


Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
Ber4ber43
Posts: 25
Joined: 30 Apr 2019, 14:35

Re: How to pause a task?

Postby Ber4ber43 » 09 Nov 2019, 14:10

Thank you, you helped me a lot

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 17 guests