Under what circumstances might you use the yield method of the Thread class A. To call from the currently running thread to allow another thread of the same priority to run B. To call on a waiting thread to allow it to run C. To allow a thread of higher priority to run D. To call from the currently running thread with a parameter designating which thread should be allowed to run. The answer given was A. But I think the answer should be A and C since in the API doc yield is describe as "Causes the currently executing thread object to temporarily pause and allow other threads to execute" Other threads could mean thread of the same priority and higher priority. At least, that's what I think.
I think the yield method is to allow another thread of same / lesser priority to run. The thread with the higher priority will anyway be taken care by the JVM.
Joined: Feb 08, 2000
Thanx for your reply Just For A Moment... But in Roberts and Heller p. 208, it says "Note that most schedulers do not stop yielding thread from running in favor of a thread of lower priority." So generally speaking, yield does not allow lower priority thread to run. In my opinion, a yielding thread would give thread of the same priority and higher priority the chance to run. I know what you mean by thread with the higher priority will be taken care by the JVM since the JVM will EVENTUALLY (but not immediately) allow the thread of higher priority to run. In my opinion, the yielding thread will make the higher priority to run much earlier than it is scheduled. In Thinking in Java, it defines yield() give control to other threads and some other books just define it just allowing other runnable thread to run. Now, I'm left with this question in my mind. Should I include the answer "To allow a thread of higher priority to run" and "To allow a thread of lower priority to run" in my answer if ever this question is asked. Need confirmation. I'm going to take the test soon.
Jerson This topic came up long back. The static yield() method in Thread class is for giving a chance for other threads of SAME PRIORITY. To allow a thread of higher priority to run. Allowing a higher priority thread to run is, not the goal/job of yield method. It is JVM's. Simillarly for lower priority threads. When a thread calls the yield() method it just gives the chance for SAME PRIORITY thread-mates. If none exists this again continues to run. Only ans a) is correct. Refer to these 2 discussions here and here. regds maha anna
[This message has been edited by maha anna (edited April 01, 2000).]