This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to make yield() work as expected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to make yield() work as expected" Watch "How to make yield() work as expected" New topic
Author

How to make yield() work as expected

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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!!!


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Feb 18, 2010
Posts: 434
@Ireneusz: The PingPong example helped me understand yield() considerably better than before.

Will try running PingPong program on JVM.
 
 
subject: How to make yield() work as expected
 
Similar Threads
Thread problem.
Will a started thread run to completion in this example?
join() method in Thread doesn't work as expected
Problem in understanding join()