java System.currentTimeMillis 是否总是返回一个 >= 先前调用的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2978598/
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
Will System.currentTimeMillis always return a value >= previous calls?
提问by 1984isnotamanual
https://docs.oracle.com/javase/6/docs/api/java/lang/System.html#currentTimeMillis()says:
https://docs.oracle.com/javase/6/docs/api/java/lang/System.html#currentTimeMillis()说:
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.
以毫秒为单位返回当前时间。请注意,虽然返回值的时间单位是毫秒,但值的粒度取决于底层操作系统,可能会更大。例如,许多操作系统以几十毫秒为单位测量时间。
It is not clear to me if I am guaranteed that this code will always print ever increasing (or the same) numbers.
我不清楚是否可以保证此代码将始终打印不断增加(或相同)的数字。
while (1) {
System.out.println(System.currentTimeMillis() );
}
回答by Cowan
The short answer is no, System.currentTimeMillis()is notmonotonic. It is based on system time, and hence can be subject to variation either way (forward or backward) in the case of clock adjustments (e.g. via NTP).
简短的回答是否定的,System.currentTimeMillis()是不是单调的。它基于系统时间,因此在时钟调整(例如通过NTP)的情况下,可能会发生变化(向前或向后)。
System.nanoTime()is monotonic, if and only if the underlying platform supports CLOCK_MONOTONIC-- see the comments on Java bug report 6458294for a good writeup on some circumstances where this is/isn't true.
System.nanoTime()是单调的,当且仅当底层平台支持时CLOCK_MONOTONIC——请参阅Java 错误报告 6458294上的评论,以了解在某些情况下这是/不是真的。
(And, as an additional anecdote, I have personally observed (several times) System.currentTimeMillis()run 'backwards', in the absence of clock adjustments, across threads -- that is, a call to that method in one thread returned a lower value than a call in another thread, even though it occurred chronologically after it in 'real-time')
(并且,作为一个额外的轶事,我个人观察到(多次)System.currentTimeMillis()在没有时钟调整的情况下跨线程运行“向后”——也就是说,在一个线程中调用该方法返回的值低于调用在另一个线程中,即使它在“实时”中按时间顺序发生)
If you need a monotonic source, System.nanoTime()on a platform supporting monotonicity is your best option.
如果您需要单调源,System.nanoTime()在支持单调性的平台上是您的最佳选择。
回答by Sean Reilly
No, it will not always be >= all previous calls.
不,它不会总是 >= 所有以前的调用。
It might not increase every time if you call it several times in quick succession from the same thread (I know this is the = part of >=, but the behavior often surprises people).
If you call it several times in quick succession from multiple threads, it might do any number of things -- it could go slightly back in time across threads by a very small amount, depending on implementation and random chance.
Most seriously, the value might go back in time by a large amount if the user (rare) or an NTP sync (potentially common) adjusts the system clock.
如果您从同一个线程快速连续多次调用它,它可能不会每次都增加(我知道这是 >= 的 = 部分,但这种行为经常让人们感到惊讶)。
如果您从多个线程快速连续多次调用它,它可能会做任何数量的事情——它可能会在线程间稍微倒退一点,这取决于实现和随机机会。
最严重的是,如果用户(很少见)或 NTP 同步(可能很常见)调整系统时钟,则该值可能会大量回溯。
回答by Mark Rushakoff
It couldn't possibly be guaranteedto be increasing, based on the fact that the user could potentially change the system time between calls.
基于用户可能会更改调用之间的系统时间这一事实,它不可能保证会增加。
Besides that, it shouldstay increasing since it represents milliseconds since the epoch. If it were a normal "wall time", you would have to worry about time changes on leap day or on daylight savings changeover.
除此之外,它应该保持增长,因为它代表自纪元以来的毫秒数。如果这是一个正常的“墙上时间”,您将不得不担心闰日或夏令时转换的时间变化。
回答by Peter Lawrey
If you want a value which is monotonicly increasing you can do something like.
如果你想要一个单调增加的值,你可以做类似的事情。
public enum Time {
;
private static long lastTime;
public synchronized static long increasingTimeMillis() {
long now = System.currentTimeMillis();
if (now > lastTime)
return lastTime = now;
return ++lastTime;
}
}
As long as you call this less than a thousand times per second, your increasing time won't drift too far from the real time but will be unique. (This can work, even if you restart your application)
只要你每秒调用它少于一千次,你增加的时间就不会偏离实际时间太远,而是独一无二的。(即使您重新启动应用程序,这也可以工作)
回答by trashgod
@Mark Rushakoff is right; nanoTime()might be slightly more reliable.
@Mark Rushakoff 是对的;nanoTime()可能稍微可靠一点。
Addendum: note these caveats, cited by @Steven Schlansker.
附录:注意@Steven Schlansker 引用的这些警告。

