Question on timing.

Discussion in 'Plugin Development' started by darknesschaos, Jan 18, 2011.

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


    How would I set a particular piece of code to run in a plugin at a certain point in time? or how about something like a timer?

    An example would be like,
    You use a command. The command sends a message to a player and/or does something in say x (mili)seconds.

  2. Offline


    Bukkit (to my knowledge) doesn't support this in its API. But Java does.

    public static void waiting (int n){
            long t0, t1;
            t0 =  System.currentTimeMillis();
                 //what to do here
                t1 = System.currentTimeMillis();
            while ((t1 - t0) < (n * 1000));
  3. Offline


    Thanks for the fast reply, I shall try it out.
  4. Offline


    That isn't a good way to do it, your code will freeze the main thread for that period of time and so the server will not be able to issue new commands until your delay has timed out.

    What you need is a thread to sleep for n seconds and then issue the command. However, as you say the API doesn't support it. There is no way for other threads to communicate back to the main thread.
  5. Offline


    so, there is no way to do this yet?
  6. Offline


    Yeah. Sorry Dark, I probably should've mentioned what Raph just said. It is the only way to do it right now, sorry.
  7. Offline


    heh, its ok. I just gotta program a little smarter then.
  8. Offline


    If a event "onTimeChange(interval)" is ever released then I'd recommended hooking it to that.
  9. Offline


    One option is to piggyback on some other hook and have that check if there is anything in the queue (you could used a "LinkedBlockQueue<Runnable> queue" to create a queue, or a Block).

    For example, every time a player moves you could check if the timer has expired. The more hooks you check from the quicker it will detect the change. However, the more checks, the more server load.

    LinkedBlockingQueue queue = new LinkedBlockingQueue();
    void addToQueue(Runnable r) {
    void pollQueue() {
        Runnable r;
        do {
            r = queue.poll();
            if( r != null ) {
        } while ( r != null );
    You need to call pollQueue() from some hook.

    To add code to the queue use

    final SomeClass finalVariableName = variableName;
    addToQueue( new Runnable {
        public run() {
            <do something>;
    } );
    You need to use the final keyword and make copies/clones of any variables you want to pass to the Runnable.
  10. Offline


    Use the java.util.Timer class:

    // Setup a timer so that the update method gets called (and call it)
    updateTimer = new Timer();
    updateTimer.schedule(new KikkitUpdater(this), 0, UPDATE_INTERVAL);
    Taken from:
    And the updater class:

    More info:
  11. Offline


    I'm 90% sure that is also risky. He is calling the API functions from the timer thread.
  12. Offline


    Good point. I'll have to change it.

    Is there a mechanism in Java similar to the lock keyword in C# for thread safe access?
  13. Offline


    You can use synchronized blocks.

    int x;
    Object xSync = new Object[]
    To update the x from another thread:

    synchronized(xSync) {
    Effectively, xSync acts as a mutex.
  14. Offline


    Works just like lock! Nice.
  15. Offline


    Also, for non-primative types, you can use the variable itself as the sync object.

    However, be careful.

    Integer x = 7;
    <do stuff>
    synchronized(x) {
    is not allowed.

    x = y causes variable x to change reference so that it points at the same object as y. This means that x is no longer synchronized properly.
Thread Status:
Not open for further replies.

Share This Page