睡眠的目的是什么(long millis,int nanos)? [英] What's the purpose of sleep(long millis, int nanos)?

查看:131
本文介绍了睡眠的目的是什么(long millis,int nanos)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JDK中,它实现为:

In the JDK, it's implemented as:

public static void sleep(long millis, int nanos) 
throws InterruptedException {
if (millis < 0) {
        throw new IllegalArgumentException("timeout value is negative");
}

if (nanos < 0 || nanos > 999999) {
        throw new IllegalArgumentException(
            "nanosecond timeout value out of range");
}

if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
    millis++;
}

sleep(millis);
}

这意味着 nanos 论证根本没有做任何事情。

which means the nanos argument doesn't do anything at all.

背后的想法是,在具有更准确时序的硬件上,JVM可以为它提供更好的实现吗?

Is the idea behind it that on hardware with more accurate timing, the JVM for it can provide a better implementation for it?

推荐答案

常规操作系统没有足够精细的分辨率来一次睡眠纳秒。但是,存在实时操作系统,其中安排事件在确切的时刻发生是至关重要的,许多运营的延迟非常低。 ABS系统是RTOS的一个示例。睡眠纳秒在这样的系统上比在普通操作系统上更有用,在普通操作系统中,操作系统无法在不到15ms的时间内可靠地睡眠。

A regular OS doesn't have a fine grained enough resolution to sleep for nanoseconds at a time. However, real time operating systems exist, where scheduling an event to take place at an exact moment in time is critical and latencies for many operations are VERY low. An ABS system is one example of a RTOS. Sleeping for nanoseconds is much more useful on such systems than on normal OSes where the OS can't reliably sleep for any period less than 15ms.

然而,有两个单独的JDK没有解决方案。因此,在Windows和Linux上,JVM将为 x 纳秒进行最佳的睡眠尝试。

However, having two separate JDKs is no solution. Hence on Windows and Linux the JVM will make a best attempt to sleep for x nanoseconds.

这篇关于睡眠的目的是什么(long millis,int nanos)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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