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) yield();
3) yield(100); //Or some other suitable amount in milliseconds
4) yield(t);
Ans 1) and 2). Explanation -yield is a static method and causes whatever thread is currently executing to yield its cycles.
While selecting the options, I was very well aware that yield is a static method but I felt the question was not worded appropriately such that I could choose option 2 in addition to option 1 without any reservations. Some code to illustrate my point-
We are to choose an option such that thread t stops executing.
Now t is instantiated in the main method of another class B. So while yield at line 2 will compile, it will stop the main thread
from executing and not t. Whereas yield() in the run method of
A will work. The present wording does not specify where yield is being invoked from.
IMHO, the question has to be reworded to say something like-
'Given a reference called t to a class A which extends Thread, which of the following will cause it to give up cycles to allow another thread to execute, provided that you are attempting to invoke yield A's methods.'
Would like to hear your opinions on this.
Thanks and regards
Sajida
[This message has been edited by sajida kal (edited May 08, 2001).]