--- old/test/java/lang/ref/EarlyTimeout.java 2014-04-14 16:48:09.766818513 +0400 +++ new/test/java/lang/ref/EarlyTimeout.java 2014-04-14 16:48:09.110826759 +0400 @@ -33,6 +33,7 @@ import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; import java.util.concurrent.CountDownLatch; +import static java.util.concurrent.TimeUnit.NANOSECONDS; /** * In order to demonstrate the issue we make several threads (two appears to be sufficient) @@ -48,6 +49,8 @@ static final int THREADS_COUNT = 2; static final int TIMEOUT = 1000; + static final int TOLERANCE = 16; // if elapsed time is less than timeout, and differs from it + // by no greater than specified amount, we won't treat it as error static Object referent = new Object(); static final ReferenceQueue queue = new ReferenceQueue(); @@ -77,7 +80,7 @@ } int nonNullRefCount = 0; for (EarlyTimeout thread : threads) { - if (thread.reference == null && thread.actual < TIMEOUT) { + if (thread.reference == null && thread.actual < TIMEOUT - TOLERANCE) { throw new RuntimeException("elapsed time " + thread.actual + " is less than timeout " + TIMEOUT); } @@ -93,9 +96,9 @@ public void run() { try { startedSignal.countDown(); - long start = System.currentTimeMillis(); + long start = System.nanoTime(); reference = queue.remove(TIMEOUT); - actual = System.currentTimeMillis() - start; + actual = NANOSECONDS.toMillis(System.nanoTime() - start); } catch (InterruptedException ex) { throw new RuntimeException(ex); }