Question on Timers

  • So I was trying out some things with Timers for an update to one of my plugins and I was wondering if there is a way to pause a timer from within itself via an if loop.


    I tried it and I got a ConcurrentModificationException so was wondering if there is a very specific way I need to do it to avoid that.


    I tried making the timer a field and using a second method called inside the timer to pause it but got the exception again.


    What I have now and works is to save the ID of the timer as a field and then use the Timer.killTimer(timerID) method, then create the timer again after I am done with whatever I wanted to do while it was stopped.

  • Hi!


    I hope I understand your intention right.
    For my understanding a timer in Java is always "fire and forget". For me it doesn't matter if the Timer in RW-API has a pause()-function.


    You have to manage a pause mechanic on yourself.


    Because coding is fun, I just made a timer test.
    The source code is not rich in helpful comments :D , but I think you get the idea with the pause flag.
    Use /timer start , /timer pause , /timer unpause


    I hope this is in someway helpful?

  • hmmm not exactly, what I want is for the timer itself to change the boolean value not a command or any other function.


    For example I have a timer that is running and every 5 seconds it checks if you are holding 2 apples, I want the timer to be paused the moment it finds you holding 2 apples. i.e. my timer.pause() should be inside the timer runnable task.


    Then the problem is that a runnable task can only access final, or effectively final variables from outside thus cannot change their values :/

  • Hmmm...


    Perhaps an event-driven solution, perhaps the UpdateEvent?


    Something like this:

  • I thought of that but was too much trouble for something really small and unimportant :/


    Thus ended up with my awkward current solution of killing the timer via ID number :D

  • Sorry for the late response :|


    I'm glad to hear you solved the issue, however, basically you can stop the timer from within the runnable. If you need a reference to the timer object, you could create the runnable separately (initialize the timer with "null" runnable/task), for example:

    Timer handling should be thread-safe (so it's safe to call the kill() or start() method from another thread). Where did you get the ConcurrentModificationException exactly? =O


    Kill the timer only if you don't want to use it anymore (so all related resources will be released). If you just want to pause the timer temporarily, you can use the "pause()" method (however, unfortunately the pause() method doesn't work atm due to a bug, this will be fixed in the next update) :saint:

    Edited once, last by red51: pause() should actually work now ().

  • i made my Timer a field and then initialised it in a method and inside its runnable i used the .pause() method. When my if loop triggered and the .pause() method was invoked i got the exception


    Maybe the exception is related to the fact that the .pause() method is npt working properly atm? :/

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!