English 中文(简体)
是否保证了System.nanotime()+ System.nanotime()=0?
原标题:Is -System.nanoTime() + System.nanoTime() guaranteed to be >= 0?
  • 时间:2012-01-13 16:19:17
  •  标签:
  • java
  • time

今天,我有一部这样的法典:

public class Test {
    public static void main(String args[]) {
        long a = System.currentTimeMillis(); // line 1
        long b = System.currentTimeMillis(); // line 2
        assert b - a >= 0;

        long y = System.nanoTime(); // line 5
        long z = System.nanoTime(); // line 6
    }
}

因此,IERS指出,下一个跃式第二步将在<30th2012年6月11:59.9之后立即进行。

我很想知道,我是否有权说,如果第1行的运行时间是30th <<>th<>/sup>,2012年6月11日:59.9的周转时间为0.9秒,1st July 2012 00:0

And line 2 is run at 0.1 second after line 1,

<代码>b-可以是negative?

If that s the case, is it true that if line 5 is run at 0.9 seconds after 30th June 2012 11:59.9 turns 1st July 2012 00:00.0,

第6行在第五行之后的0.1秒。

The result of z - y could be negative ? (-900,000,000 nanoseconds?)

最佳回答

<>代码>System.nanotime>should 单逐年增加——如果您有两条电话,<代码>A和B和<代码>A在<代码>B前发生,则<代码>A <=B。 但在实践中,您实际上可以观察到<代码>nanotime 。

< 编号>nanotime>/编号>由万国邮联内部对口单位确定,其起步时间基本上属任意性质(因此无法用来确定隔离墙的时段)。 这可能在多核心环境中造成问题,因为核心内部时间可能比另一点不同。 热点试图对此进行补偿,但总是不会成功,因此,事实上,你可以看到,在某些局势中,打上了退步。

有一个recent discussion,关于这一点,载于一致利益邮寄名单。 See in particular this email ? 简要报告有相当详细的内容。

问题回答

我有权说,如果第1行在2012年6月30日之后运行0.9秒,则第11:59.9段转至2012年7月1日,00:00.0。

如果24小时不作调整,30th June 2012 11:59.9后0.9秒即为1st July 2012 00:00.8

The result of b - a would be negative ?

目前的时光是自1970年以来的低温秒。 当天一开始,它就重新开始。 或者说,在你们的生活中,任何时间都是如此。

z的结果——结果将是负面的?

nanoTime() is not the time since the start of the day either. On many JVMs/OSes its the number of nano-seconds since the CPU was last reset.


Not all OSes provide the same resolution. e.g. RHEL/Centos 5.x give only micro-second resolution. This means you can have many calls in a row give the same value (to the micro-second)

long a = System.currentTimeMillis(); // line 1
long b = System.currentTimeMillis(); // line 2
assert b - a >= 0;

This will go backwards whenever the time is corrected by turning it backwards. e.g. via NTP.

long y = System.nanoTime(); // line 5
long z = System.nanoTime(); // line 6

这将落后于各个系统,一个以上的袖珍,无法纠正圣安普反时代的不同。 例如,如果你在Windows XP上,有两张袖珍片,你就会看到差额急剧上升4 000 000 000 000美元,或后退,因为这个差额将翻一番。

No, you are wrong. Because this is not millisecond part of current time, but total milliseconds passed from year 1970.

They could be the same, but later is not less than earlier. However, if a NTP daemon doing its job it could happen, if at some moment the system clock has been adjusted.

纳米时间更为可靠,因为它取决于系统锁,不应通过24小时调整加以改变。

My reading of the wiki page is same as yours: currentTimeMillis() can go backwards due to leap second.

(Why did they bring this fine astronomical problem into civil time? No civilian cares if solar noon is off by a few seconds; actually nobody uses local time to begin with; people in the same time zone can observer solar noon differ by 1 hour. and in a big country with no time zone, the difference can be hours.)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...