Package org.deftserver.io.callback

Source Code of org.deftserver.io.callback.PeriodicCallback

package org.deftserver.io.callback;

import org.deftserver.io.IOLoop;
import org.deftserver.io.timeout.Timeout;
import org.deftserver.web.AsyncCallback;

public class PeriodicCallback {
 
  private final AsyncCallback cb;
  private final long period;
  private boolean active = true;
 
  /**
   * A periodic callback that will execute its callback once every period.
   * @param cb
   * @param period The period in ms
   */
  public PeriodicCallback(AsyncCallback cb, long period) {
    this.cb = cb;
    this.period = period;
  }
 
  /**
   * Start the {@code PeriodicCallback}
   */
  public void start() {
    IOLoop.INSTANCE.addTimeout(
        new Timeout(
            System.currentTimeMillis() + period,
            new AsyncCallback() { @Override public void onCallback() { run(); }}
        )
    );
  }
 
  private void run() {
    if (active) {
      cb.onCallback();
      start()// reschedule
    }
  }
 
  /**
   * Cancel the {@code PeriodicCallback}. (No way to resume the cancellation, you will need to create a new
   * {@code PeriodicCallback}).
   */
  public void cancel() {
    this.active = false;
  }
 
}
TOP

Related Classes of org.deftserver.io.callback.PeriodicCallback

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.