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 on yield() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "on yield()" Watch "on yield()" New topic

on yield()

mounika paruchuri

Joined: Feb 12, 2002
Posts: 26
Question id : 952739433290
In which of the following cases a thread will definitely be alive but not be running ?
a) The thread has issued a call to wait().
b) The thread is trying to enter a synchronized block and the monitor is not free.
c)A higher priority thread comes in ready to run state...
d)The thread is sleeping as a result of a call to the sleep() method
e)The thread call yield().
the answer is a,b,c....
My question is why e is not coreect and
also I would appreciate somebody explaining or giving a link to the behaviour of wait,yield and sleep methods in such cases and illustrate their differences...
I have gone through RHE and Mughal on these topics several times...but I do mistakes on these type of questions again and again..
so having my SCJP on Monday...I would greatly appreciate any clear explaining
of this stuff

thanks in advance
mounika paruchuri

Joined: Feb 12, 2002
Posts: 26
sorry a typo,
the answer to above question is a,b,d
Rob Ross

Joined: Jan 07, 2002
Posts: 2205
Well the question asked "In which of the following cases a thread will definitely be alive but not be running "
There are certain things you can *definitely* predict about thread behavior, and there are others that you cannot.
You cannot make predictions about yield(), for the same reasons that answer c is not correct.
You should think of yield() the same way you think of System.gc();'s a suggestion to the JVM. There's no guarantee it will actually perform the requested action.
When you call yield(), you are saying to the JVM "Hey Mr. BoJangles (I refer to my JVM as Mr. BoJangles), this thread doesn't really need to be executed right now, so if you want to give it to another thread that might have something more important to do, go ahead, I don't mind."
That's all you are doing when you call yield(). There is no guarantee that the JVM will then actually pause your thread and cause another one to run. So you can't say the thread is definately not running after you call yield(). That's why answer e is not correct.

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