mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 18:03:12 +01:00
Use global timer
This commit is contained in:
parent
bdc15f113e
commit
47ed630d4d
65
common/src/main/java/io/bitsquare/common/FrameRateTimer.java
Normal file
65
common/src/main/java/io/bitsquare/common/FrameRateTimer.java
Normal file
@ -0,0 +1,65 @@
|
||||
package io.bitsquare.common;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* We simulate a global frame rate timer similar to FXTimer to avoid creation of threads for each timer call.
|
||||
* Used only in headless apps like the seed node.
|
||||
*/
|
||||
public class FrameRateTimer implements Timer, Runnable {
|
||||
private final Logger log = LoggerFactory.getLogger(FrameRateTimer.class);
|
||||
|
||||
private long interval;
|
||||
private Runnable runnable;
|
||||
private long startTs;
|
||||
private boolean isPeriodically;
|
||||
|
||||
public FrameRateTimer() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
if ((currentTimeMillis - startTs) >= interval) {
|
||||
runnable.run();
|
||||
if (isPeriodically)
|
||||
startTs = currentTimeMillis;
|
||||
else
|
||||
stop();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
log.error(t.getMessage());
|
||||
t.printStackTrace();
|
||||
stop();
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timer runLater(Duration delay, Runnable runnable) {
|
||||
this.interval = delay.toMillis();
|
||||
this.runnable = runnable;
|
||||
startTs = System.currentTimeMillis();
|
||||
MasterTimer.addListener(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timer runPeriodically(Duration interval, Runnable runnable) {
|
||||
this.interval = interval.toMillis();
|
||||
isPeriodically = true;
|
||||
this.runnable = runnable;
|
||||
startTs = System.currentTimeMillis();
|
||||
MasterTimer.addListener(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
MasterTimer.removeListener(this);
|
||||
}
|
||||
}
|
35
common/src/main/java/io/bitsquare/common/MasterTimer.java
Normal file
35
common/src/main/java/io/bitsquare/common/MasterTimer.java
Normal file
@ -0,0 +1,35 @@
|
||||
package io.bitsquare.common;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class MasterTimer {
|
||||
private final static Logger log = LoggerFactory.getLogger(MasterTimer.class);
|
||||
private static final java.util.Timer timer = new java.util.Timer();
|
||||
public static long FRAME_INTERVAL_MS = 16;
|
||||
|
||||
static {
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
listeners.stream().forEach(UserThread::execute);
|
||||
}
|
||||
}, FRAME_INTERVAL_MS, FRAME_INTERVAL_MS); // frame rate of 60 fps is about 16 ms
|
||||
}
|
||||
|
||||
private static Set<Runnable> listeners = new CopyOnWriteArraySet<>();
|
||||
|
||||
public static void addListener(Runnable runnable) {
|
||||
listeners.add(runnable);
|
||||
}
|
||||
|
||||
public static void removeListener(Runnable runnable) {
|
||||
listeners.remove(runnable);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user