当前位置:网站首页>解决高並發下System.currentTimeMillis卡頓
解决高並發下System.currentTimeMillis卡頓
2022-07-03 03:11: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();
}
边栏推荐
- docker安装mysql
- The core idea of performance optimization, dry goods sharing
- Process the dataset and use labelencoder to convert all IDs to start from 0
- MySql實戰45講【SQL查詢和更新執行流程】
- Opengauss database development and debugging tool guide
- I2C 子系統(四):I2C debug
- Docker install MySQL
- I2C 子系统(二):I3C spec
- Can I use read-only to automatically implement properties- Is read-only auto-implemented property possible?
- VS 2019 配置tensorRT生成engine
猜你喜欢

Idea format code idea set shortcut key format code

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

I2C subsystem (IV): I2C debug

Yiwen takes you to know ZigBee

Force deduction ----- the minimum path cost in the grid

I2C 子系统(一):I2C spec

Opengauss database development and debugging tool guide

敏捷认证(Professional Scrum Master)模拟练习题-2

Le processus de connexion mysql avec docker
![MySQL practice 45 lecture [row lock]](/img/71/344daddee537a96f0d38241e6896e1.png)
MySQL practice 45 lecture [row lock]
随机推荐
MySQL practice 45 lecture [transaction isolation]
二维数组中的元素求其存储地址
一文带你了解 ZigBee
力扣------网格中的最小路径代价
I2C 子系统(四):I2C debug
Creation and destruction of function stack frame
Use optimization | points that can be optimized in recyclerview
Super easy to use logzero
C语言初阶-指针详解-庖丁解牛篇
tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
MySql实战45讲【事务隔离】
分布式事务
45 lectures on MySQL [index]
Chart. JS multitooltip tag - chart js multiTooltip labels
C#通用接口调用
从输入URL到页面展示这中间发生了什么?
Left connection, inner connection
VS code配置虚拟环境
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"