当前位置:网站首页>解决高并发下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();
}
边栏推荐
- [combinatorics] Application of exponential generating function (multiple set arrangement problem | different balls in different boxes | derivation of exponential generating function of odd / even sequ
- How do you adjust the scope of activerecord Association in rails 3- How do you scope ActiveRecord associations in Rails 3?
- VS code配置虚拟环境
- Segmentation fault occurs during VFORK execution
- Source code analysis | resource loading resources
- Spark on yarn资源优化思路笔记
- Last update time of all sqlserver tables
- 用docker 连接mysql的过程
- Getting started | jetpack hilt dependency injection framework
- tensor中的append应该如何实现
猜你喜欢

Vs Code configure virtual environment

docker安装redis

Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
![[error record] the parameter 'can't have a value of' null 'because of its type, but the im](/img/1c/46d951e2d0193999f35f14d18a2de0.jpg)
[error record] the parameter 'can't have a value of' null 'because of its type, but the im

一文带你了解 ZigBee

VS 2019安装及配置opencv

Joking about Domain Driven Design (III) -- Dilemma

Spark on yarn资源优化思路笔记
![MySQL practice 45 lecture [row lock]](/img/71/344daddee537a96f0d38241e6896e1.png)
MySQL practice 45 lecture [row lock]
![MySQL practice 45 lecture [transaction isolation]](/img/a5/5420651d6be51e892976f02be8c43c.png)
MySQL practice 45 lecture [transaction isolation]
随机推荐
Force freeing memory in PHP
The solution of "the required function is not supported" in win10 remote desktop connection is to modify the Registry [easy to understand]
Are there any recommended term life insurance products? I want to buy a term life insurance.
VS 2019安装及配置opencv
Do you really understand relays?
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
VS code配置虚拟环境
About HTTP cache control
从输入URL到页面展示这中间发生了什么?
你真的懂继电器吗?
I2C 子系統(四):I2C debug
复选框的使用:全选,全不选,选一部分
Joking about Domain Driven Design (III) -- Dilemma
一文带你了解 ZigBee
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
3D drawing example
docker安装mysql
js根据树结构查找某个节点的下面的所有父节点或者子节点
C # general interface call
Idea format code idea set shortcut key format code