Given a reference t to a class which extends Thread, which of the ff will cause it to give up cycles to allow another thread to execute 1) t.yield(); 2) yield() 3) yield (100) 4) yield(t); the answer given was 2 only i would think #1 is also a valid answer (if you don't consider the fact that 2 and 3 does not have semi-colons and won't even compile) since yield is a static method of the Thread class, couldn't it also be called/referenced to by an instance of Thread? and since t extends Thread then x instanceof Thread should return true. any comments?
Answer1 is correct answer in sense that it will compile and cause the same result as yield(). It is recommended to use yield() instead of t.yield() because last variant is misleading. When you call static method yield() and there are more than one threads, it will stop currently running thread (which, BTW, means a thread in which yield() call occured) - and it may be not "t" thread. But since the question asks "which of the ff will cause it to give up cycles to allow another thread to execute" - answer 1 is correct.
I agree, 2 would not compile. I also read this question to be that reference t IS a class that extends Thread given to some THING of unknown type. Even if we put a ';' at the end of 2, the class executing the statement would have to be of type Thread for the static 'yield();' to work. So I think 1 is the only correct answer. Otherwise, it should read "a class which extends Thread is given a reference to an object t,....". Then I can see 2 with a semicolon is correct.
I agree with Mapraputa. I got this answer wrong when I took the mock test coz I had answered both 1 & 2. Scott, you are right in a way, but I believe the question is really not intended to test the ';' aspect of the question. Therefore, we have to assume that Marcus indeed meant yield(); Also, in the real exam you do not have these kind of typos and the questions are (most of the time) very clear and precise. -sampath
Scott's remark was a bit unclear to me, but I think he means, and I think I agree, that simply writing yield(); won't work unless it is written in a class that inherits from Thread. As I understand it, the preferred way to invoke this method is by "Thread.yield();" which works anywhere and avoids all ambiguity. But that option isn't given in the question. Hmmm....
scott irwin
Ranch Hand
Joined: Aug 07, 2000
Posts: 87
posted
0
Jim, Correct. I could not tell from the wording if I (the class) was a thread or I (the class) was given a thread. I read it to be I was given a thread. Therefore, t.yield(); would be correct. If I was a thread, then a simple yield(); would work and the bit about giving a reference was just to cloud the question. Scott
marie m
Greenhorn
Joined: Sep 27, 2000
Posts: 7
posted
0
i understood this question to be t is a reference to a class that extends Thread, i.e., t = new ClassThatextendsThread();, which means t is an instanceof Thread so answer #1) t.yield(); though not recommended should be correct and if the statement is within the class declaration of t itself, then i can understand that 2 will also be correct... i wonder what Marcus has to say about this?
Hmm this question was supposed to be testing the knowlege that calling yield will cause the currently executing thread to give up cycles but I guess it doesn't do that very well, and I'll fix those semi-colons. How could it be phrased better? God I hate threads but they are important. I own the O'reilly/Oaks book on the subject and it is not very good. I have just been reading the reviews of another book on the subject at Amazon that gets good reviews (Hyde) Marcus
[This message has been edited by Marcus Green (edited September 28, 2000).]