File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes yield() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "yield()" Watch "yield()" New topic


Hima Nadi
Ranch Hand

Joined: Apr 08, 2002
Posts: 32
Which of the following will definitely stop a thread from executing:
my ans is wait(),suspend(),sleep()
but Jxam says yield() is also i missing something
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
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.

Fun programming etcetera!
Steven Sun

Joined: Apr 29, 2002
Posts: 26
yield will cause thread itself give up the CPU time and turned to ready status until JVM re-call it is "run"->"ready",but also stop execute

newly SCJP2 <img src="tongue.gif" border="0">
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
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."

SCJP 1.4
I agree. Here's the link:
subject: yield()
It's not a secret anymore!