繁体   English   中英

如何提高 System.currentTimeMillis(); 的性能?

[英]How to improve the performance for System.currentTimeMillis();?

System.currentTimeMillis(); 是Java中的系统方法。 如果串行调用此方法,似乎没有性能问题。 但是如果你不断地同时调用这个方法,性能问题就会显式地出现。 作为依赖于 OS 时钟源的本机方法。 但是如何在 Java 中提高它的性能。 固定速率的刷新时间毫秒策略不可用。

示例如下:


int parallism = 32;

for(int i=0;i< parallism ;i++){
    new Thread(() -> {
        for(;;){

            // Focus here, how can i measure the logic efficiently
            long begin = System.currentTimeMillis();
            
            // Here may be the logic: 
            // Define empty block here means 0ms elapsed

            long elapsed = (System.currentTimeMillis() - begin);
            if(elapsed >= 5){
                System.err.println("Elapsed: "+elapsed+" ms.");
            }
        }
    }).start();
}

Thread.sleep(Integer.MAX_VALUE); // Just avoid process exit


性能低的原因: https://pzemtsov.github.io/2017/07/23/the-slow-currenttimemillis.ZFC35FDC70D5FC69D269883A82EZ5A

(无法使用)另一种解决方案: https://programmer.group/5e85bd0cc8b52.html

等我发布我的解决方案....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM