• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

yield () method

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following quote is from kathy sierra(SCJP1.5),Ch-9,page 698.

" even if yield() does cause a
thread to step out of running and back to runnable
, there's no guarantee the yielding
thread won't just be chosen again over all the others! "



Does it means invoking yield() method atleast move current thread from running to runnable ?

OR

yield() method may or may not move current thread from running to runnable?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharat,

Quote from the Java Language Specification chapter 17.9

Neither a sleep for a period of zero time nor a yield operation need have observable effects.

 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. So sleep() method with 0 time period and yield() method may not cause current thread from running to runnable , right ?

But what is following (it's from JLS, 17.9)??

It is important to note that neither Thread.sleep nor Thread.yield have any synchronization semantics. In particular, the compiler does not have to flush writes cached in registers out to shared memory before a call to Thread.sleep or Thread.yield, nor does the compiler have to reload values cached in registers after a call to Thread.sleep or Thread.yield.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing to remember here is that "yield" gives no guarantees eitherway. The specification states basically that nothing must be done, but that something may be done. My guess is that "yield" is so tightly connected to the scheduling methods of the VM, that nothing can be said for sure. The wording of the specification suggests to me that this spec is more like a strong suggestion to the VM/javac builders then a law.
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Thanks. Topic is terminated.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic