8 Replies Latest reply: Jun 22, 2009 5:20 PM by 807588 RSS

    Thread sleep

    807588
      When we say, Thread.sleep(1000), I know that the current thread goes to sleep for 1000 milliseconds.

      Is there something internally, which wakes the thread?

      Like for wait() we notify(), is there something internally?
        • 1. Re: Thread sleep
          JoachimSauer
          pawanvi wrote:
          Is there something internally, which wakes the thread?
          Probably.

          But the specifics are not defined, so it's implementation-dependent.

          Why does that information matter to you?
          • 2. Re: Thread sleep
            807588
            This question was cropped up, in one of my interviews. I did not get any answers.... Am just curious to know the answer.
            • 3. Re: Thread sleep
              JoachimSauer
              pawanvi wrote:
              This question was cropped up, in one of my interviews. I did not get any answers.... Am just curious to know the answer.
              I gave you the answer. What's wrong with that?
              • 4. Re: Thread sleep
                807588
                What do you mean by implementation- dependent. I do know that, sleep is a native method and its implementation dependent.

                I want to know how exactly this is handled.
                • 5. Re: Thread sleep
                  JoachimSauer
                  pawanvi wrote:
                  What do you mean by implementation- dependent.
                  It means that the exact way it is implemented depends entirely on how it's implemented in any given JVM. There's no way a JVM has to implement it. Any way that satisfies the documentation is ok.
                  I do know that, sleep is a native method and its implementation dependent.
                  Right. You can't give a general answer that's true or all JVM implementation.
                  I want to know how exactly this is handled.
                  Then the first step you need to take is to make your question more specific:

                  For which exact JVM implementation/version do you want to know this? Since every implementation and version could handle it differently you can only really answer that for a specific one.
                  • 6. Re: Thread sleep
                    807588
                    http://www.javamex.com/tutorials/threads/thread_scheduling.shtml

                    http://www.javamex.com/tutorials/threads/sleep.shtml
                    • 7. Re: Thread sleep
                      807588
                      pawanvi wrote:
                      What do you mean by implementation- dependent. I do know that, sleep is a native method and its implementation dependent.

                      I want to know how exactly this is handled.
                      What part of "implementation-dependent" don't you understand?
                      .
                      .
                      .
                      Oh... all of it...
                      • 8. Re: Thread sleep
                        807588
                        "implementation-dependant" usually means, you should need to know and you shouldn't rely on the behaviour of one version of a JVM.

                        However, AFAIK most threads are native threads supported by the OS and sleep is supported by the OS too. The OS has a scheduler and it decides when a thread is started or switched.