Saving ScheduledFuture Stops Code Execution

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Dydez
Posts: 2
Joined: 17 Sep 2016, 16:01

Saving ScheduledFuture Stops Code Execution

Postby Dydez » 17 Sep 2016, 16:11

Hi,

I'm trying to schedule at fixed rate a task for each of 9~ ai users. For some reason saving the ScheduledFuture object seems to stop the loop. When i run like below, the bolded trace does not run.

Code: Select all

   public void addAIDrafters(List<User> aiDrafterList){
      Integer x = 5;
      for (User aiDrafter : aiDrafterList){
         AIDraftPlayer aiDraftPlayer = new AIDraftPlayer(aiDrafter, this);
         ScheduledFuture<?> draftStartTimer = ts.scheduleAtFixedRate(aiDraftPlayer, x, 5, TimeUnit.SECONDS);
         aiDrafters.put(aiDrafter.getName(), draftStartTimer);
[b]         trace("WILL START IN "+x);[/b]
         x += 2;
      }
   }


If I stop saving the ScheduledFuture object, like below, the loop runs as expected. All tasks get scheduled, and execute at intervals. Unfortunately, this is not good enough, because i need to save the ScheduledFuture objects so I have a way of stopping them later.

Code: Select all

   public void addAIDrafters(List<User> aiDrafterList){
      Integer x = 5;
      for (User aiDrafter : aiDrafterList){
         AIDraftPlayer aiDraftPlayer = new AIDraftPlayer(aiDrafter, this);
         ts.scheduleAtFixedRate(aiDraftPlayer, x, 5, TimeUnit.SECONDS);
[b]         trace("WILL START IN "+x);[/b]
         x += 2;
      }
   }


My background is mostly javascript, so don't know much about threads. I'm guessing the problem is in a lack of understanding of Java basics. Any help would be appreciated. Thanks!!
User avatar
Lapo
Site Admin
Posts: 20216
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Saving ScheduledFuture Stops Code Execution

Postby Lapo » 19 Sep 2016, 09:37

By "saving" you mean storing a reference in a Map?

You can store the ScheduledFuture where you please, without side effects. The problem of your task stopping is to be searched in the code that it runs.

Keep in mind that any Exception thrown in the Runnable class will halt the execution if it's not caught. Typically this is the common cause of sudden task interruptions.

Make sure you're catching Extensions in your run() method as explained in the docs:
http://docs2x.smartfoxserver.com/Gettin ... wtos#item7

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Dydez
Posts: 2
Joined: 17 Sep 2016, 16:01

Re: Saving ScheduledFuture Stops Code Execution

Postby Dydez » 20 Sep 2016, 20:34

Thanks Lapo. I'm catching errors in the run method. I've pasted my runnable class code below.

Code: Select all

public class AIDraftPlayer implements Runnable {
   
   User user;
   RoomExtension roomExt;
   
   public AIDraftPlayer(User usr, RoomExtension ext){
      user = usr;
      roomExt = ext;
   }
   
   
   public void run(){
      try{         
         Logger logger = roomExt.getLogger();
         logger.debug(user.getName()+" is drafting");
         ISFSObject positionLimits = user.getVariable("draftPositionLimits").getSFSObjectValue();
         Set<String> positions = positionLimits.getKeys();
         String positionToDraft = null;
         for (String position : positions){
            if (positionLimits.getInt(position) > 0) {
               positionToDraft = position;
               break;
            }
         }
         SportsPlayer playerToDraft = getTopAtPosition(positionToDraft);
         logger.debug("drafting "+playerToDraft.getLastName());
         roomExt.handleClientRequest("draftPlayer", user, playerToDraft.getSFSObject());
      } catch (Exception e){
         e.printStackTrace();
      }
         
   }
User avatar
Lapo
Site Admin
Posts: 20216
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Saving ScheduledFuture Stops Code Execution

Postby Lapo » 21 Sep 2016, 08:09

If the problem still exist I would recommend logging some data while the task runs so you can make sure that they are being triggered.

There's no particular reason why a certain task should stop running, unless you run out of threads in the Scheduler. Even in that case the tasks will eventually trigger, the only problem is that they will do it later than expected.

This scenario however is possible only if you're starting hundreds or, more likely, thousands of tasks, which doesn't seem the case here.
Another reason for a similar behavior is that your tasks take a long time to complete (e.g. many seconds), at which point you may run of threads.

However whether you keep a reference to the ScheduledFuture or not is absolutely irrelevant to the completion of the task itself. The reference is useful because it allows you to keep track of all started tasks (especially those that repeat in loops) and you can always call the cancel() method on any of them if you decide not to run them.

hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 5 guests