Originally posted by Savio Mascarenhas:
Question 24)
Under what circumstances might you use the yield method of the Thread class
1) To call from the currently running thread to allow another thread of the same or higher priority to run
2) To call on a waiting thread to allow it to run
3) To allow a thread of higher priority to run
4) To call from the currently running thread with a parameter designating which thread should be allowed to run
The answer mentioned was (1).Is (1) fine ???.
Suppose when yield() is executed ,there is only one "lower priority thread" that is in the ready state . In such a case won'nt that thread run,irrespective of what priority it holds .
Pls give ur opinion .
The answer is obviously 1
1. whenever you call yield, you call it for the currently executing thread only. Once this thread yields, it will quit the running state and reach the ready state. So any thread that is in the ready state which has the same priority or higher priority will take over the CPU.
2. false. the yield() method does not take any parameters. So, it cannot call any other thread like yield(t). Also, if the thread t1 is running, you cannot have a statement t2.yield() within the run of t1. You can just have yield() which means, that thread whose run is being executed(t1 in this case) has decided to yield on its own ( some kind-heartedness that
)
3. false. When a thread yields, the new thread that takes over
need not always have a higher priority. It may be an equal priority thread also.
4. false -- as given in explanation of option 2
Regarding your final question, i think what you say is true. If the waiting thread is of a lower priority, if it is the only thread -- the process should be the current thread yields, goes to the ready state and then comes back to the running state.
Guess somebody more knowledgable regarding threads can ascertain that. Or probably we can try it with a program.
Hope this helps.
------------------
Regards,
Shree
[This message has been edited by shree vijay (edited December 09, 2000).]