java park and sleep issue

Boris Samorodov bsam at passap.ru
Wed Sep 2 12:31:26 UTC 2015


Hi All,


I'm a newcomer to the java world, so my questions may be too vague.

So, I've got an unexpected result at FreeBSD (r287323 amd64) and java
(openjdk version "1.7.0_80"):
-----
Starting...
For timeVal = 0; Time elapsed: 1 ms
For timeVal = 1; Time elapsed: 2000 ms
For timeVal = 2; Time elapsed: 3000 ms
For timeVal = 3; Time elapsed: 4000 ms
For timeVal = 4; Time elapsed: 5000 ms
-----

Linux (CentOS-6.6 x64) and java (java version "1.7.0_71") give a more
expected result:
-----
Starting...
For timeVal = 0; Time elapsed: 1 ms
For timeVal = 1; Time elapsed: 1056 ms
For timeVal = 2; Time elapsed: 2065 ms
For timeVal = 3; Time elapsed: 3066 ms
For timeVal = 4; Time elapsed: 4066 ms
-----

The testcase is:
-----
public class App {
    public static void main(String[] args) {
        System.out.println("Starting...");

        for (int expectedMS = 0; expectedMS < 5; expectedMS++) {
            long start = System.currentTimeMillis();

            for (int i = 0; i < 1000; i++)
                java.util.concurrent.locks.LockSupport.parkNanos((long)
1e6 * expectedMS);

            long stop = System.currentTimeMillis();
            System.out.println("For timeVal = " + expectedMS + "; Time
elapsed: " + (stop - start) + " ms");
        }

    }
}
-----

BTW, Thread.sleep() gives the same results both at FreeBSD and Linux.

Is it a real issue? Or may be it's just OK?

-- 
WBR, Boris Samorodov (bsam)
FreeBSD Committer, http://www.FreeBSD.org The Power To Serve


More information about the freebsd-questions mailing list