4 Replies Latest reply: Feb 5, 2009 4:44 PM by 807557 RSS

    Unexpected delay..

    807557
      Hi,
      I'm using Java 1.6.0.7 and I write code:


      long cas = System.currentTimeMillis();
                System.out.println("1 " + (System.currentTimeMillis() - cas));
                System.out.println("2 " + (System.currentTimeMillis() - cas));
                System.out.println("3 " + (System.currentTimeMillis() - cas));
                System.out.println("4 " + (System.currentTimeMillis() - cas));
                System.out.println("5 " + (System.currentTimeMillis() - cas));
                System.out.println("6 " + (System.currentTimeMillis() - cas));
                System.out.println("7 " + (System.currentTimeMillis() - cas));
                Thread.yield();
                System.out.println("8 " + (System.currentTimeMillis() - cas));
                Thread.yield();
                System.out.println("9 " + (System.currentTimeMillis() - cas));
                System.out.println("10 " + (System.currentTimeMillis() - cas));
                System.out.println("11 " + (System.currentTimeMillis() - cas));
                if ((System.currentTimeMillis() - cas) > 0 ){}
                if ((System.currentTimeMillis() - cas) < 0 ){}
                System.out.println("12 " + (System.currentTimeMillis() - cas));
                System.out.println("13 " + (System.currentTimeMillis() - cas));
                System.out.println("14 " + (System.currentTimeMillis() - cas));
                System.out.println("15 " + (System.currentTimeMillis() - cas));
                System.out.println("16 " + (System.currentTimeMillis() - cas));


      and sometimes it happend:

      1 0
      2 0
      3 0
      4 0
      5 0
      6 0
      7 0
      8 0
      9 0
      10 296
      11 296
      12 296
      13 296
      14 296
      15 296
      16 296

      it means: delay almost 300 ms...
      how is it possible ? Garbage collector stops my program for 300ms ?

      Thanks Martin Patak
        • 1. Re: Unexpected delay..
          807557
          It's probably not GC but simply other activities on your system. A 300ms delay is nothing really.

          But this is an off-topic post for the real-time Java forum.

          David Holmes
          • 2. Re: Unexpected delay..
            807557
            Well I know that it isn't any realtime system, but I didn't know where to put this article.

            This error happens only sometimes, but cpu-usage is still less than 15%.
            • 3. Re: Unexpected delay..
              807557
              xxAS wrote:
              Well I know that it isn't any realtime system, but I didn't know where to put this article.
              Any of the general Java programming forums, or the concurrency forum. But I'll drag into real-time relevance :)
              This error happens only sometimes, but cpu-usage is still less than 15%.
              You're executing on a time-sharing scheduler that operates across all threads in the system, including system tasks. Your Java threads can be forced off CPU at any time and for a length of time that depends on the complete system load at that time. Any system tasks have higher priority than your Java threads, so it doesn't take much activity to displace the JVM. In a real-time JVM you can assign priorities that allow you to be as high-as or higher than system tasks so you get greater scheduling control and won't be pushed off CPU (unless you're blocking on shared resources).

              David Holmes
              • 4. Re: Unexpected delay..
                807557
                By the way, I just looked a bit closer at your little experiment and I see from all the zeroes and 296 values that you must be on Windows: on Windows the time reported by currentTimeMillis() is only updated every 10ms - hence the bunch of zeroes making it look like it takes "no time" between calls (and the same with the bunch of 296), and if you continue this experiment you'll probably see that you get delayed periodically..

                David