• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

yield()??

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm somewhat confused about yield(). I know this question might have appeared here earlier. 1.4 thread API says:
"public static void yield()Causes the currently executing thread object to temporarily pause and allow other threads to execute."
This means that it will definetely go into runnable state from the running state. However an earlier discussion over here says that it is just a hint to the jvm that it may schedule other threads if it wants.
Also, I have read somewherer (don't remember where) that the thread goes into the runnable state and then jvm looks at the priorities of the thread already waiting there and then picks the thread for execution. So how I interpret this is that at least for once the running thread will go into runnable state and may come back again to running state immediately if there are no other threads with higher priority but that is indeterminate i.e. jvm dependent. However one thing is for sure that the thread will defintely pause for a moment i.e. goes to runnable state.
Am I right? Java Gurus, your input would really help.
Thanks,
Deep
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi:
I think you are right in your explnation, however when it comes to Threads there is no guarantee. It is all up to the schaudualer. Hence, the running thread is not necessarly give up its slot for another thread, specially if there are no other threads with simillar or higher poriority.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes yield will cause the current thread to leave the run state, but there is no guarantee that it won't be the thread chosen to run next so essentially it is possible for the call to yield to have no effect. Again it is all jvm dependent.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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"
The Java Programming Language 10.6.1, Arnold, Gosling, Holmes
"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 simply ignore this hint provided by yield." Concurrent Programming in Java 1.1.2.5, Doug Lea
I understand this to mean, it depends on the implementation of the JVM whether a thread changes state.
 
reply
    Bookmark Topic Watch Topic
  • New Topic