PDA

View Full Version : about yield and sleep in java 2011



saneha
04-24-2011, 10:28 PM
dear sir in case of yield it give chance to the next thread of equal priority but if we use the sleep and our program have the two or more equal priority threads then is sleep give chance to the next equal priority thread or it give chance to the next thread which has higher priority but lower priority then the currenlty running thread? please explian it.

Vuhelper
04-24-2011, 10:29 PM
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.





But in case of sleep() it is not compulsory that next thread should have higher priority but lower priority then the thread which have been sent to sleep. In case of sleep next thread in the queue will run which ever has higher priority in the queue.