aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait(), notify() doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait(), notify() doubts" Watch "wait(), notify() doubts" New topic
Author

wait(), notify() doubts

Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344


What does the above pgm is doing? This pgm is taken from K&B book and they given the explanation, but unfortunately I couldn't able to understand it(in the book I understood all the concepts they have explained, but some where struggling to understand wait(), notify() and notifyall()). Can anyone if possible explain the above pgm?

Before that I will tell what I have understood from the above pgm..

Two Threads are there. One thread is main and another thread is threadB, which is doing the calculation. Before calculations done at ThreadB, the main thread will output the final value which will be always 0.

Upto this I understood.Now inorder to avoid this wait and notify is used more or less like join()..am i correct. One thread is depend upon the calcualtion of another thread.. so we are forcing a calcualtion thread should run first, then main thread should continue.. like join does

So before the output is printed in the main thread without calaculation, we are asking the main thread to wait untill the ThreadB finishes it calaculation. But why the wait has been called with ThreadB instance? And synchronized on b object? And also whether notify() should be the last statement in the synchronized block/method..?


[ December 09, 2006: Message edited by: Micheal John ]

[ December 09, 2006: Message edited by: Micheal John ]
[ December 09, 2006: Message edited by: Micheal John ]

Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

As I see it...

calling b.wait is the same as saying "hey b, put me in your personal queue, and as long as you don't tell me to go away, I'll stay there and this thread is blocking!" , now another thread might also access b and call b.notifyall on b (and I hope it does or you main thread will be stuck forever). This means that b will kick all of the waiting threads of his queue and they can continue what they were doing.

For the first thread this means, trying to get his lock back which it released when it called the wait, and then continue.

If you now look at the code, you should find your own way out it think.
If you still don't, please post.

regards,
Mark


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
"hey b, put me in your personal queue, and as long as you don't tell me to go away, I'll stay there and this thread is blocking!"


I understood paritally upto I'll stay there.. what it's mean "this thread is blocking"


synchronized(b) - This means the main thread is acquiring the lock of ThreadB object. So no other thread can access ThreadB object.

b.wait() - This means main thread releasing the ThreadB object and asking ThreadB object to notify me once you have done with your work, so that main thread can cointinue/finish?
correct..?


For the first thread this means, trying to get his lock back which it released when it called the wait, and then continue.


Whether after the ThreadB given the notify(), the main thread has to get the lock of ThreadB object again..? if so..why?

After the ThreadB notify(), the ThreadB will be dead..correct and after the ThreadB given the notify(), where exactly the control will go to main thread?
[ December 09, 2006: Message edited by: Micheal John ]
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

Indeed, the current thread just blocks. It does not continue until it is notified (note that there is also an overloaded version that takes milliseconds to wait for notification OR the current time is over).

wait releases the lock on the object, otherwise it would be impossible for anyone else to call the notifyAll method on that object. This means that after the notify came, the lock has to be re-obtained, and it is possible that the thread has to wait to get its lock back because the other thread, the one who called the notifyAll on the object, is still doing stuff and doesn't feel like releasing the lock yet.

Also notice that join and wait are 2 different things. Join is a method of the Thread class, that blocks the thread until another thread is finished.

wait() is a method of the Object class that blocks the thread and waits for notification of the object its waiting on.

regards,
Mark
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: wait(), notify() doubts