gpt4 book ai didi

java - 如何使用方法 Object.wait(long timeout, int nanos)

转载 作者:行者123 更新时间:2023-11-30 07:08:20 24 4
gpt4 key购买 nike

我正在阅读有关 Object 类的内容,我发现了一个方法 wait(long timeout, int nanos)

我阅读了描述 Docs Oracle Class Object我发现:

The amount of real time, measured in nanoseconds, is given by:

1000000*timeout+nanos

Parameters:
timeout - the maximum time to wait in milliseconds.
nanos - additional time, in nanoseconds range 0-999999.

我查看了此方法的实现 (openjdk-7u40-fcs-src-b43-26_aug_2013),并找到了这段代码:

public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 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 && timeout == 0)) {
timeout++;
}

wait(timeout);
}

第一个和第二个 if 是显而易见的。第三个什么都不做或只是增加 timeout 并且不关心 nanos

我的问题是,如何调用真正适用于给定 nanos 的方法 wait(long timeout, int nanos)

提前致谢

最佳答案

确实考虑了纳米:第三个if说:

if (nanos >= 500000...
timeout++;

这意味着如果超过 500000 纳秒,我们会将其四舍五入到另一毫秒,否则,我们会将其向下舍入。

这也符合定义:1000000*timeout+nanos 转换回毫秒后。

关于java - 如何使用方法 Object.wait(long timeout, int nanos),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24062405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com