System.currentTimeMillis()的调用比new一个普通对象要耗时的多(具体耗时高出多少我还没测试过,有人说是100倍左右),然而该方法又是一个常用方法,有时不得不使用,比如日志,在高并发情形下怎么做才好呢?
System.currentTimeMillis()之所以慢是因为去跟系统打了一次交道,什么快?内存!如果该方法从内存直接取数,那岂不是相当快,看代码:
class MillisecondClock {
private long rate = 0;// 频率
private volatile long now = 0;// 当前时间
private MillisecondClock(long rate) {
this.rate = rate;
this.now = System.currentTimeMillis();
start();
}
private void start() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(rate);
} catch (InterruptedException e) {
e.printStackTrace();
}
now = System.currentTimeMillis();
}
}).start();
}
public long now() {
return now;
}
public static final MillisecondClock CLOCK = new MillisecondClock(10);
}
用的时候直接CLOCK.now()即可,你可以适当的进行变形,比如调整更新频率,开放构造函数,设置daemon线程,改为懒汉模式等等。
如下图是同时开启100个线程,每个线程并发请求一亿次的结果,可见耗时相差16倍左右(环境:JDK 1.7 win7 64位 4核 4G)。
- 大小: 253.2 KB
分享到:
相关推荐
long haomiao = System.currentTimeMillis(); int b = (int)((haomiao/1000)%26+65); char daxiezimu = (char)b; System.out.println(haomiao); System.out.println(daxiezimu); } }
主要介绍了System.currentTimeMillis()计算方式与时间的单位转换详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
高并发场景下System.currentTimeMillis()的性能问题的优化
下面小编就为大家带来一篇Java获取当前系统事件System.currentTimeMillis()方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
System.currentTimeMillis() SystemClock.uptimeMillis() SystemClock.elapsedRealtime() SystemClock.currentThreadTimeMillis 0、时间的单位: 秒:second 毫秒:Millisecond 微妙:Microsecond 纳秒:nanosecond ...
在网上看了一些for循环的效率问题,发现基本上都是一些理论的东西,且大多都是Copy来的,前后文自相矛盾。自己做了一个例子,来看看他们的效率 1.最慢 private long getTime1() { long time = 0; int count = 0;...
long lasttick = System.currentTimeMillis(); GB2312Helper gb2312Helper = GB2312Helper.singleton(); String info1 = "测试汉字拼音和五笔"; CharacterRecord recs1[] = gb2312Helper.getRecords(info1);...
long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( new FileInputStream(f) ); long contentLength = f.length(); ...
FakeTime使用本机Java代理将System.currentTimeMillis()实现替换为您可以使用系统属性进行控制的代理。 受启发。 public class ExamRegistrationServiceTest implements FakeTimeMixin { @Autowired ...
val start = System.currentTimeMillis() //logic val end = System.currentTimeMillis() val time = end - start println("[MTC] cost time:$time") } 本地调试模式下运行可以执行以下命令: ./gradlew :...
//加密 long lStart = System.currentTimeMillis(); String enString = AES.Encrypt(cSrc, cKey); System.out.println("加密后的字串是:" + enString); long lUseTime = System....
long begin=System.currentTimeMillis(); SortTest.class.getMethod(s, Number[].class).invoke(this, (Object)testNum); long end=System.currentTimeMillis(); System.out.println(s+":"+(end-begin)+...
System.out.println("开始导出Excel文件: "+System.currentTimeMillis()); XlsExport e = new XlsExport(); // header e.createRow(0); for (ExportFiled filed : ExportFiled.values()) { e.setCell...
// long begain = System.currentTimeMillis();//开始系统时间 // try { // Thread.sleep(100); // } catch (Exception e) { // e.printStackTrace(); // } // long CheckTime = System....
long start = System.currentTimeMillis(); try { // 打开word app = new ActiveXComponent("Word.Application"); // 获得word中所有打开的文档 Dispatch documents = app.getProperty("Documents")....
long start = System.currentTimeMillis(); ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application) try { app.setProperty("Visible", false); Dispatch ...
if(System.currentTimeMillis()-timeStamp > 5000){//如果时间间隔超过5秒 timeStamp = System.currentTimeMillis(); //重新记录时间 currPhoto = (currPhoto 1)%bmpPhotos.length; //更换照片 } canvas...
时间戳 这⾥时间戳的粒度是毫秒级,⽣成订单ID时,使⽤System.currentTimeMillis()作为时间戳。 1. 机器号 每个订单服务器都将被分配⼀个唯⼀的编号,⽣成订单ID时,直接使⽤该唯⼀编号作为机器号即可。 1. ⾃增...
item.setOdrId(System.currentTimeMillis()); Long result=saleOrderServiceImpl.save(item); System.out.println(result); session.put("item", item); return SUCCESS; } catch (Exception e) { e....