• 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

wait-notify

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All ,
I have some doubt regarding wait-notify calling.I want to express my doubt by using this simple example-



As you would see that I have one increment method in which I am incrementing the count.And out-put should be 20000.,if its synchronized code. So I want to know that how I can implement that synchronized block with wait(),notify implementation.Please clarify my doubt,as I am new to java especially threading concept.
Thanks in Advance
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Wait/Notify usage, you need to synchronize on shared monitor object. In your case, you are using one method but calling from different object.
I suggest to read more about Wait(), Notify() documentation.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to Ranch
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Khojaye wrote:For Wait/Notify usage, you need to synchronize on shared monitor object. In your case, you are using one method but calling from different object.


He actually only has one Runner instance, which is where all the work is done, so he is good there, he can use the Runner for synchronization.

What I don't get is what do you need wait() and notify() for? Wait and notify are for inter-thread signaling. If all you want is to make sure the count gets incremented safely, all you need is synchronization. Is there some reason you need signaling as well?
 
reply
    Bookmark Topic Watch Topic
  • New Topic