简体   繁体   English

System.currentTimeMillis()(Java)错误的时间

[英]Wrong Time with System.currentTimeMillis () (Java)

I made a little program to test the System.currentTimeMillis (). 我做了一个小程序来测试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. 如您所见,第3行存在问题。时间磨损是错误的。 It should be between 1261860501012 and 1261860501033. There is an error of, roughly, 73 milli seconds. 它应该在1261860501012和1261860501033之间。大约有73毫秒的误差。

Somebody knows where the problem come from ? 有人知道问题来自哪里?

Thanks a lot 非常感谢

bill0ute bill0ute

Edit : OS : Debian 4.0, Java : 6_17. 编辑:操作系统:Debian 4.0,Java:6_17。

My code : 我的代码:

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

Edit : The program run on a Linux based VPS 编辑:程序在基于Linux的VPS上运行

System.currentTimeMillis() is dependent on System clock. System.currentTimeMillis()依赖于系统时钟。 It looks like the system clock has been micro-corrected by an external programme, for Linux that's probably NTP . 看起来系统时钟已被外部程序微调,对于可能是NTP的 Linux。

Note you shouldn't use System.currentTimeMillis() to measure elapsed time. 请注意,您不应使用System.currentTimeMillis()来测量已用时间。 It's better to use System.nanoTime() but even that isn't guaranteed to be monotonic. 最好使用System.nanoTime()但即使这样也不能保证是单调的。

First of all, you have a little typo, it's 73 milliseconds, not seconds ( would be disturbing then :-) ). 首先,你有一个小错字,它是73毫秒,而不是秒(那将是令人不安的:-))。

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. 为了达到这一点,您应该知道Java是一种非常高级的语言,只能访问通过本机函数调用提供给您的系统函数。 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. 这些调用是由您的虚拟机实现的,并且有很多(Sun,Open,Dalvik ..),所以不能给出一般建议,但返回时间currentTimeMillis取决于许多东西,如线程(在VM和本机线程中),板载计时器等的分辨率。我承认结果很奇怪,但除非你高度依赖于他们的正确顺序,否则我不会打扰而只是生活在异常中十分之一秒的范围。

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. 在看过您的源代码之后,我非常确定您的Log函数使用某种优先级处理或线程,这会导致错误的结果。 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); 

We once saw a similar thing, running on Ubuntu with AMD64x2 chips. 我们曾经看过类似的东西,在Ubuntu上运行AMD64x2芯片。 If you also have the chip that is where I would start looking. 如果你还有芯片,我会开始寻找。

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. 有关通过ntpd(8)守护程序保持系统时钟准确的问题的讨论,请参见http://support.ntp.org/bin/view/Support/KnownOsIssues

I also recommend http://www.vmware.com/pdf/vmware_timekeeping.pdf for a discussion on the accuracy of the system clock in VMWare. 我还建议http://www.vmware.com/pdf/vmware_timekeeping.pdf讨论VMWare中系统时钟的准确性。 It also has as excellent discussion on system clocks in general. 它对系统时钟的讨论也很普遍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM