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.