This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Marcus Green's Mock Exam 3 #44 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); Answers: 1 and 2 (!) Correct me if I'm wrong, but we have no chance to garantee that this call "will cause giving up cycles ..." www.javaprepare.com: Shot tips. ".yield() - if there're no threads of the same priority call is ignored."
Thanks a lot, Valentin! I got it time before . That thread is like a discussion, without strict and sole answer. Will you help me with my #44 in terms of exam, please? Did I miss something important in the question, or my answer is correct? [ October 21, 2002: Message edited by: Tim Kamen ]
Joined: Aug 26, 2001
The API for java.lang.Thread.yield states
Causes the currently executing thread object to temporarily pause and allow other threads to execute.
Moreover, the yield method is static, and thus, a reference of a thread is of no use. It is the scheduler's job to decide if the currently executing thread will yield the CPU to another thread or not. The effect of the yield method is really implementation-dependent as the API does not specify any specific behavior. It does not mention that only threads with the same priority will get the chance to run. The answers provided by Marcus are both correct, however, I would rephrase the question as follows: "Given a reference called t to a class which extends Thread, which of the following may cause the scheduler to allow another thread to execute."
Joined: Oct 21, 2002
Oh, thanks again. That's exactly what I'd like to hear