System.currentTimeMillis()方法真的返回当前时间吗? [英] Does the method System.currentTimeMillis() really return the current time?

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

问题描述

根据链接中提出的想法,我实施了几种不同的睡眠方法。其中一种方法是二进制睡眠,它看起来像这样:

Based on ideas presented in link I implemented several different "sleep methods". One of this methods was the "binary sleep", which looks like this:

while (System.currentTimeMillis() < nextTimeStamp)
{
sleepTime -= (sleepTime / 2);
sleep(sleepTime);
}

因为检查是否已达到下一个时间步骤是在开头我希望该方法运行时间过长。但模拟误差的累积分布(预期时间 - 实时)如下所示: alt文本http://img267.imageshack.us/img267/4224/errorvscummulativearran.jpg

Because the check if the next time step is already reached takes place at the beginning I, would expect that the method is running too long. But the cummulative distribution of the simulation error (expected time - real time) looks like this: alt text http://img267.imageshack.us/img267/4224/errorvscummulativearran.jpg

有人知道为什么我得到这个结果吗?也许方法System.currentTimeMillis()没有真正返回当前时间?

Does somebody has an idea why I'm getting this results? Maybe the method System.currentTimeMillis() does not really return the current time?

BR,

Markus

@irreputable

@irreputable

当我进行评估时,我还使用德国统计程序创建了钟形曲线。因为无法更改标题,所以这里是所有相关项目的英文翻译:

When I made the evaluation I also created a bell curve by using a german statistic program. Because it was not possible to change caption, here is the english translation of all relevant items:

Häufigkeit= frequency

Häufigkeit = frequency

Fehler =错误

Fehler = error

Mittelwert =平均值

Mittelwert = average

Std-Abw =标准差

Std-Abw = standard deviation

alt text http://img694.imageshack.us /img694/2254/bellcurve.jpg

推荐答案

不,不是。它的小弟弟 系统#nanoTime() 精度 /docs/api/java/lang/System.html#currentTimeMillis%28%29\"rel =nofollow noreferrer> System#currentTimeMillis()

No it does not. Its young brother System#nanoTime() has a much better precision than System#currentTimeMillis().

除了他们的Javadocs中的答案(点击上面的链接),这个主题也在这里讨论过几次。在currenttimemillis vs nanotime上进行搜索,您将了解每个主题: System.currentTimeMillis vs System.nanoTime

Apart from the answers in their Javadocs (click at the links here above), this subject was discussed several times here as well. Do a search on "currenttimemillis vs nanotime" and you'll get under each this topic: System.currentTimeMillis vs System.nanoTime.

这篇关于System.currentTimeMillis()方法真的返回当前时间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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