Page 1 of 1

How to pause a task?

Posted: 09 Feb 2017, 06:24
by verlout1

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,

Re: How to pause a task?

Posted: 09 Feb 2017, 06:31
by verlout1
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?

Re: How to pause a task?

Posted: 09 Feb 2017, 08:48
by Lapo
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

Re: How to pause a task?

Posted: 09 Feb 2017, 11:15
by verlout1
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

Re: How to pause a task?

Posted: 07 Nov 2019, 19:42
by Ber4ber43
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

Re: How to pause a task?

Posted: 08 Nov 2019, 08:01
by Lapo
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

Re: How to pause a task?

Posted: 08 Nov 2019, 14:08
by Ber4ber43
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?

Re: How to pause a task?

Posted: 09 Nov 2019, 07:42
by Lapo
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

Re: How to pause a task?

Posted: 09 Nov 2019, 14:10
by Ber4ber43
Thank you, you helped me a lot