当前位置:网站首页>解决高并发下System.currentTimeMillis卡顿
解决高并发下System.currentTimeMillis卡顿
2022-07-03 03:08:00 【&友情岁月&】
public class SystemClock {
private final int period;
private final AtomicLong now;
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() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread thread = new Thread(runnable, "System Clock");
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();
}
边栏推荐
- C语言初阶-指针详解-庖丁解牛篇
- 模糊查詢時報錯Parameter index out of range (1 > number of parameters, which is 0)
- BigVision代码
- SqlServer行转列PIVOT
- Force freeing memory in PHP
- Update and return document in mongodb - update and return document in mongodb
- Spark on yarn资源优化思路笔记
- [Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
- Find the storage address of the elements in the two-dimensional array
- Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
猜你喜欢
随机推荐
一文带你了解 ZigBee
TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
el-tree搜索方法使用
Find the storage address of the elements in the two-dimensional array
The difference between componentscan and componentscans
Pytest (6) -fixture (Firmware)
Kubernetes cluster log and efk architecture log scheme
What does it mean when lambda is not entered?
Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
Source code analysis | layout file loading process
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Can netstat still play like this?
45 lectures on MySQL [index]
I2C 子系统(一):I2C spec
MySql实战45讲【事务隔离】
Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
L'index des paramètres d'erreur est sorti de la plage pour les requêtes floues (1 > Nombre de paramètres, qui est 0)
Sous - système I2C (IV): débogage I2C
I2C 子系統(四):I2C debug









