aspose file tools*
The moose likes Mock Exam Errata and the fly likes Marcus Green's Exam 3 #44 - yield() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Marcus Green Watch "Marcus Green New topic
Author

Marcus Green's Exam 3 #44 - yield()

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
I found this question in one mock exam.
Which is the correct form of using the yield().
1.yield();
2.t.yield(); //assuming that t is the instance of the thred.
3.yield(500);
4.Thread.yield();
5.try
{
yield()
}
catch(InterruptedException e)
{}
The answers given are 1,4. But i think 2 is also correct.
Can someone please clarify.
Thanks.
raja pratap
Greenhorn

Joined: Mar 21, 2000
Posts: 21
I think yeild() method cannot be called using an instance of thread.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
can you give me some explanation please.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Uma- you are right that t.yield() is correct. Since yield()is static you don't need an instance, but you can use one. (Try it raja, it works.) The actual object t is never really used - the compiler just looks up the declared type of the reference, and uses that as the class. So t.yield() is exactly equivalent to Thread.yield(), and it should be an answer.
On the other hand, I don't think (1) yield() should be an answer, unless the question states that it's called from a class which extends Thread. In general, you need to specify the class (directly or using an instance) to invoke a static method, as in "Thread.yield()" or "t.yield()". "yield()" by itself just causes the compiler to look in the current class for a method yield(), and it will complain if it doesn't find it.
What mock exam is this from? Let us know and we can move this into Mock Exam Errata.

"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Yes, The question is from marcus mock exam3. I changed the question. I couldn't remember the question exactly. Now i copied the question from marus site and posting it again. Take a look.
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) yeild()
3) yield(100) //Or some other suitable amount in milliseconds
4) yield(t);

The answer given is only 2. But i think 1 is also correct.
Any suggestions?
Thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
As discussed above - answer (1) is correct; answer (2) is not (unless the question is reworded slightly). Note that Marcus' Exam 3 is not "officially" released yet because Marcus is well aware there are errors he hasn't gotten around to fixing yet. But since Exam 3 is out anyway "unofficially", I'll go ahead and move this to Mock Exam Errata so people can find it.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
I have updated that question now. Because I did specify that the class extended Thread the t.yield() would compile (though it I think it is a slightly misleading example to give). And yield() on its own would work (though I had mis spelt it in my question).
But I was wondering what yield() on its own really means, is it the equivalent of this.yield()?
Threads, doncha love em. Thanks for pointing this out (and for Jim for emailing me). All and any corrections are greatfully received.
Marcus

SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Marcus- I can't yet see the modified question on your site, so I don't know if you have reworded the question, or just changed the answer. But if you haven't reworded the question, then yield() is not correct. You have stated that t is a reference to a class which extends Thread, but you haven't said that the code we're writing is within such a class. I.e. the following code fits your problem statement:
<code><pre>class MyThread extends Thread {}
class Test {
public static void main(String[] args) {
Thread t = new MyThread();
yield();
}
}</pre></code>
And this won't compile, because yield() is interpreted as this.yield() - the compiler looks for a yield() method in class Test, and can't find one, because Test does not extend Thread. t.yield() would compile, as would Thread.yield().
Actually, looking again at the original question, I'm now convinced there is no possible answer to the question as worded. The question asks what code will cause "it" to give up cycles, where "it" refers to the thread referenced by t. The problem is, there's no reason to think that t is the current thread - and the current thread is the only one that we can ever cause to yield(). The methods Thread.yield() and t.yield() may compile, but they don't necessarily cause t to yield. Nothing can, unless t is the curent thread.
Hmmmm... how about this:
Given
Given a reference called t to the current thread, which of the following will cause the current thread to give up cycles to allow another thread to execute? Assume this code is within a class which extends Thread.
1) t.yield();
2) yield()
3) yield(100) //Or some other suitable amount in milliseconds
4) yield(t);
Correct answers would be 1 & 2.
Or maybe this question would be better with some sample code instead?
Heh, writing a good question is harder than I realized.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green's Exam 3 #44 - yield()
 
Similar Threads
Threads
Marcus Green #3, Q44
Marcus Green #3 Question 44
yield();
Marcus Mock # 3 Help!!