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

wuwen.55 wuwen.55 at gmail.com
Tue Nov 11 06:35:01 UTC 2014


Thank you!
    I'm version is 7u55. and upgrade 7u60 test is ok.


jdk 1.7.0_55  test result

time:Tue Nov 11 13:24:01 CST 2014
nanoTime:18093680000
currentTime:18093
-------------------------
time:Tue Nov 11 13:24:02 CST 2014
nanoTime:19097878000
currentTime:19098
-------------------------
time:Tue Nov 11 12:24:03 CST 2014
nanoTime:-3580426662000   (negative difference)
currentTime:-3580427
-------------------------
time:Tue Nov 11 12:24:04 CST 2014
nanoTime:-3579424553000
currentTime:-3579425

jdk 1.7.0_60 test result

time:Tue Nov 11 12:28:51 CST 2014
nanoTime:8034263027
currentTime:8034
-------------------------
time:Tue Nov 11 12:28:52 CST 2014
nanoTime:9034780087
currentTime:9035
-------------------------
time:Tue Nov 11 11:28:50 CST 2014
nanoTime:10035322113  (is ok)
currentTime:-3593155
-------------------------
time:Tue Nov 11 11:28:51 CST 2014
nanoTime:11035884267
currentTime:-3592154
 


> 在 2014年11月11日,上午10:25,David Holmes <david.holmes at oracle.com> 写道:
> 
> Repeating my response due to the subject mangling in the other "thread":
> 
> This is a known issue:
> 
> https://bugs.openjdk.java.net/browse/JDK-6900441
> 
> fixed in 7u60, 8 and 9.
> 
> What version were you running?
> 
> David
> 
> On 7/11/2014 6:22 PM, wuwen.55 wrote:
>> 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