Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hey Guys doubt on Wait(),notify() ans notifyAll().  RSS feed

 
lokesh koppaka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hii Guys,
I have written the following code for testing how does notify(),wait() works.But it seems that the code doesn't works properly.The problem is program makes infinite waiting,i call using notify but it doesn't works.Can anyone tell me why the problem is.

I had posted the code below.


****************************************************CODE**********************************************

******************************************************CODE******************************************
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lokesh, Welcome to the Ranch!

The notify() and wait() methods are instance members, which means they work on particular Objects. You have three different ThreadMe objects, t, t1, and t2. t calls wait() (which is implicitly this.wait()) and t1 and t2 call notify (implicitly this.notify()). Since t1 and t2 are different instances than t, the notify() they call doesn't affect the wait() called on t (just like setting the instance variable id on t2 to the value 3 doesn't change t's id - it is still 1).

In order to do interthread communication you need a common Object to share among them to do the wait() and notify() on and to synchronize on (as it is, the code appears synchronized but again, since the synchronized lock in use is per-Object each of your ThreadMe instances are using a different lock and are therefore not synchronized).

There are two ways you can go about doing this. One is using a single ThreadMe instance and using a ThreadLocal object to hold the id. This can be a bit difficult to setup this scenario so I would avoid it. The second, and in my opinion better, option would be to create an Object that you share among all the ThreadMe instances and synchronize on:


Note that in the main method() I create one arbitrary Object and pass it to all three ThreadMe instances. Then, in the incrementCount() method, I removed the synchronized keyword since it wasn't effective (different instances) and instead put all the code inside a synchronized block (using the Object as the source of the synchronization). Then I re-used the lock for both the wait() and notify() calls.


p.s. I added code tags to your post to make it easier to read. Please read UseCodeTags (<- that's a link) so you can see how its done!
 
lokesh koppaka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey steve Thanks got it!!! its a great explanation.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, glad I could help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!