Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread wait() and notify()

 
ManChun Lam
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds more complicated than we usually have in beginners'. Moving.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic