当前位置:网站首页>解决高並發下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();
}
边栏推荐
- yii2 中andWhere多个or查询 orm条件
- Opengauss database development and debugging tool guide
- Source code analysis | resource loading resources
- Parameter index out of range (1 > number of parameters, which is 0)
- 销毁Session和清空指定的属性
- docker安装redis
- MySql实战45讲【索引】
- How do you adjust the scope of activerecord Association in rails 3- How do you scope ActiveRecord associations in Rails 3?
- Check log4j problems using stain analysis
- Change cell color in Excel using C - cell color changing in Excel using C
猜你喜欢

力扣------网格中的最小路径代价

Anhui University | small target tracking: large-scale data sets and baselines

Installation and use of memory leak tool VLD

Docker install redis

用docker 連接mysql的過程

MySql实战45讲【SQL查询和更新执行流程】

I2C subsystem (IV): I2C debug

3D drawing example

函数栈帧的创建与销毁

The idea cannot be loaded, and the market solution can be applied (pro test)
随机推荐
文件重命名
Getting started | jetpack hilt dependency injection framework
tensor中的append应该如何实现
The solution of "the required function is not supported" in win10 remote desktop connection is to modify the Registry [easy to understand]
Update and return document in mongodb - update and return document in mongodb
Spark on yarn资源优化思路笔记
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
Are there any recommended term life insurance products? I want to buy a term life insurance.
Add automatic model generation function to hade
Left connection, inner connection
I2C 子系统(一):I2C spec
MySQL Real combat 45 [SQL query and Update Execution Process]
I2C subsystem (I): I2C spec
The process of connecting MySQL with docker
Docker install redis
Source code analysis | layout file loading process
[error record] the parameter 'can't have a value of' null 'because of its type, but the im
基于QT的tensorRT加速的yolov5
Anhui University | small target tracking: large-scale data sets and baselines
力扣------网格中的最小路径代价