简体   繁体   English

为什么Thread.join使用currentTimeMillis?

[英]Why does Thread.join use currentTimeMillis?

In Java, there are two ways of accessing time, System.currentTimeMillis and System.nanoTime. 在Java中,有两种访问时间的方法,System.currentTimeMillis和System.nanoTime。 The first is similar to a wall clock, and can change time based on leap seconds, and other OS caused time changes. 第一个类似于挂钟,可以根据闰秒改变时间,而其他操作系统导致时间变化。 Because of this, measuring durations of time is better suited to use nanoTime which is more accurate and usually more precise. 因此,测量持续时间更适合使用更准确且通常更精确的nanoTime

With that in mind, why does Thread.join() use the currentTimeMillis instead of nanoTime? 考虑到这一点,为什么Thread.join()使用currentTimeMillis而不是nanoTime?

The code in question is: 有问题的代码是:

    public final synchronized void join(long millis) throws InterruptedException {
    long base = System.currentTimeMillis();
    long now = 0;

    if (millis < 0) {
        throw new IllegalArgumentException("timeout value is negative");
    }

    if (millis == 0) {
        while (isAlive()) {
            wait(0);
        }
    } else {
        while (isAlive()) {
            long delay = millis - now;
            if (delay <= 0) {
                break;
            }
            wait(delay);
            now = System.currentTimeMillis() - base;
        }
    }
}

This actually is a bug, and was reported in JDK-8098798 and JDK-8200284 . 这实际上是一个错误,并在JDK-8098798JDK-8200284中报告 It has been fixed in JDK 12. 它已在JDK 12中修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM