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

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

Replies

  • Dhaval Pujara
    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
  • Jaydip Jadhav
    Jaydip Jadhav
    Hey
    yeild() method basically causes the currently executing thread object to temporarily pause and allow other threads to execute.
  • Alok mishra
    Alok mishra
    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
    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
    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
    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
    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
    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.

You are reading an archived discussion.

Related Posts

To all the CEans, Please note that writing a proper description to each of your resource is very critical. The search engine relies on the description and the title of...
iam an electric engineering ,i have just graduated and want to do some courses but actually dont know from where should i start .......sooo need some help from professinals....like uoo....
What are the different types of servers ? Please mention the functions of each server and their differences.
hi,i am a ece 3rd year student i have an interest in software field but don't have proper guidence.i want to know that what qualities should i improve and which...
Hey CEans, I was thinking to get an External Hard Disk of 1 TB. Should I buy hard disk from Seagate ,or any other company? If anyone, has used it...