bill0ute
bill0ute

Reputation: 369

Wrong Time with System.currentTimeMillis () (Java)

I made a little program to test the System.currentTimeMillis (). And I have a strange result. This is my logs :

1    26-12-09 20:48:21 - [Log] lTime = 1261860501009
2    26-12-09 20:48:21 - [Log] lTime = 1261860501012
3    26-12-09 20:48:21 - [Log] lTime = 1261864899078
4    26-12-09 20:48:21 - [Log] lTime = 1261860501033
5    26-12-09 20:48:21 - [Log] lTime = 1261860501069

As you can see, there is a problem on line 3. The time millis is wrong. It should be between 1261860501012 and 1261860501033. There is an error of, roughly, 73 milli seconds.

Somebody knows where the problem come from ?

Thanks a lot

bill0ute

Edit : OS : Debian 4.0, Java : 6_17.

My code :

while (true) 
    setLog (System.currentTimeMillis ());

Edit : The program run on a Linux based VPS

Upvotes: 10

Views: 17156

Answers (4)

Steve Emmerson
Steve Emmerson

Reputation: 7832

The method in question depends on the system clock and system clocks can have problems. See http://support.ntp.org/bin/view/Support/KnownOsIssues for a discussion of issues keeping the system clock accurate via the ntpd(8) daemon.

I also recommend http://www.vmware.com/pdf/vmware_timekeeping.pdf for a discussion on the accuracy of the system clock in VMWare. It also has as excellent discussion on system clocks in general.

Upvotes: 0

Pool
Pool

Reputation: 12342

System.currentTimeMillis() is dependent on System clock. It looks like the system clock has been micro-corrected by an external programme, for Linux that's probably NTP.

Note you shouldn't use System.currentTimeMillis() to measure elapsed time. It's better to use System.nanoTime() but even that isn't guaranteed to be monotonic.

Upvotes: 7

Paul Wagland
Paul Wagland

Reputation: 29096

We once saw a similar thing, running on Ubuntu with AMD64x2 chips. If you also have the chip that is where I would start looking.

Upvotes: 0

moritz
moritz

Reputation: 2478

First of all, you have a little typo, it's 73 milliseconds, not seconds ( would be disturbing then :-) ).

To get to the point, you should be aware that Java is a very high-level language with access to system functions only provided to you through native function calls. These calls are implemented by your Virtual Machine, and there are quite a few ( Sun, Open, Dalvik.. ), so general advice can't be given, but the return time currentTimeMillis is depending on a lot of stuff, like Threading ( in the VM as well as native threads ), the resolution of the onboard timer etc. I admit that the results are strange, but unless you are highly dependent on their correct order, I wouldn't bother and just live with an anomaly in the range of a tenth of a second.

If you need more specific advice, please paste some of your source code!

Edit:

After having seen your source, I'm quite sure that your Log function uses some kind of priority processing, or threading, that leads to false results. Just try to assign the return value of the method in question and pass that variable to your log:

long foo = System.currentTimeMillis();
setLog(foo); 

Upvotes: 0

Related Questions