File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "yield()" Watch "yield()" New topic
Author

yield()

Hima Nadi
Ranch Hand

Joined: Apr 08, 2002
Posts: 32
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

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
Greenhorn

Joined: Apr 29, 2002
Posts: 26
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


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

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."


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: yield()
 
Similar Threads
Thread
When is lock released?
on threads
Thread
some general Q's