• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Question about Yield

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I was trying something on yield() method , and found that behavior of the program is not what I expected. I'm sure, I must have mistaken the concept of yield, and now would like to know the science behind this.



As per K&B in page 712 on threads, it says yield() method would keep the locks and would not release them.

From the above statement, I assumed that, when the first thread enters run, and have a lock on this (MyClass) object, it increments value and would keep the lock with itself without releasing it (due to yield() method). This should prevent the second thread ,entering run and have a lock on this (as already being held with first thread). So I believed that value would be incremented only once and the value would be printed 11 some time later after Main thread is awaken. But this is not what happening . Value is incremented twice and 12 is printed finally in main method.

Where did I do a mistake in understanding this concept? Please some one clarify me on this.Thanks in advance.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Where did I do a mistake in understanding this concept? Please some one clarify me on this.Thanks in advance.



Well, assuming the yield() was even honored, what do you think will happen to the thread? Yield just means to give up the current timeslice. The other thread will block because it can't get the lock, and the main thread is sleeping, which thread do you think will run again?

Henry
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I got it now . I thought, it was the second thread that is entering into the Run. I never realized that the first thread can again take over the control. That was so stupid of me .


Thanks Henry, for making me understand the logic behind that
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic