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).]
Joined: Mar 22, 2001
Could any of the moderators answer my question ? This forum hasn't seen a lot of activity the last few days..all quiet on this front? Thanks! Sajida
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(); If "t" needs to give up cycles to allow something else to run then "t" must be running, n'est pas? Remeber yield() always works on the currently running thread! Take this example: object A wants to force object b to yield() so it issues a b.yield(). Guess who ends up yielding? Object a!!! Why? Because in order for object a to issue a yield() it must be the currently running thread!!! So 1 works because yield() can be issued from any object that extends Thread and 2 works because you can always use an object reference to run a static method. 3 and 4 do not work because yield() is not overloaded to take those parameters.
Hi Thomas, Thanks for the reply. The usage of yield is difficult to view practically thanks to how the JVM implements it, so my understanding of it is still a bit shaky. Would really appreciate your help if you could also clear my doubts. you have said
object A wants to force object b to yield() so it issues a b.yield(). Guess who ends up yielding? Object a
So in my example posted earlier, the command t.yield() issued in main actually translates to yield issued on the main thread, since main is currently executing. yup, that makes sense. okay, so now i know that if i want to stop t, i can invoke t.yield() only in the code which t is executing. The same would apply to yield(). But my question of WHERE the methods are invoked from in answering the question still remains. The question assumes that we will invoke t.yield or yield from an instance method of t, but if do this from say main, then it asks the thread running main to yield. All it says is given a reference to t to a class which extends thread etc etc..So I could have the reference to t in my main method also, right ? Am I missing some subtle language reference here which says that the methods are only being invoked by code which t is running ? Thanks and have a nice day! Sajida