• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tim Kamen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer to the following discussion dealing with Thread.yield:
http://www.coderanch.com/t/239572/java-programmer-SCJP/certification/Thread-yield-Cowgirl
 
Tim Kamen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, thanks again. That's exactly what I'd like to hear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic