• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does a thread always move to the ready state if yield method is called?  RSS feed

 
Robert A Kirkwood
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the "Complete Java 2 Certification" book, a call to the yield() method causes the currently executing thread to move to the Ready state if the scheduler is willing to run any other thread in place of the yielding thread. This would indicate that if the yield method is called but there is no other thread to run then the thread never moves to the Ready state.
My question is really, how is the evaluation of which thread to run carried out if the running thread is not moved to the Ready state first?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Threads deliberately avoid getting into mandating specific designs for scheduling the threads. Each JVM is free to do what it likes, within the constraints of the API. I am pretty sure, therefore, that there is no guarantee which thread will run after you call yield().
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads forum...
 
Geoff Thé
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In our SCJP study group we're using both the K&B and M&R books. We just covered threads, and there seems to be some confusion as to what yield() really does to the thread state.

On page 369 of M&R there is a diagram clearly showing that when yield() is called the thread leaves the "Runnning" state and goes into the "Ready-to-Run" state.

But on page 553 of K&B, they state that "The yield() method is not guaranteed to cause a thread to leave the running state, although if there are runnable threads of the same priority as the currently running thread, then the current thread will probably leave the running state."

Which one is correct?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the documentation for yield. It says the currently running thread is temporarily paused. So I would assume its paused, even if its immediately restarted.

The scheduling is handled by the OS though so if you want a certain answer you will have to consult the documentation for your particular OS. The JVM could do thread scheduling if it wished but I believe they stopped this a long time ago.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!