This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please, help! I'm a bit confused :) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please, help! I Watch "Please, help! I New topic
Author

Please, help! I'm a bit confused :)

Tim Kamen
Greenhorn

Joined: Oct 21, 2002
Posts: 3
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."
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Please refer to the following discussion dealing with Thread.yield:
http://www.coderanch.com/t/239572/java-programmer-SCJP/certification/Thread-yield-Cowgirl


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Tim Kamen
Greenhorn

Joined: Oct 21, 2002
Posts: 3
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 ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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."
Tim Kamen
Greenhorn

Joined: Oct 21, 2002
Posts: 3
Oh, thanks again. That's exactly what I'd like to hear
 
Consider Paul's rocket mass heater.
 
subject: Please, help! I'm a bit confused :)