AW: Java CountDownLatch.await(timeout) is influenced by changes to Systemtime on Linux

Bernd Eckenfels ecki at zusammenkunft.net
Mon Nov 10 20:59:47 UTC 2014


Hm, how did you change the time? (normal date command?) System.nanoTime is supposed to be monotonic. I think on linux it even uses a timer for exactly that.

i think there was a open bug around park(), but the nanoTime should be find (and negative difference are sonewhat normal with it anyway)
gruss
bernd

-- 
http://bernd.eckenfels.net

----- Ursprüngliche Nachricht -----
Von: "wuwen.55" <wuwen.55 at gmail.com>
Gesendet: ‎10.‎11.‎2014 19:54
An: "core-libs-dev at openjdk.java.net" <core-libs-dev at openjdk.java.net>
Betreff: Java CountDownLatch.await(timeout) is influenced by changes to Systemtime on Linux

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