Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

yield()

 
Hima Nadi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following will definitely stop a thread from executing:
wait()
notify()
yield()
suspend()
sleep()
my ans is wait(),suspend(),sleep()
but Jxam says yield() is also correct.am i missing something
Thanks
Hima
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think yield() is, technically, a correct answer since calling yield() causes the currently running thread to return to the ready state and allows the VM to decide which thread to continue with. This is not to say that that the VM will not possibly pick the same thread that just yielded and continue (to the eye of the suer) as if nothing had happened. I suppose the point that they are after is that the running thread must stop executing for some period of time, even if that thread is re-chosen by the VM. This seems like a loaded, if not poorly worded question.
 
Steven Sun
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah!
yield will cause thread itself give up the CPU time and turned to ready status until JVM re-call it run.it is "run"->"ready",but also stop execute
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, yield() is just a suggestion made to the thread scheduler that the current thread is not doing anything very interesting at the moment, and if it wants, it can go ahead and run a different thread.
Since it's just a suggestion, there's no guarantee about the behavior on a particular implementation of a JVM. In fact, on a multiple processor system, a good thread scheduling algorithm will just ignore your yield() call, since it can do a far better job of thread balancing that you can as a programmer.
So, it's not correct to say that "calling yield() will definately stop a thread from executing."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic