public class CurrentTimeMillisUtil {
private volatile long now;
private static class SingletonHolder {
private static final CurrentTimeMillisUtil INSTANCE = new CurrentTimeMillisUtil();
}
public static CurrentTimeMillisUtil getInstance() {
return SingletonHolder.INSTANCE;
}
private CurrentTimeMillisUtil() {
this.now = System.currentTimeMillis();
scheduleTick();
}
private void scheduleTick() {
new ScheduledThreadPoolExecutor(1, runnable -> {
Thread thread = new Thread(runnable, "current-time-millis");
thread.setDaemon(true);
return thread;
}).scheduleAtFixedRate(() -> now = System.currentTimeMillis(), 1, 1, TimeUnit.MILLISECONDS);
}
public long now() {
return now;
}
}
Java 高并发下当前时间戳获取工具类(单线程维护)
最新推荐文章于 2023-04-11 16:31:38 发布