• 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

How to make yield() work as expected

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sierra/Bates says that yield() might not make any difference in the code. However, I am hard-pressed to find any examples that use yield() so that one can understand this method.

Below is code I created based on Sierra/Bates.



Again, the output of t1 and t2 is interleaved. I was expecting t2 to yield to t1, i.e. t1 will execute first, then t2.

Please explain how to modify this code to enhance one's understanding of yield().

OR

Please point us to code that demonstrates usage of yield()


So much studying, so little time!!!
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't expect a ordered output with yield() method. yield() method gives a chance to the same priority threads to execute, but the same thread also can be chosen for executing! When you call yield on a thread, it came to Runnable state from Running state, and will compete to get the chance with other threads!
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see how yield can affect a way the code is executed, try the same program on different platforms with varoius number of processors,
esspecially with only one cpu/core.

Here is a simple ping-pong demo played on different "tables" that illustrates effect of yield


I ran this code first on Linux / amd cpu with 2 cores.
First with yield() commented:

Linux - 2 cores - without yield()
The second run with uncommented yield() in line 40

Linux - 2 cores - with yield()


Now let see how this program will run on Windows Xp only 1 cpu:

Win XP - 1 cpu - without yield()

Win XP - 1 cpu - with yield()


Pay attention how this program behaves on Win XP with single cpu - even when we have many threads,
a single thread without yield/sleep or wait() can grab a whole cpu time.
Unfortunately I have no Linux box with only one cpu/core.


 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ireneusz: The PingPong example helped me understand yield() considerably better than before.

Will try running PingPong program on JVM.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic