5 Replies Latest reply: Dec 2, 2012 8:16 AM by jtahlborn RSS

    Difference between LockSupport.parkUntil and Thread.sleep method

    860521
      Hi

      Could someone please explain the difference between LockSupport.parkUntil and Thread.sleep methods.

      Thanks in advance.
        • 1. Re: Difference between LockSupport.parkUntil and Thread.sleep method
          EJP
          What part of the Javadoc didn't you understand?
          • 2. Re: Difference between LockSupport.parkUntil and Thread.sleep method
            860521
            The thing that I am trying to understand is - the difference / correlation between the two methods mentioned.

            In the javadoc, there is a mention of correlation between the LockSupport methods park, unpark and the Thread methods suspend, resume - but not park and sleep.

            Also, I would like to understand the statement

            These methods are designed to be used as tools for creating higher-level synchronization utilities, and are not in themselves useful for most concurrency control applications. (from the javadoc)
            • 3. Re: Difference between LockSupport.parkUntil and Thread.sleep method
              DrClap
              javaLearner wrote:
              The thing that I am trying to understand is - the difference / correlation between the two methods mentioned.

              In the javadoc, there is a mention of correlation between the LockSupport methods park, unpark and the Thread methods suspend, resume - but not park and sleep.
              Then that suggests that there isn't any difference /correlation -- whatever you meant by that. Perhaps your question is like asking for the difference between a fish and a bicycle.
              Also, I would like to understand the statement

              These methods are designed to be used as tools for creating higher-level synchronization utilities, and are not in themselves useful for most concurrency control applications. (from the javadoc)
              Again, you're going to have to explain what you don't understand about it.
              • 4. Re: Difference between LockSupport.parkUntil and Thread.sleep method
                966867
                Hi,
                javaLearner wrote:Could someone please explain the difference between LockSupport.parkUntil and Thread.sleep methods.
                I would say semantically both methods are the same:
                LockSupport .parckUntil(long deadline) = Thread.sleep(long millis)
                1)     LockSupport.parckUntil(long deadline) uses finally native method Unsafe.park(boolean isAbsolute, long time).
                2)     Thread.sleep(long millis) is already native.
                Maybe both methods have the same native implementation, but haven’t seen the code.

                http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/concurrent/locks/LockSupport.java#LockSupport.parkUntil%28long%29
                http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Thread.java#Thread.sleep%28long%29


                Best Regards
                Andrej
                • 5. Re: Difference between LockSupport.parkUntil and Thread.sleep method
                  jtahlborn
                  it seems quite obvious from the javadoc that these methods are not the same. from parkUntil:

                  >
                  ... unless the permit is available.

                  If the permit is available then it is consumed and the call returns immediately; ...
                  >

                  What about this resembles anything in Thread.sleep?