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;
}
}
}
join()is @since 1.0 andnanoTime()isn't, and changing the behaviour would have been poor practice. - user207421