Question 44) 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) Thread.yield(); 3) yield(100); //Or some other suitable amount in milliseconds 4) yield(t);
Answer to Question 44) Objective 7.1) yield is a static method and causes whatever thread is currently executing to yield its cycles. 1) t.yield(); 2) Thread.yield() (Thanks Roseanne )
Such a behavior is quite possible, but not guaranteed as the same thread may be chosen to run by the scheduler after yielding. Therefore, I think having this question asking which of the following can cause it to give up cycles to allow another thread to execute is more appropriate. Marcus Green's exam 3, Question 44
In my opinion, it is possible that on some virtual machines neither "must" or "can" is appropriate. "On JVM implementations that employ pre-emptive scheduling policies, especially those on multiprocessors, it is possible and even desirable that the scheduler will simply ignore this hint provided by yield." Concurrent Programming in Java, Lea 184.108.40.206
I think that can is OK; it's vague enough either way. Maybe it will never happen on a particular JVM; maybe it will always happen on another JVM; either way, "can" is appropriate, IMO. There's another problem with this questin which I consider more serious:
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.
What's "it"? The thread represented by t seems most likely though that's not definite. The problem is, ifyou call t.yeild(), that isn't likely to have any effct on t at all - unless t just happens to also be the currently executing thread. I think that this is the sort of thing that the real exam might actually test you on - whether or not you know that yield() is static and therefore most likely has no effect on t. And the question is extremely ambiguous in this regard. The answer is less ambiguous, except that it does say that option 1 is correct - and in many cases, it's not. I suppose that if we add the word "can" as previously suggested, that might be vague enough to allow option 1 to be correct. But that's still misleading. The most likely scenario is that option 1 will have no effect at all on t.
"I'm not back." - Bill Harding, Twister
Joined: Mar 05, 2003
Good point, Jim. I ran across a question on a mock exam just today where the trick was about thread t1 invoking t2.sleep().
How about: Given a reference called t to a class which extends Thread, which of the following may cause the currently executing thread to give up cycles to allow another thread to execute? [ October 13, 2003: Message edited by: Thomas Paul ]
I think it should be changed to can, No comment on the other point? While I might agree with Marlene that Tom's suggested wording make the problem too easy, that's preferable to being wrong of misleading. Another possible wording is "which of the following can cause a thread to give up cycles..." or (going back to the other part) "which of the following will typically cause a thread to give up cycles..." who is this Marcus Green person? I think he's a myth. Who was that masked man? [ October 13, 2003: Message edited by: Jim Yingst ]