Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Difference between Thread.yield() and Thread.sleep() methods

    Hi,

    I came to know that sleep() is used to pause the running to out of runnable state.i alse saw in some book, that yield() is also used to pause running thread briefly.

    what it means really?
    what is the diff between these two methods in fuction wise?

    Thanking U
    Naag.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    yield I believe is used not to pause a thread but to allow the threading system to play threads that are waiting to run.

  3. #3
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    may be both are close but there is a different it is shown in the API:)

    yield():

    Causes the currently executing thread object to temporarily pause and allow other threads to execute.

    sleep(long millis) or sleep(long millis,int nanos)

    Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds.

    TIP:
    always check up ur java platform API

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Sleep is used to delay the execution for a period of time, and sleeping thread is guaranteed for sleep at least the specified time. But it not guaranteed that newly weak up thread will actually returning to running.

    Yield is used to get the running thread into out of runnable state with the same priority. A thread might yield, then immediately re-enter to the running state. May be you can confused on that, read about thread priority and it'll give an idea what exactly happen.

  5. #5
    abhilash.k is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Thumbs up To the best i know

    We can prevent a thread from execution by using any of the 3 methods of Thread class:

    1. yield()
    2. join()
    3. sleep()

    1. yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent.

    2. join()-> If any executing thread t1 calls join() on t2 i.e; t2.join() immediately t1 will enter into waiting state until t2 completes its execution.

    3. sleep()-> Based on our requirement we can make a thread to be in sleeping state for a specified period of time(hope not much explanation required for our favorite method).

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In other words, when one thread calls the join() of another thread, currently running thread will wait until the thread it joins with is completed.

    There is another way to prevent thread execution, stop(), but it's deprecated.

  7. #7
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi,
    Thank u for ur quick reply.
    yield() causes current thread to pauses and gives chance to other waiting threads of equal priority threads.

    what my doubt is?
    if we use sleep() current thread goes out of runnable state?
    why thread remained in runnable state if we call yield()?

    why i am asking this question specifically is , i read the Thread life cycle in that they mentioned ,if we apply yield it will be on runnable state,but if we call sleep(), it will be on not runnable state.

    Thanking u
    Naag

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not clear what you've read about thread on that book or something. As I said yield() have to much deal with thread priorities. Did you read the Suns' tutorial about Threads?

  9. #9
    abhilash.k is offline Member
    Join Date
    Nov 2008
    Posts
    2
    Rep Power
    0

    Default

    It could be that when you are calling the sleep() method irrespective of anything the thread will be in sleeping state until the specified time is completed. But with the yield method the thread never goes into the sleeping state it only allows other threads of the same priority to execute. Now if there are no threads of the same priority or any thread that could utilize the yielding of the currently running thread(say t1) then t1 may immediately come into running state, now if t1 is not in runnable state when yield() method is called then it cannot immediately come into running state from runnable state.

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Threads at mercy of Operating System.

    Quote Originally Posted by abhilash.k View Post
    It could be that when you are calling the sleep() method irrespective of anything the thread will be in sleeping state until the specified time is completed.
    Yes.
    Quote Originally Posted by abhilash.k View Post
    But with the yield method the thread never goes into the sleeping state it only allows other threads of the same priority to execute.
    Yield and sleep are implemented in essentially the same manner, yield allows resumption on next dispatch cycle. Sleep provide definite time interval.
    Quote Originally Posted by abhilash.k View Post
    Now if there are no threads of the same priority or any thread that could utilize the yielding of the currently running thread(say t1) then t1 may immediately come into running state, now if t1 is not in runnable state when yield() method is called then it cannot immediately come into running state from runnable state.
    Basically yes, but the operating system has ultimate decision as to which thread will run when. Even with great effort and detailed coding, one cannot prove which thread will run next without a true real-time operating system, and that would have to be linked in to Java by real engineering - based on a definite problem domain.

    Best is to do some testing with actual Threads, I did and am satisfied with the results.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  11. #11
    bobowani is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default

    when should i use grant

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Please start a new thread with a complete question.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    This is very old thread, and the bobowanis' question should be start in a new thread. I've to close this thread, please start a new one if you want to discuss about this further in the correct sub-forum.

Similar Threads

  1. Thread Safe Methods / Locks
    By mrhyman in forum Threads and Synchronization
    Replies: 16
    Last Post: 10-24-2008, 09:57 PM
  2. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM
  3. How to use sleep method of the Thread class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:42 PM
  4. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 05:14 AM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •