Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need for wait() and notify()  RSS feed

 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that always beats me it the need for wait and notify methods().
To synchronize code you may use synchronized methods and synchronized blocks, and after the exection of synchonized part the lock of the object is released,then why do i need to call a wait method in a synchorized block?
What is the difference between the lock getting released after the execution of the synchorized block and lock getting realease upon calling wait in the sychronized block?
thanks in advance
william
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the best answer to that is by an example. the known example for such cases is the consumer/producer example.
lets say you got a box with stuff in it.
the producer has its own thread and it produces a lot of merchandise.
now you also have a consumer thats buys this merchandise from the BOX in a diffrent thread.
now, as you realize, we must synchronize the produce() and the consume() methods because they both share the same box and we dont want both the consumer and the producer to acess the box at the same time (cause this will cuase problems).
but, what happens if the consumer tries to consume some merhcandise from the box when theer isnt any yet in the box.. what hapnes in the consumer consumes faster than the producer or the other way??? we ran into a problm.
to solve it we usually create a boolean flag thats indicates wheteher there is stuff in the box or not. if their is the boolean is set to true if there isnt the boolean is set to false.
now using the wait() and notify() methods we have no problem. if the consumer tries to cinsume and the flag is false (no stuff in the box) then he "waits" and releases the lock on the box.
he "waits" until the producer puts something in and then "wakes" or notify() the conosumer to wake up again. same goes the other way.
now we dont have to worry about those threads anymore. theyll wait and wake up when they should and release their locks while doing so.

hope it made any sense at all..
if not tell me and ill post here an example code for that.
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy,
Thanks for that reply.Yes what u say really makes sense.Yes if the the consumer checks the the bool flag indicating that the stack for things put by producer is empty it waits.Now my question is:
Why do i have to wait?why cant i return from the method as soon as I(consumer) find the stack empty.By returning from the synchornised method i will automatically release the lock.Which can be picked up by the producer.
Hope i have made my point clear.
Pl clarify
william
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok good question
the example i gave was simple, now lets make it more complicated.
lets say instead of 1 consumer there are now 5 consumers.
right now there is nothing at the box ok?
now the first consumer goes in sees there in nothing and exits, then the second then the third, NOW the FIRST one enters again, now the SECOND again and again the FIRST!!
as you may know, we have no control over which thread has cpu time. as soon as a thread goes to the ready state it can gain the cpu again and go to the synchronized method!
so it isnt very efficient if the same consumer goes lets say 5 times to check if there is something in the box before the producer puts it there.
we want every consumer to check ONLY ONCE if there is something in the box and if there isnt it will just WAIT till the producer puts something there and wakes him up.
when you have many threads you will see it makes it a lot more efficient and logical.
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey roy,
thanks again,and i completely concur with you on that.In fact I ran some example before i came to this conclusion.
"Wait and notify claim to be means of interthread communications.
How is this achived?
To the best of my knowledge there are two ways of releasing locks
1.I return from the sychornized method of the shared object
2.call wait method on the shared object
Both ways ensure the release of lock, no doubt, but in the former case the after the release of the lock the sequence of execution of code is rather unpredictable.
In the later case though when i call wait in a synchronised block, two things happen
1.The calling thread releases the lock.
2.The calling thread goes to sleep mode until some other thread calls notify or notifyAll.Therefore the code after the wait call in a synch method is not excuted and the code in the run method after the synch method call is also not executed(which is not the case when i realease the lock by virtue of returning from sync method)until notify is called.
I may therefore use wait notify to enable communication between my threads"
Pl correct me if i am wrong
william
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you got it william
couldnt have put it better myself.
just another last thing, notice that it is always a good practice to check the boolean variable in a while loop and not in if statement.
the reason for that is that when threads "wake up" ie. gets notified they continue from the place they started waiting.
now if there is only 1 thread thats no problem but if there are 5 waiting and you wake al of them using notifyAll() then every thread (at its own time when it gets the lock) will do the code inside teh synchronized method and WILL NOT check to see the boolean flag again (check to see if the box is empty after some consumers got items from there).
if you put the boolean flag checking in a loop, then every thread when he gets the lock on the synch method will check (just to be sure) the boolean flag again to see if it is ok to continue or should he wait again.
[ May 07, 2002: Message edited by: Roy Ben Ami ]
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey roy,
yup thats a real worthy tidbit u've mentioned.I'll keep that in mind.
Thanx again for the reply
william
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!