System.nanoTime()是否在线程间保持一致? [英] Is System.nanoTime() consistent across threads?

查看:137
本文介绍了System.nanoTime()是否在线程间保持一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算两个事件之间经过的时间,以纳秒为单位。为此,我可以使用 System.nanoTime(),如上所述此处。问题是这两个事件发生在不同的线程中。

I want to count the time elapsed between two events in nanoseconds. To do that, I can use System.nanoTime() as mentioned here. The problem is that the two events are happening in different threads.

由于 nanoTime()未返回绝对时间戳但是只能用来计算时间差异,我想知道我在两个不同线程上获得的值是否与两个事件之间经过的物理时间一致。

Since nanoTime() doesn't return an absolute timestamp but instead can only be used to calculate time differences, I'd like to know if the values I get on the two different threads are consistent with the physical time elapsed between the two events.

推荐答案

这是应该是,但由于有缺陷的内核或硬件,答案可能是否定的,至少在某些环境中是这样。

It's supposed to be, but due to buggy kernels or hardware, the answer can be no, at least in some environments.

这篇关于System.nanoTime()是否在线程间保持一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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