Java CountDownLatch.await(timeout) is influenced by changes to System time on Linux

wuwen.55 wuwen.55 at gmail.com
Fri Nov 7 08:22:44 UTC 2014


call .await(10, TimeUnit.SECONDS) and change the time one hours back.

i.e. start at 13:00 call .await and at 13:05 change the time to 12:05

you will notice that the wait does not return.

when change the time to the current time . change the time to 13:05.

the wait still does not return.

test code:
CountDownLatch c = new CountDownLatch(1);
...
	    c.await(10, TimeUnit.SECONDS);
...
----------------------------------

AbstractQueuedSynchronizer.doAcquireNanos


long lastTime = System.nanoTime();
	final Node node = addWaiter(Node.EXCLUSIVE);
	boolean failed = true;
	try {
	    for (;;) {
		final Node p = node.predecessor();
		if (p == head && tryAcquire(arg)) {
		    setHead(node);
		    p.next = null; // help GC
		    failed = false;
		    return true;
		}
		if (nanosTimeout <= 0)
		    return false;
		if (shouldParkAfterFailedAcquire(p, node) &&
		    nanosTimeout > spinForTimeoutThreshold)
		    LockSupport.parkNanos(this, nanosTimeout);
		long now = System.nanoTime();
		nanosTimeout -= now - lastTime;
		lastTime = now;
		if (Thread.interrupted())
		    throw new InterruptedException();
	    }
	} finally {
	    if (failed)
		cancelAcquire(node);
	}

when change the time one hours back

now - lastTime is negative number.

—————————





More information about the core-libs-dev mailing list