Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong Time with System.currentTimeMillis () (Java)

Tags:

java

time

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

like image 766
bill0ute Avatar asked Dec 26 '09 21:12

bill0ute


1 Answers

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.

like image 122
Pool Avatar answered Sep 20 '22 12:09

Pool