System.currentTimeMillis()(Java)错误的时间 [英] Wrong Time with System.currentTimeMillis () (Java)

查看:727
本文介绍了System.currentTimeMillis()(Java)错误的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个小程序来测试System.currentTimeMillis()。我有一个奇怪的结果。这是我的日志:

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

如您所见,第3行出现问题。时间磨损是错误的。它应该介于1261860501012和1261860501033之间。
大约有73毫秒的错误。

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 ?

非常感谢

bill0ute

编辑:
OS:Debian 4.0,Java:6_17。

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

我的代码:

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

编辑:程序在基于Linux的VPS上运行

Edit : The program run on a Linux based VPS

推荐答案

System.currentTimeMillis()取决于系统时钟。看起来系统时钟已被外部程序微调,对于Linux可能是 NTP

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.

请注意,您不应使用 System.currentTimeMillis()来衡量已用时间。最好使用 System.nanoTime(),但即使这样也不能保证单调。

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.

这篇关于System.currentTimeMillis()(Java)错误的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆