简体   繁体   中英

Why does Thread.join use currentTimeMillis?

In Java, there are two ways of accessing time, System.currentTimeMillis and 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.

With that in mind, why does Thread.join() use the currentTimeMillis instead of 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 . It has been fixed in JDK 12.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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