Index: vm/tests/kernel/java/lang/ObjectTest.java =================================================================== --- vm/tests/kernel/java/lang/ObjectTest.java (revision 476051) +++ vm/tests/kernel/java/lang/ObjectTest.java (working copy) @@ -208,8 +208,10 @@ fail("The main thread was interrupted!"); } } - assertTrue("Current thread hasn't sleep enough!", - finish - start + 1 > millis); + long expectedWait = finish - start + 2; + assertTrue("Current thread hasn't slept enough: " + + "expected at least " + millis + " but was " + expectedWait, + expectedWait > millis); } public void testWaitlongint1() { Index: vm/tests/kernel/java/lang/ThreadTest.java =================================================================== --- vm/tests/kernel/java/lang/ThreadTest.java (revision 476051) +++ vm/tests/kernel/java/lang/ThreadTest.java (working copy) @@ -136,12 +136,12 @@ private Action action; private boolean exceptionReceived = false; private long startTime; + private long endTime; ThreadWaiting(Action action, long millis, int nanos) { this.millis = millis; this.nanos = nanos; this.action = action; - this.startTime = System.currentTimeMillis(); } public void run () { @@ -156,7 +156,9 @@ } case SLEEP: try { + this.startTime = System.currentTimeMillis(); Thread.sleep(millis, nanos); + this.endTime = System.currentTimeMillis(); } catch (InterruptedException e) { exceptionReceived = true; } @@ -172,6 +174,10 @@ public long getStartTime() { return startTime; } + + public long getEndTime() { + return endTime; + } } private class ThreadRunningAnotherThread extends Thread { @@ -742,12 +748,14 @@ } catch (InterruptedException e) { fail(INTERRUPTED_MESSAGE); } - long duration = System.currentTimeMillis() - tW.getStartTime(); - duration *= 1000000; + long duration = tW.getEndTime() - tW.getStartTime(); + duration *= 1000000; // nano + duration += 1000000; // + 1 ms for 123456 ns long sleepTime = millis * 1000000 + nanos; - assertTrue("thread has not slept enough", + assertTrue("thread has not slept enough: expected " + sleepTime + + " but was " + duration, duration >= sleepTime); - } + } public void testYield() { ThreadYielding t1 = new ThreadYielding(1); @@ -1558,14 +1566,17 @@ int nanos = 999999; ThreadRunning t = new ThreadRunning(); t.start(); - long joinStartTime = System.currentTimeMillis(); + long joinStartTime = 0; + long curTime = 0; try { + joinStartTime = System.currentTimeMillis(); t.join(millis, nanos); + curTime = System.currentTimeMillis(); } catch (InterruptedException e) { fail(INTERRUPTED_MESSAGE); } - long curTime = System.currentTimeMillis(); - long duration = 1000000 * (curTime - joinStartTime); + // add one more ms to duration considering accuracy of measurement + long duration = 1000000 * (curTime - joinStartTime) + 1000000; long joinTime = 1000000 * millis + nanos; t.stopWork = true; assertTrue("join should wait for at least " + joinTime + Index: vm/thread/src/thread_native_condvar.c =================================================================== --- vm/thread/src/thread_native_condvar.c (revision 476051) +++ vm/thread/src/thread_native_condvar.c (working copy) @@ -68,7 +68,7 @@ // Delegate to OS wait apr_status = (!ms && !nano)? apr_thread_cond_wait((apr_thread_cond_t*)cond, (apr_thread_mutex_t*)mutex): - apr_thread_cond_timedwait ((apr_thread_cond_t*)cond, (apr_thread_mutex_t*)mutex, ms*1000+ nano / 1000); + apr_thread_cond_timedwait ((apr_thread_cond_t*)cond, (apr_thread_mutex_t*)mutex, ms*1000 + (nano < 1000) ? 1 : nano / 1000); set_suspend_disable(disable_count);