Saturday 16 March 2013

What is the difference between yielding and sleeping?

What is the difference between yielding and sleeping? 
In yielding the thread permits another thread which is waiting in queue to use CPU time ( i.e execute ) while the current thread goes to the back of the queue ( i.e in a round robin fashion ).

In sleeping the thread suspends its activity itself by calling the sleep method. This thread can be made active again by using notify() or notifyAll() methods.

The major problem with the sleep() approach is that it completely depends on external threads to "Awaken" it. Such an arrangement is dead-lock prone and kind of very tough to debug and so it is generally avoided.



Blog Author: Vijay Kumar

Go to: Coding Problem Solutions

No comments: