[HOWTO] Staggered Tasks

Discussion in 'Resources' started by jayfella, Feb 15, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    jayfella

    Processing a large task, or cancelling a repeating can be a bit of a head scratcher the first time you come across needing it, so here is a short but to the point example of how to go about doing it.

    The code below simply continues repeating the task until a condition is met:

    Code:java
    1.  
    2. public class StaggeredRunnable implements Runnable
    3. {
    4. private final Plugin myPlugin;
    5. private final List<String> hugeList;
    6.  
    7. private final int taskId;
    8.  
    9. private int iteratorCount = 0;
    10. private final int maxIterationsPerTick = 300;
    11.  
    12. public StaggeredRunnable(Plugin myPlugin, List<String> hugeList)
    13. {
    14. this.myPlugin = myPlugin;
    15. this.hugeList = hugeList;
    16. }
    17.  
    18. public void start()
    19. {
    20. // reset whenever we call this method
    21. iteratorCount = 0;
    22.  
    23. long delay_before_starting = 10;
    24. long delay_between_restarting = 10;
    25.  
    26. // synchronous - thread safe
    27. this.taskId = this.myPlugin.getServer().getScheduler().runTaskTimer(this.myPlugin, this, delay_before_starting, delay_between_restarting).getTaskId();
    28.  
    29. // asynchronous - NOT thread safe
    30. this.taskId = this.myPlugin.getServer().getScheduler().runTaskTimerAsynchronously(this.myPlugin, this, delay_before_starting, delay_between_restarting).getTaskId();
    31.  
    32. // Choose one or the other, not both.
    33. // They are both here simply for the sake of completion.
    34. }
    35.  
    36. // this example will stagger parsing a huge list
    37.  
    38. @Override
    39. public void run()
    40. {
    41. iteratorCount = 0;
    42.  
    43. // while the list isnt empty, and we havent exceeded matIteraternsPerTick....
    44. // the loop will stop when it reaches 300 iterations OR the list becomes empty
    45. // this ensures that the server will be happy clappy, not doing too much per tick.
    46.  
    47.  
    48. while (!this.hugeList.isEmpty() && iteratorCount < maxIterationsPerTick)
    49. {
    50. // do something with this huge list...
    51.  
    52.  
    53. // remove the first element (will always be present if !isEmpty)
    54. this.hugeList.remove(0);
    55.  
    56. iteratorCount++;
    57. }
    58.  
    59. // if our condition/result is met, cancel this task.
    60. // this can be anything, it is just cancelling this repeating task when we have met a condition we are looking for.
    61. if (hugeList.isEmpty())
    62. {
    63. this.myPlugin.getServer().getScheduler().cancelTask(this.taskId);
    64. }
    65.  
    66. }
    67.  
    68. }
    69.  


    To start this example, you would create a new instance of the class:
    Code:java
    1.  
    2. StaggeredRunnable staggered = new StaggeredRunnable(myPlugin, myHugeList);
    3. staggered.start();
    4.  


    The repeating task is started by executing the method start() - and as a result can store the taskId given by the scheduler locally inside itself. It keeps things neat and tidy. When the condition to stop has been met, it can then stop the task using the stored ID number.
     
  2. Offline

    TnT

    Moved to resources.
     
  3. You set iteratorCount to 0 every time the run method is called, which means that it will always be 0.
     
  4. Offline

    jayfella

    it will always be zero when it starts, yes, as it should be, because its a new "stagger". The while loop increments it, and stops if the list is empty or the iterator counter hits 300. This instance of the stagger is then done, and it starts all over again when the scheduler starts it, until eventually its complete.
     
  5. Oh, that way:p Get it now.
    That comment made me sound like a noob xD
     
  6. Offline

    jayfella

    Believe me, i've been there lol.
     
  7. Offline

    Double0negative

    jayfella

    This is also called time slicing. It is very useful in block rollback plugins such as log block and my rollback engine in SG
     
Thread Status:
Not open for further replies.

Share This Page