当前位置:网站首页>解决高並發下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(); }
边栏推荐
- Creation and destruction of function stack frame
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
- Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
- Andwhere multiple or query ORM conditions in yii2
- How to implement append in tensor
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
- Can netstat still play like this?
- 迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
- How to return ordered keys after counter counts the quantity
- Spark on yarn资源优化思路笔记
猜你喜欢
Three.js本地环境搭建
Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
分布式事务
TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
Idea format code idea set shortcut key format code
Yiwen takes you to know ZigBee
el-tree搜索方法使用
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
Agile certification (professional scrum Master) simulation exercise-2
随机推荐
What does it mean when lambda is not entered?
Use optimization | points that can be optimized in recyclerview
C语言初阶-指针详解-庖丁解牛篇
基于QT的tensorRT加速的yolov5
Chart. JS multitooltip tag - chart js multiTooltip labels
[C语言]给账号密码进行MD5加密
Introduction to cron expression
How to limit the size of the dictionary- How to limit the size of a dictionary?
[leectode 2022.2.15] lucky numbers in the matrix
Idea format code idea set shortcut key format code
Can netstat still play like this?
二维数组中的元素求其存储地址
Do you really understand relays?
Notifydatasetchanged not applicable to recyclerview - notifydatasetchanged not working on recyclerview
I2C subsystem (I): I2C spec
Sous - système I2C (IV): débogage I2C
Model transformation onnx2engine
tensor中的append应该如何实现
JS finds all the parent nodes or child nodes under a node according to the tree structure
Cron表达式介绍