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 public class StaggeredRunnable implements Runnable{ private final Plugin myPlugin; private final List<String> hugeList; private final int taskId; private int iteratorCount = 0; private final int maxIterationsPerTick = 300; public StaggeredRunnable(Plugin myPlugin, List<String> hugeList) { this.myPlugin = myPlugin; this.hugeList = hugeList; } public void start() { // reset whenever we call this method iteratorCount = 0; long delay_before_starting = 10; long delay_between_restarting = 10; // synchronous - thread safe this.taskId = this.myPlugin.getServer().getScheduler().runTaskTimer(this.myPlugin, this, delay_before_starting, delay_between_restarting).getTaskId(); // asynchronous - NOT thread safe this.taskId = this.myPlugin.getServer().getScheduler().runTaskTimerAsynchronously(this.myPlugin, this, delay_before_starting, delay_between_restarting).getTaskId(); // Choose one or the other, not both. // They are both here simply for the sake of completion. } // this example will stagger parsing a huge list @Override public void run() { iteratorCount = 0; // while the list isnt empty, and we havent exceeded matIteraternsPerTick.... // the loop will stop when it reaches 300 iterations OR the list becomes empty // this ensures that the server will be happy clappy, not doing too much per tick. while (!this.hugeList.isEmpty() && iteratorCount < maxIterationsPerTick) { // do something with this huge list... // remove the first element (will always be present if !isEmpty) this.hugeList.remove(0); iteratorCount++; } // if our condition/result is met, cancel this task. // this can be anything, it is just cancelling this repeating task when we have met a condition we are looking for. if (hugeList.isEmpty()) { this.myPlugin.getServer().getScheduler().cancelTask(this.taskId); } } } To start this example, you would create a new instance of the class: Code:java StaggeredRunnable staggered = new StaggeredRunnable(myPlugin, myHugeList);staggered.start(); 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.
You set iteratorCount to 0 every time the run method is called, which means that it will always be 0.
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.
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