http://java.sun.com/docs/books/tutorial/essential/threads/priority.html Java tutorial states that "Threads can only yield the CPU to other threads of the same priority--attempts to yield to a lower priority thread are ignored." However this does not seem to be the case. I created two threads, one with higher priority then the other, both call yield in run() method, the results are intermingled, so it seems the yield to lower priority thread did occur. Any explanations are welcome.
[This message has been edited by huiying li (edited March 09, 2001).]
Hi Huiying, There are never any guarantees with threads The JDK for <code>yield()</code> says:
Causes the currently executing thread object to temporarily pause and allow other threads to execute.
It does not say wether the other thread must have the same, higher or lower priority ... just that it will cause the current thread to pause. In the end, which thread runs depends on the the way the scheduler is implemented on the local system. As Win98 uses time-slicing, it's possible a lower priority thread could run. On systems that don't use time-slicing, a lower priority thread may not run given a yeild(). Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform
Thanks Jane Many people have said that the questions on Thread are tricky on exam, so I had hoped that the Sun tutorials would help me to prepare for it. I have read Mughal's book. Do you have suggestions on what else to read up for the test?