5 Replies Latest reply: Sep 29, 2009 9:17 AM by 800408 RSS

    sleep in java real time

    800408
      Hi David,
      in our communication module we are trying every "n" seconds to estbalish communication.
      in normal java i probably was using the Thread.sleep(n) method.
      i know that in java real time it is not the proper way.
      what you recommned to use here???
      maybe:
      RealtimeThread.waitForNextPeriod ?????
      or somthing else??

      Thanks
      Gabi
        • 1. Re: sleep in java real time
          807557
          Gabi,
          Yes, use of sleep is not recommended for any logic which executes repeatedly at a given interval or any logic which require precise temporal correctness.
          Greg
          • 2. Re: sleep in java real time
            800408
            Hi Greg,
            Thanks for your replay.
            for our communication module we do not need precise correctness.
            if we put under Thread.Sleep(n) for example 2000 and it was actually 2100 its not a problem for us.

            Thanks
            p.s
            as much as i know you will be here at 13 of october so see you soon.

            Gabi
            • 3. Re: sleep in java real time
              807557
              Additionally, for any logic which requires execution at fixed time intervals the suggested solution is to use either a RealtimeThread or NoHeapRealtimeThread with a ReleaseParameters object of type PeriodicParameters and a conditional loop with .waitForNextPeriod() somewhere in the loop.
              Greg
              • 4. Re: sleep in java real time
                800408
                Hi Greg,
                Thanks for your replay.
                for our communication module we do not need precise correctness.
                if we put under Thread.Sleep(n) for example 2000 and it was actually 2100 its not a problem for us.

                Thanks
                p.s
                as much as i know you will be here at 13 of october so see you soon.

                Gabi
                • 5. Re: sleep in java real time
                  807557
                  Gabi,
                  Gabi wrote:
                  for our communication module we do not need precise correctness.
                  if we put under Thread.Sleep(n) for example 2000 and it was actually 2100 its not a problem for us.
                  The issue is not just the precision of Thread.sleep (which is pretty bad in general - RealtimeThread.sleep is much better!) but the semantics. A sleep (ignoring inaccuracies) is a way to schedule with a fixed-delay, whereas you use periodic threads to schedule at a fixed-rate. The difference being that in fixed-rate the period is the time between successive starts of the periodic activity, whereas in fixed-delay the "period" is the delay between the end of one session of "periodic" activity and the start of the next. (See also the definition of fixed-rate vs fixed-delay in the java.util.Timer javadocs).

                  David