Java-创建内部时钟 [英] Java - Creating an Internal Clock

查看:225
本文介绍了Java-创建内部时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望计算从Java中特定时间起经过的毫秒数。

I wish to calculate the time passed in milliseconds from a specific time in Java.

使用 System.currentTimeMillis( ); 作为开始时间,然后将其与上一个时间一起使用以获取经过时间。我希望做类似的事情,但不要依赖系统时间。

The classic way it to use System.currentTimeMillis(); for the starting time, and then use this again with the previous one to get the elapsed time. I wish to do something similar to this, but NOT rely on the system time for this.

如果我依赖系统时间,程序的用户可以操纵

If I rely on the system time, the user of the program could manipulate the system clock to hack the program.

我尝试使用类似于以下代码:

I have tried using code similar to the following:

int elapsed = 0;
while (true) {
    Thread.sleep(10);
    elapsed += 10;
}

这是可行的,但是在计算机的情况下我不太可靠滞后然后锁定一两秒。

This works, but I it is not too reliable in the case that the computer lags and then locks up for a second or two.

有任何想法吗?

推荐答案

您要使用 System.nanoTime 。它与系统时钟无关。

You want to utilize System.nanoTime. It has no relation to the system clock. It can only be used to track relative time which seems to be all you want to do.

只是为了跟踪相对时间,这似乎是您想要做的所有事情。是一个简短的解释。

In an effort to prevent this answer from just being a link to another answer here is a short explanation.

来自文档


公共静态长整型nanoTime()返回最精确的
个系统计时器的当前值,以纳秒为单位。

public static long nanoTime() Returns the current value of the most precise available system timer, in nanoseconds.

此方法只能用于测量经过的时间时间,与系统或挂钟时间的任何其他概念无关。返回的值
代表自某个固定但任意的时间
以来的纳秒(可能在将来,因此值可能为负)。此方法
提供纳秒精度,但不一定提供纳秒精度。无法保证值更改的频率。
连续调用中跨越大约292年(2 63 纳秒)的差异,由于数值溢出,因此无法准确计算经过时间

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.

还有另一个指向计时器信息的链接: https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks

Yet another link to timer information: https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks

您可以使用Java的Timer类生成检查以某种特定的精度回调,可以说每500毫秒一次。此回调不会用于确定500ms确实通过了。您将在回调中调用 System.nanoTime 并将其与上次调用 System.nanoTime 的时间进行比较。不管壁钟如何变化,这都将为您提供经过时间的准确表示。

You could use Java's Timer class to spawn a "check" callback at some specific precision, lets say every 500ms. This callback would not be used to determine that 500ms actually did pass. You would call System.nanoTime in the callback and compare it to the last time you called System.nanoTime. That would give you a fairly accurate representation of the amount of time that has passed regardless of the wall clock changing.

您可以在此处查看:System.currentTimeMillis与System.nanoTime

这篇关于Java-创建内部时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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