Kathy clearly states in her book that the yield() method MIGHT put the current running Thread in a Runnable state (or ready-to-run state), but even if it is put there, it might get selected immediately by the thread scheduler. (There is also a question about it in the tests). But IT IS NOT NECESSARLY that the Thread will exit the running state! Other books (like the one of Khalid Azim Mughal) it clearly state that yield() puts the current Thread into the ready-to-run state (and that eventually get again selected). I have also checked JLS (1.0) and it states that yield() "causes the current thread to yield(), allowing the thread scheduler to choose another runnable thread for execution". Question: is Kathy mistaken? Or am I looking to the wrong JLS? Did JLS change? I have looked for yield() in JLS version 1.0 (because in JLS version 2.0 - I couldn't find). Thanks
Certain behavior of threads is similiar to concepts in garabage collection, in the sense that there is "no guarantee" that certain things will occur. yeild is just a suggestion to yield, to my knowledge. The yield method COULD put the currently executing thread in the Ready-To-Run state and give way for other threads to execute, or maybe it will never leave the Running state because there are no other threads competing for the cpu, or maybe all the other threads are low priority or something. As I'm sure you've read in your book as well, it all depends on the JVM implementation. A method like yield, it seems to me, should be used to suggest a yield, but you can't count on what the behavior will be. [ June 30, 2003: Message edited by: Brian Joseph ]
In Concurrent Programming in Java (184.108.40.206), Doug Lea says Thread.yield is a purely heuristic hint advising the JVM that if there are any other runnable but non-running threads, the scheduler should run one or more of these threads rather than the current thread. The JVM may interpret this hint in any way it likes. On JVM implementations that employ pre-emptive scheduling policies, especially those on multiprocessors, it is possible and even desirable that the scheduler will simple ignore this hint provided by yield. In The Java Programming Language (10.6.1), Arnold, Gosling and Holmes say yield Provides a hint to the scheduler that the current thread need not run at the present time, so the scheduler may choose another thread to run. The scheduler may follow or ignore this suggestion as it sees fit--you can generally rely on the scheduler to "do the right thing" even though there is no specification of exactly what that is. [ June 30, 2003: Message edited by: Marlene Miller ]
posted 15 years ago
Thanks a lot for the answers. It is clear now. Miki
Miki<br /> <br />SCJP 1.4, SCBCD 1.3
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
global solutions you can do in your home or backyard