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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1964363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:44:22  来源:igfitidea点击:

Wrong Time with System.currentTimeMillis () (Java)

javatime

提问by bill0ute

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

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

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.

可以看到,第3行有问题,时间毫秒错误。它应该在 1261860501012 和 1261860501033 之间。大约有 73 毫秒的误差。

Somebody knows where the problem come from ?

有人知道问题出在哪里吗?

Thanks a lot

非常感谢

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 上运行

回答by Pool

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()取决于系统时钟。看起来系统时钟已被外部程序微校正,对于 Linux,可能是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.

请注意,您不应该使用System.currentTimeMillis()来测量经过的时间。使用更好,System.nanoTime()但即使这样也不能保证是单调的。

回答by Steve Emmerson

The method in question depends on the system clock and system clocks can have problems. See http://support.ntp.org/bin/view/Support/KnownOsIssuesfor 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.pdffor a discussion on the accuracy of the system clock in VMWare. It also has as excellent discussion on system clocks in general.

我还推荐http://www.vmware.com/pdf/vmware_timekeeping.pdf来讨论 VMWare 中系统时钟的准确性。它还对一般系统时钟进行了出色的讨论。

回答by moritz

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. 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.

为了切入正题,您应该意识到 Java 是一种非常高级的语言,它可以访问仅通过本机函数调用提供给您的系统函数。这些调用是由你的虚拟机实现的,有很多(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. Just try to assign the return value of the method in question and pass that variable to your log:

在看过你的源代码后,我很确定你的 Log 函数使用了某种优先级处理或线程,这会导致错误的结果。只需尝试分配相关方法的返回值并将该变量传递给您的日志:

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

回答by Paul Wagland

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.

我们曾经看到过类似的事情,运行在带有 AMD64x2 芯片的 Ubuntu 上。如果你也有我会开始寻找的芯片。