Execute something on a certain instant

Discussion in 'Plugin Development' started by Sorrow, Mar 23, 2012.

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


    Hello everyone.
    I would like to know if there is a more "elegant" and less resource-eater way to execute a block of code when a pre-defined instant comes without checking if it has passed every playermove or any frequent event.
    I have thought about thread and scheduler, but I need it works with persistence, this means that if the server crashes, it will keep execute the code on that instant.

    Thanks in advance!
  2. Offline

    Father Of Time

    :eek:... What!?
  3. Offline


    For example:
    On 24/3/2012 at 00:00 I want to delete an object in a Set.
    On 24/3/2012 at 05:03 I want to delete another object in that Set.

    How can I do that without checking every tot seconds if the time has passed through the scheduler?
  4. Offline

    Father Of Time

    If I understand you correctly I would do it with a combination of time stamps and timers. make a timer that ticks once every minute, and when it ticks check to see if its passed certain times, if it is mark a false boolean in a hashset to true, if not leave it be. Then at the end of a day when every time event has been handled set the entire hashset back to false and start over.

    Sorry that this explanation is lacking, but I'm on my way out of the office, take care!
  5. Offline


    I mean, checking each minute if the time has passed is very resource demanding, right?
  6. Offline


    Sorrow, Not as much as checking on every move event...
  7. Offline


    So the best way is checking each minute, maybe store the expiration times in a TreeMap and checking only the first element.
    So, there are absolutely no way to execute the code in that instant only one time, without doing any check over time, right?
  8. Offline


    Try a thread that sleeps every x*1000 where x are the seconds. I think that's the best way of doing it.
    1. public class Timer extends Thread
    2. {
    3. private int s;
    4. public Timer(int seconds)
    5. {
    6. s = seconds;
    7. }
    8. public void run()
    9. {
    10. //your code
    11. try{sleep(s*1000);}catch(InterruptedException ie){}
    12. }
    13. }

    And to start the thread,
    1. Timer timer = new Timer(seconds);//provide your seconds ^_^
    2. timer.start();
  9. Offline


    Make sure you don't access the Bukkit API in your new Timer thread. If you want to access the Bukkit API it must be done through the Scheduler (the main thread is considered to be a part of the Scheduler). Also, if you are not experienced with multi-threading, i'd recommend you search on google for "java concurrency". Any object you reference from multiple threads must be "encased" in synchronization statements at all times. No exceptions :)
  10. Offline


    I've studied something about concurrency programming and I know the problems caused by multiple accesses to the same method/variable.
    Though, using thread has the inconvenience that the thread dies on an unexpected shutdown or either a restart, causing the loss of every time has passed since the thread was set to wait. So I think that it is impossible to get the time the thread has waited before the crash/shutdown and set the thread to wait again the remaining time...
    Is there a way to solve this?

    By the way, thanks a lot for the time you are spending helping me.
  11. Offline


    You might want to take a look here
Thread Status:
Not open for further replies.

Share This Page