繁体   English   中英

System.currentTimeMillis()方法真的返回当前时间吗?

[英]Does the method System.currentTimeMillis() really return the current time?

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

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

因为检查是否已经到达下一个时间步骤发生在开头I,所以预计该方法运行时间太长。 但模拟误差的累积分布(预期时间 - 实时)如下所示: alt text http://img267.imageshack.us/img267/4224/errorvscummulativearran.jpg

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

BR,

马库斯

@irreputable

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

Häufigkeit=频率

费勒=错误

Mittelwert =平均值

Std-Abw =标准偏差

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

不,不是的。 它的弟弟System#nanoTime()System#currentTimeMillis()具有更好的精度

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

根据文档,

  * Returns the current time in milliseconds. Note that * while the unit of time of the return value is a millisecond, * the granularity of the value depends on the underlying * operating system and may be larger. For example, many * operating systems measure time in units of tens of * milliseconds. 

您所看到的是基础时钟分辨率达到15ms。 这是操作系统和中断率的一个特性。 有一个Linux内核的补丁,将此分辨率提高到1毫秒,我不确定Windows。 已经有很多关于此的帖子

暂无
暂无

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

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