mirror of https://github.com/longtai-cn/hippo4j
commit
5ca6e50647
@ -0,0 +1,53 @@
|
||||
package cn.hippo4j.core.toolkit;
|
||||
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/4/25 17:03
|
||||
* @description:
|
||||
*/
|
||||
public class SystemClock {
|
||||
|
||||
private final int period;
|
||||
|
||||
private final AtomicLong now;
|
||||
|
||||
private static final String THREAD_NAME ="system.clock";
|
||||
|
||||
private static class InstanceHolder {
|
||||
private static final SystemClock INSTANCE = new SystemClock(1);
|
||||
}
|
||||
|
||||
private SystemClock(int period) {
|
||||
this.period = period;
|
||||
this.now = new AtomicLong(System.currentTimeMillis());
|
||||
scheduleClockUpdating();
|
||||
}
|
||||
|
||||
private static SystemClock instance() {
|
||||
return InstanceHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private void scheduleClockUpdating() {
|
||||
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1, r -> {
|
||||
Thread thread = new Thread(r, THREAD_NAME);
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
scheduler.scheduleAtFixedRate(() -> now.set(System.currentTimeMillis()), period, period, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private long currentTimeMillis() {
|
||||
return now.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用来替换原来的System.currentTimeMillis()
|
||||
*/
|
||||
public static long now() {
|
||||
return instance().currentTimeMillis();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue