• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

yield() - method  RSS feed

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Concurrent Programming in Java (1.1.2.5), 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 ]
 
Miki Muzsi
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the answers. It is clear now.
Miki
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!