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

Viewed 60

The System.currentTimeMillis(); is system method in Java. If invoke this method serially, it seems that no performance issues. But if you keep invoking this method concurrently, the performance issue will occurred explicitly. As the native method dependent with OS clock_source. But how to improve it performance in Java. Refresh time milli policy with fixed rate is not usable.

Examples like below:


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


1 Answers
Related