This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Given a reference called t to a class which extends Thread, which of the following will cause it to give up cycles to allow another thread to execute. 1) t.yield(); 2) Thread.yield(); 3) yield(100); //Or some other suitable amount in milliseconds 4) yield(t);
The answers given are 1 and 2 but I find the question troubling. The question does not state whether t is a reference to the current thread or another thread so I took it to mean that t could be a reference to another thread. If so, none of the answers are correct. Perhaps I am missing something but the question seems to be unclear in its intent. Are we expected to see our way through ambiguously worded questions on the real exam?
My take is that yes, some of the questions are a little ambigous. As I recall from my SCJP exam however, I don't remember there being more than 1 or 2 questions that I thought were questionable / ambigous... Also the real exam questions were, in general, easier than the chapter review questions in the Kathy Sierra - Bert Bates book. If you have access to that book, I'd say that if you can answer most of the review questions in it without difficulty, you'll have no problem with the real exam.
A+, Network+, SCJP, SCWCD<br />preparing for SCBCD, SCEA, CompTIA I-Net+
Yield is a static method. In fact the Javadoc says that yield() Causes the currently executing thread object to temporarily pause and allow other threads to execute. So it doesn't matter if t is an instance and we say t.yield() v.s. Thread.yield(). It will always act on the "currently executing thread" no matter where we call it from. HTH
Joined: Dec 05, 2003
Peter, Just as you point out, yield only works on the current thread. This is the reason that I believe the question is poorly worded. It is not clear from the question whether t refers to the current thread or another thread. If t refers to another thread then all the answers are wrong. If the question had made it clear that t refers to the current thread then I would agree that answers 1 and 2 are correct.
This question is problematic and was recognized as such by Marcus himself a long time ago. He suggested that instead of "will" one read "can" for clarity. Another problem with it is that yield doesn't guarantee that the current thread actually will give up its cycles. I wonder why the questions don't get updated on his site.