Results 1 to 13 of 13
- 11-02-2008, 07:02 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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.
-
yield I believe is used not to pause a thread but to allow the threading system to play threads that are waiting to run.
- 11-02-2008, 10:53 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
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
- 11-03-2008, 08:26 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 11-03-2008, 11:51 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
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).
- 11-03-2008, 04:57 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 11-04-2008, 02:31 AM #7
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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
- 11-04-2008, 04:09 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 11-04-2008, 07:00 AM #9
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
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.
- 11-11-2008, 02:51 PM #10
Threads at mercy of Operating System.
Yes. Yield and sleep are implemented in essentially the same manner, yield allows resumption on next dispatch cycle. Sleep provide definite time interval. 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
- 07-29-2010, 03:28 PM #11
Member
- Join Date
- Jul 2010
- Posts
- 1
- Rep Power
- 0
when should i use grant
- 07-29-2010, 05:29 PM #12
Please start a new thread with a complete question.
- 07-30-2010, 05:37 PM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Thread Safe Methods / Locks
By mrhyman in forum Threads and SynchronizationReplies: 16Last Post: 10-24-2008, 09:57 PM -
Sleep in thread
By jithan in forum New To JavaReplies: 1Last Post: 08-27-2008, 02:27 PM -
How to use sleep method of the Thread class
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:42 PM -
Can't get my thread to sleep!
By jamesfrize in forum New To JavaReplies: 2Last Post: 03-25-2008, 05:14 AM -
How to use the sleep and thread?
By jiuhu in forum Java AppletsReplies: 4Last Post: 08-07-2007, 02:56 AM


LinkBack URL
About LinkBacks


Bookmarks