aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread wait() and notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread wait() and notify()" Watch "Thread wait() and notify()" New topic
Author

Thread wait() and notify()

ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32
To understand wait() and notify(), I modified an existing Producer-Consumer pattern code. I got how threads communicate with each other but the output of the following code baffles me.



I expected the output to be:
Producer Put: 0
Consumer Got: 0
Producer Put: 1
Consumer Got: 1
Producer Put: 2
Consumer Got: 2

But, I get the following:
Producer Put: 0
Consumer Got: 0
Producer Put: 1
Consumer Got: 1
Producer Put: 2
Consumer Got: 2
Producer Put: 3
Consumer Got: 3
Consumer Got: 3

Questions:
1. Why does Producer keep puting even though I set the shared variable maxHit to true if the number is > 2, and no assignment is made? (Line 59)
2. Why are there 2 consecutive "Consummer Got: 3" at the end?

Thank you for all your help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Sounds more complicated than we usually have in beginners'. Moving.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Please don't think wait() and notify() are methods of the Thread class? They are not; they are final methods of the Object class.

And please never say == false or == true, which can lean to subtle errors if you mistakenly write =.

You should write

if (!maxHit && !valueFalse)

or use de Morgan's Law and write

if (!(maxHit || valueFalse))
ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32
I understand that wait() and notify() are methods of Object.

I agree with you on the "==" operators. I have changed my code.

I still cannot figure the results out though. Thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

1. Why does Producer keep puting even though I set the shared variable maxHit to true if the number is > 2, and no assignment is made? (Line 59)


The producer only adds once more than you expect. Think of the sequence which occurs:


So your condition for stopping is wrong if you want it to stop before it reaches 3.

2. Why are there 2 consecutive "Consummer Got: 3" at the end?

When the Consumer is done with getting the 3, the consumer thread maintains possession of the synchronizing lock long enough to get into wait() statement, before the producer has a chance to switch the maxHits flag to true. So the Consumer goes into wait() and the Producer has a chance to get hold of the synchronizing lock, switches maxHits to true, notifies the Consumer it is done, then ends.

The Consumer has a chance to get the synchronized lock again, and since Producer didn't increment sharedNumber it sees the value of 3 and reports it. The Consumer now sees the maxHits is true and the Consumer ends.

The sequence that occurs here is:


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Steve Luke:

When the Consumer is done with getting the 3, the consumer thread maintains possession of the synchronizing lock long enough to get into wait() statement, before the producer has a chance to switch the maxHits flag to true. ...


That statement isn't exactly true. It does give up the lock when the method comes to an end, but the Consumer thread continues to execute and regains the lock before the Producer Thread is scheduled to run and can try to gain the synchronized lock. The effect in this case is the same, but it wasn't really accurate so I am correcting myself.
ManChun Lam
Ranch Hand

Joined: May 04, 2008
Posts: 32
Thank you very much. You step-by-step solution really helps me understand the problem. May I ask you for 1 more favor? How do developers debug multi-threaded code (more than 2)? Tracing by "brain power" is certainly not doable when there are like 5 threads! Thanks again.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Well, the problems that you get with 3,4,5,+ threads are the same ones you get with 2 threads, so it is usually a good idea to start small (like you are doing) to work out the kinks, then build up from there. That makes it easier to work out the events as they happen.

Using a debugging tool with break statements and a profiling tool like VisualVM or JProbe make it easier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread wait() and notify()