注意了!System.currentTimeMillis() 存在性能问题...

简介: System.currentTimeMillis()是极其常用的基础Java API,广泛地用来获取时间戳或测量代码执行时长等,在我们的印象中应该快如闪电。但实际上在并发调用或者特别频繁调用它的情况下(比如一个业务繁忙的接口,或者吞吐量大的需要取得时间戳的流式程序),其性能表现会令人大跌眼镜。

作者:LittleMagic

链接:https://www.jianshu.com/p/d2039190b1cb


System.currentTimeMillis()是极其常用的基础Java API,广泛地用来获取时间戳或测量代码执行时长等,在我们的印象中应该快如闪电。


但实际上在并发调用或者特别频繁调用它的情况下(比如一个业务繁忙的接口,或者吞吐量大的需要取得时间戳的流式程序),其性能表现会令人大跌眼镜。


直接看下面的Demo。

public class CurrentTimeMillisPerfDemo {
    private static final int COUNT = 100;
    public static void main(String[] args) throws Exception {
        long beginTime = System.nanoTime();
        for (int i = 0; i < COUNT; i++) {
            System.currentTimeMillis();
        }
        long elapsedTime = System.nanoTime() - beginTime;
        System.out.println("100 System.currentTimeMillis() serial calls: " + elapsedTime + " ns");
        CountDownLatch startLatch = new CountDownLatch(1);
        CountDownLatch endLatch = new CountDownLatch(COUNT);
        for (int i = 0; i < COUNT; i++) {
            new Thread(() -> {
                try {
                    startLatch.await();
                    System.currentTimeMillis();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    endLatch.countDown();
                }
            }).start();
        }
        beginTime = System.nanoTime();
        startLatch.countDown();
        endLatch.await();
        elapsedTime = System.nanoTime() - beginTime;
        System.out.println("100 System.currentTimeMillis() parallel calls: " + elapsedTime + " ns");
    }
}

执行结果如下图。


image.png


可见,并发调用System.currentTimeMillis()一百次,耗费的时间是单线程调用一百次的250倍。


如果单线程的调用频次增加(比如达到每毫秒数次的地步),也会观察到类似的情况。


实际上在极端情况下,System.currentTimeMillis()的耗时甚至会比创建一个简单的对象实例还要多,看官可以自行将上面线程中的语句换成new HashMap<>之类的试试看。


为什么会这样呢?


来到HotSpot源码的hotspot/src/os/linux/vm/os_linux.cpp文件中,有一个javaTimeMillis()方法,这就是System.currentTimeMillis()的native实现。

jlong os::javaTimeMillis() {
  timeval time;
  int status = gettimeofday(&time, NULL);
  assert(status != -1, "linux error");
  return jlong(time.tv_sec) * 1000  +  jlong(time.tv_usec / 1000);
}

挖源码就到此为止,因为已经有国外大佬深入到了汇编的级别来探究,详情可以参见 The Slow currentTimeMillis() 这篇文章,我就不班门弄斧了。


http://pzemtsov.github.io/2017/07/23/the-slow-currenttimemillis.html


简单来讲就是:


调用gettimeofday()需要从用户态切换到内核态;

gettimeofday()的表现受Linux系统的计时器(时钟源)影响,在HPET计时器下性能尤其差;

系统只有一个全局时钟源,高并发或频繁访问会造成严重的争用。

HPET计时器性能较差的原因是会将所有对时间戳的请求串行执行。TSC计时器性能较好,因为有专用的寄存器来保存时间戳。缺点是可能不稳定,因为它是纯硬件的计时器,频率可变(与处理器的CLK信号有关)。关于HPET和TSC的细节可以参见:


https://en.wikipedia.org/wiki/High_Precision_Event_Timer\https://en.wikipedia.org/wiki/Time_Stamp_Counter


另外,可以用以下的命令查看和修改时钟源。

~ cat /sys/devices/system/clocksource/clocksource0/available_clocksource
tsc hpet acpi_pm
~ cat /sys/devices/system/clocksource/clocksource0/current_clocksource
tsc
~ echo 'hpet' > /sys/devices/system/clocksource/clocksource0/current_clocksource

如何解决这个问题?最常见的办法是用单个调度线程来按毫秒更新时间戳,相当于维护一个全局缓存。其他线程取时间戳时相当于从内存取,不会再造成时钟资源的争用,代价就是牺牲了一些精确度。具体代码如下。

public class CurrentTimeMillisClock {
    private volatile long now;
    private CurrentTimeMillisClock() {
        this.now = System.currentTimeMillis();
        scheduleTick();
    }
    private void scheduleTick() {
        new ScheduledThreadPoolExecutor(1, runnable -> {
            Thread thread = new Thread(runnable, "current-time-millis");
            thread.setDaemon(true);
            return thread;
        }).scheduleAtFixedRate(() -> {
            now = System.currentTimeMillis();
        }, 1, 1, TimeUnit.MILLISECONDS);
    }
    public long now() {
        return now;
    }
    public static CurrentTimeMillisClock getInstance() {
        return SingletonHolder.INSTANCE;
    }
    private static class SingletonHolder {
        private static final CurrentTimeMillisClock INSTANCE = new CurrentTimeMillisClock();
    }
}

使用的时候,直接CurrentTimeMillisClock.getInstance().now()就可以了。不过,在System.currentTimeMillis()的效率没有影响程序整体的效率时,就不必忙着做优化,这只是为极端情况准备的。

相关文章
“System.out.println(的正确格式
“System.out.println(的正确格式
93 0
使用System.out.println()
使用System.out.println()
48 0
|
小程序 Java Apache
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
193 0
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
|
缓存 Java Linux
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
166 0
|
JavaScript Dubbo 小程序
还在用 System.currentTimeMillis() 统计代码耗时?太 Low 啦
还在用 System.currentTimeMillis() 统计代码耗时?太 Low 啦
ZCMU - 1992: Swiss-system tournament
ZCMU - 1992: Swiss-system tournament
90 0
System类的常用方法(currentTimeMillis与arraycopy)
System类的常用方法 currentTimeMillis与arraycopy import java.util.Arrays; /* java.lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public st
|
Java Spring
别再用System.currentTimeMillis()!拥抱StopWatch优雅计算程序执行耗时
别再用System.currentTimeMillis()!拥抱StopWatch优雅计算程序执行耗时
128 0
|
Java 关系型数据库 Oracle
System.out.println
This Java tutorial is to explain what System.out.println is and how it works. It is love at first type.
1102 0
|
JavaScript Linux 前端开发

热门文章

最新文章