View Feed
group-icon
Java Developers
Community of Java Developers: Get Java Programming Help from fellow Java Developers across the world.
654 Members
Join this group to post and comment.
Alok mishra
Alok mishra • Sep 14, 2013

What is the use of yield() in java multithreading ?

What is the use of yield() in java multithreading ?
Dhaval Pujara
Dhaval Pujara • Sep 15, 2013
IT'S all about thread priorities basically thread have given a priority between 1 to 10 when u r developeing application based on multithreading than there is only way to control the exicution steps by controliing the thread must be exicuted in sequence at there yield() is used ......to maintain the priority of the thread
Jaydip Jadhav
Jaydip Jadhav • Sep 15, 2013
Hey
yeild() method basically causes the currently executing thread object to temporarily pause and allow other threads to execute.
Alok mishra
Alok mishra • Sep 15, 2013
Jaydip Jadhav
Hey
yeild() method basically causes the currently executing thread object to temporarily pause and allow other threads to execute.
then how does yield() distinguish itself from sleep() ?
Alok mishra
Alok mishra • Sep 15, 2013
Dhaval Pujara
IT'S all about thread priorities basically thread have given a priority between 1 to 10 when u r developeing application based on multithreading than there is only way to control the exicution steps by controliing the thread must be exicuted in sequence at there yield() is used ......to maintain the priority of the thread
could you please demonstrate how yield() uses priorities to control thread execution ? I really didnt get your point .
The_Small_k
The_Small_k • Sep 15, 2013
Suppose you have number of threads with different priorities in runnable state and a thread with priority 5 is executing(running state) at the same time a thread came with the priority of 6. So in that case the lower priority thread will be paused(using yeild() method).

So yeild() come into action when a thread with higher priority will come into runnable state. As you can see it has no argument like sleep() so once the thread will be paused using sleep() method it will resume again only when the execution of higher priority thread will be completed or paused(externally).
Alok mishra
Alok mishra • Sep 15, 2013
The_Small_k
Suppose you have number of threads with different priorities in runnable state and a thread with priority 5 is executing(running state) at the same time a thread came with the priority of 6. So in that case the lower priority thread will be paused(using yeild() method).

So yeild() come into action when a thread with higher priority will come into runnable state. As you can see it has no argument like sleep() so once the thread will be paused using sleep() method it will resume again only when the execution of higher priority thread will be completed or paused(externally).
This is an implicit mechanism i.e. how JVM used yield() , i say i want to make a program in which i want to make use of yield() ? What then ?
Dhaval Pujara
Dhaval Pujara • Sep 16, 2013
yield() is supposed to do is
make the currently running thread head back to runnable to allow other threads of
the same priority to get their turn. So the intention is to use yield() to promote
turn-taking among equal-priority threads. In reality, though, the yield()
method isn't guaranteed to do what it claims, and even if yield() does cause a
thread to step out of running and back to runnable, there's no guarantee the yielding
thread won't just be chosen again over all the others! So while yield() might—and
often does—make a running thread give up its slot to another runnable thread of the
same priority, there's no guarantee.
A yield() won't ever cause a thread to go to the waiting/sleeping/ blocking
state.
Ajay Pandey
Ajay Pandey • Dec 11, 2013
Yield() method pauses the currently running thread giving a chance to excute same priority threads temporarily. If no wating thread or lower priority thread in runnable pool then continue execution of current thread. But its not necessary pause the thread bcos yield() method is platform dependent. May differ from one os to another os.

Share this content on your social channels -