org.jgroups.util.TimeService Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote Jakarta Enterprise Beans and Jakarta Messaging, including
all dependencies. It is intended for use by those not using maven, maven users should just import the Jakarta Enterprise Beans and
Jakarta Messaging BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
package org.jgroups.util;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**
* Provides a coarse grained time service. Registers a timer task which calls and caches {@link System#nanoTime()}
* and returns the cached value. This is way faster than calling {@link System#nanoTime()} many times, e.g.
* for each received message. The granularity (interval) can be chosen by the user.
* Note that use of values returned by {@link #timestamp()} needs to obey the same rules as for {@link System#nanoTime()}
* @author Bela Ban
* @since 3.5
*/
public class TimeService implements Runnable {
protected TimeScheduler timer;
protected volatile Future> task;
protected long interval=500; // ms
protected volatile long timestamp; // ns
public TimeService(final TimeScheduler timer) {
this(timer, 500);
}
public TimeService(final TimeScheduler timer, long interval) {
this.timer=timer;
this.interval=interval;
}
/**
* Returns the timestamp (ns)
* @return the result of the last call to {@link System#nanoTime()} (ns)
*/
public long timestamp() {
return timestamp > 0? timestamp : (timestamp=System.nanoTime());
}
public long interval() {
return interval;
}
public TimeService interval(long interval) {
if(interval != this.interval)
this.interval=interval;
return this;
}
public boolean running() {return task != null && !task.isDone();}
public TimeService start() {
startTask();
return this;
}
public TimeService stop() {
stopTask();
return this;
}
public void run() {
timestamp=System.nanoTime();
}
public String toString() {
return getClass().getSimpleName() + " (interval=" + interval + "ms)";
}
protected synchronized void startTask() {
stopTask();
task=timer != null? timer.scheduleWithFixedDelay(this, interval, interval, TimeUnit.MILLISECONDS) : null;
}
protected synchronized void stopTask() {
if(task != null) {
task.cancel(false);
task=null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy