RocketMQ里的一个获取时间的工具类SystemClock
看rocketmq源码的时候发现他们还给时钟封装里一下。
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.rocketmq.common; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; /**
* @author vintage.wang
*/
public class SystemClock { private final long precision;
private final AtomicLong now; public SystemClock(long precision) {
this.precision = precision;
now = new AtomicLong(System.currentTimeMillis());
scheduleClockUpdating();
} private void scheduleClockUpdating() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, "System Clock");
thread.setDaemon(true);
return thread;
}
});
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
now.set(System.currentTimeMillis());
}
}, precision, precision, TimeUnit.MILLISECONDS);
} public long now() {
return now.get();
} public long precision() {
return precision;
}
}
// 优化获取时间性能,精度1ms
private final SystemClock systemClock = new SystemClock(1);
...
public SystemClock getSystemClock() {
return systemClock;
}
...
long beginTime = this.getSystemClock().now();
RocketMQ里的一个获取时间的工具类SystemClock的更多相关文章
- 工具类分享之获取Request/Response工具类《RequestContextHolderUtil》
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/aiyaya_/article/details/78975893前言在开发spring web项目时, ...
- Java中Date类型如何向前向后滚动时间,( 附工具类)
Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...
- Java日期时间实用工具类
Java日期时间实用工具类 1.Date (java.util.Date) Date(); 以当前时间构造一个Date对象 Date(long); 构造函数 ...
- [java工具类01]__构建格式化输出日期和时间的工具类
在之前的学习中,我写过一篇关于字符串格式化的,就主要设计到了时间以及日期的各种格式化显示的设置,其主要时通过String类的fomat()方法实现的. 我们可以通过使用不同的转换符来实现格式化显示不同 ...
- jdk8 时间日期工具类(转)
package com.changfu.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import jav ...
- Spring MVC模式下,获取WebApplicationContext的工具类 方法
在已有的注解类型下,获取WebApplicationContext的工具类 通过 WebApplicationContextUtils.getRequiredWebApplicationContex ...
- Eclipse里选中一个变量后,这个类里的该变量不变色了?
Eclipse里选一个变量后,这个类里的该变量不变色了. 1.使用“Alt+Shift+O”对该提示功能的开/关切换 2.可以在以下设置选中后的文本提示颜色 window--> Prefere ...
- android 获取手机信息工具类
package com.yqy.yqy_listviewheadview; import android.content.Context; import android.telephony.Telep ...
- 11、Java 日期时间 日期工具类
一.简介 在Java8之前,日期时间API一直被开发者诟病,包括:java.util.Date是可变类型,SimpleDateFormat非线程安全等问题.故此,Java8引入了一套全新的日期时间处理 ...
随机推荐
- AC日记——「SCOI2016」背单词 LiBreOJ 2012
#2012. 「SCOI2016」背单词 思路: Orz: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1 ...
- 关于在C#中对类中的隐藏基类方法和重写方法的理解
最近在学习C#,在C#中的类看到重写和隐藏基类的方法这些概念.才开始感觉自己不是很理解这些概念.也区分不开这些概念.通过自己的查找资料和练习后.慢慢的理解了类中的隐藏和重写这个概念.在C#中只有在基类 ...
- C++ cin.ignore()用法
cin.ignore(int a,char b); a为一行中最大读取字符长度,b为某一个字符.在缓冲区中寻找b,找到后忽略b以前的所有字符(包括b).如果在a的范围内还没有找到b,则忽略b以前的所有 ...
- Nmap误报1720端口开放的原因
在使用Nmap扫描服务器开放端口(全连接扫描)时,一直会发现误报1720端口开放,telnet也有时会连接成功.而实际上服务器并未开启此端口.经过查阅资料,确定原因如下: H.323协议在负载中放入了 ...
- SPOJ - PHRASES K - Relevant Phrases of Annihilation
K - Relevant Phrases of Annihilation 题目大意:给你 n 个串,问你最长的在每个字符串中出现两次且不重叠的子串的长度. 思路:二分长度,然后将height分块,看是 ...
- 出现Unrecognized field "state" (class com.jt.manage.pojo.ItemCat)异常
当在pojo中,往往会出现字段无法一一对应时,有可能就会出现创建Unrecognized field "state" (class com.jt.manage.pojo.ItemC ...
- react native 调用Android原生方法
来源:https://www.youtube.com/watch?v=WmJpHHmOKM8 教程:https://www.youtube.com/watch?v=GiUo88TGebs Breaki ...
- vue中的锚链接跳转问题
在vue中的锚链接和普通的html不同,关于vue中的锚链接可以参考vue 中的 scrollBehavior 滚动行为. 在router.js中 //创建 router 实例 const rout ...
- RHEL/CentOS网络相关的配置文件路径
/etc/sysconfig/network-scripts/ifcfg-ens33 //IP地址,子网掩码等配置文件 /etc/sysconfig/network-scripts/ifcfg-lo ...
- Number Sequence HDU - 5014
There is a special number sequence which has n+1 integers. For each number in sequence, we have two ...