Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

java - System.currentTimeMillis() is not accurate on windows XP?

It seems System.currentTimeMillis is not very accurate.

See this sample:

public class MillisTime {
    public static void main(String[] args) {
        long start = 0;
        long end = 0;
        while (true) {
            if (start == 0) {
                start = System.currentTimeMillis();
            } else {
                long current = System.currentTimeMillis();
                if (current != start) {
                    end = current;
                    break;
                }
            }
        }
        System.out.println("The time interval of your OS: " + (end - start) + "ms");
    }
}

The result is (on Windows XP):

The time interval of your OS: 15ms

Why it's not 1ms? And how to get accurate millis second of current time?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is entirely expected. You'd see the same thing on .NET using DateTime.Now. (See Eric Lippert's blog post on the topic for a .NET-oriented view on this same topic.)

You can use System.nanoTime() to get a more accurate timer for measurements only - that's not meant to give an absolute time, it's only for measuring intervals.

I don't know of any way to get a more accurate absolute time, either from Java or from Win32. To be honest, how accurate is the system clock going to be anyway? Even with regular syncing with an NTP server I'd expect at least a few milliseconds inaccuracy.

Basically, if you're relying on getting an absolute time really accurately, you should probably change your design.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...