File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusing in wait notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusing in wait notify" Watch "Confusing in wait notify" New topic
Author

Confusing in wait notify

Rufat Piriyev
Ranch Hand

Joined: Mar 15, 2010
Posts: 31
Hi All this code from Sirra&Bates

When I remove notify( ); in line 26 everything work correct Why?
PS. I started to hate these threads. First 8 chapters are not so complex
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

What else are you expecting?
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
what are you expecting and what is not working?


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Rufat Piriyev wrote:Hi All this code from Sirra&Bates
...
When I remove notify( ); in line 26 everything work correct Why?
PS. I started to hate these threads. First 8 chapters are not so complex


I should point out that the code is not exactly all from Chapter 9 of Sierra & Bates (K&B) book, although the differences are inconsequential.

Removing the notify() method in line 26 will cause a problem (not "everything work correct") because the main thread will stay in the wait state perpetually.
Leaving the notify() method in will let the main thread go to completion sometimes, depending on what the Thread Scheduler does. As K&B said, "this is somewhat complex". It is also very interesting piece of code.

SCJP 6
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Hi,

I totally agree with Rufat and his reason for asking the initial question.

What Rufat means is that even if you comment out line # 26, which is the "notify()" code, the code works fine. By that what he means is that even though the main thread does a

b.wait()

it is able to continue execution with the "notify()" code in line 26 commented out. To verify this yourself, cut and paste the above code and comment out line # 26 i.e. the "notify();" statement and try compiling and running the code. What you would expect is for the main() thread to wait indefinitely because it will never get a notify() from ThreadB. But surprise, surprise....the main thread does wait for ThreadB to complete, but resumes its execution even with the "notify()" code commented out. How come?

I tried this code and could verify that this is indeed the case and I am breaking my head on why the main thread doesn't wait indefinitely even if you comment out notify().

Also I am sure the main thread and ThreadB are synchronizing on the same ThreadB object (Thread b = new ThreadB()). So logically going by the wait(), notify() rule in java, the main thread should continue to wait indefinitely if the notify() code at line # 26 is commented out. But it DOESN'T. Why? To me this seems to violate the basics of the wait() and notify() rules as speficied in the JLS i.e. wait() Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. Remember there is no timeout specified inside the wait...it is a simple wait()

We need some expert's comment on this. Thanks.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Larry Olson wrote:What Rufat means is that even if you comment out line # 26, which is the "notify()" code, the code works fine. By that what he means is that even though the main thread does a

b.wait()

it is able to continue execution with the "notify()" code in line 26 commented out. To verify this yourself, cut and paste the above code and comment out line # 26 i.e. the "notify();" statement and try compiling and running the code. What you would expect is for the main() thread to wait indefinitely because it will never get a notify() from ThreadB. But surprise, surprise....the main thread does wait for ThreadB to complete, but resumes its execution even with the "notify()" code commented out. How come?

I tried this code and could verify that this is indeed the case and I am breaking my head on why the main thread doesn't wait indefinitely even if you comment out notify().


If you comment out line #26, the code will work fine in the sense that it will compile and run. But the only output will be:
Waiting for b to complete...

because the main thread is perpetually waiting to be notified. Are you telling us that you are getting more output than what is shown above?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Basically, what is happening is that you are waiting on the same object used internally to implement join(). The join() method is implemented internally by doing a check on the isAlive() and doing a wait() on the thread object. When a thread completes, one of the tasks, after changing the thread to no longer be alive, is to send a notifyAll() on the thread object.

Or in other words, don't use the Thread object for wait() and notify() -- it is already being used.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Are you telling us that you are getting more output than what is shown above?


YES, YES, YES, YES..............YES. That is what I am saying, in case you weren't clear even after reading my clear reply above.

YES, YES, YES....the main thread continues to execute and DOES NOT wait indefinitely even if you comment out the notify().

The main thread DOES NOT wait indefinitely even if you comment out the notify().

Is that clear enough?

Cut and paste the code, compile it and run it for yourself by commenting out the "notify()" and verify this for yourself.

Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Larry Olson wrote:
Cut and paste the code, compile it and run it for yourself by commenting out the "notify()" and verify this for yourself.


I already pasted the output of the code execution in the previous message. What makes you think I did not compile and run it. Why don't you show us what output you got?
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Henry Wong wrote:
Basically, what is happening is that you are waiting on the same object used internally to implement join(). The join() method is implemented internally by doing a check on the isAlive() and doing a wait() on the thread object. When a thread completes, one of the tasks, after changing the thread to no longer be alive, is to send a notifyAll() on the thread object.

Or in other words, don't use the Thread object for wait() and notify() -- it is already being used.

Henry


But I am not the one who wrote this code. This code has been presented as an example in the KB book for SCJP 1.6. This is in Chapter 9, page 748. . So this seems to be a flawed example they have given for wait() and notify().

But I think your explanation makes sense. So even if you comment out notify(), the thread does a notifyAll() anything waiting on that thread object. I hope I am understanding this correct.

But in this particular example you have to have the b.wait() call to get the correct result, even though the notify() call seems to be optional from what I understand.

Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
I already pasted the output of the code execution in the previous message. What makes you think I did not compile and run it. Why don't you show us what output you got?


What made me think? It is your question...i.e. you had asked if the main thread doesn't wait indefinitely even after you comment out notify. If you had compiled and run the code after commenting out notify() you would have seen that the main thread doesn't wait indefinitely, but finished. If that was the case you most probably wouldn't had asked for that confirmation again.

OK, here is the output I get, when I run the code after I comment out notify().


> java ThreadA
Waiting for b to complete...
Tota is: 45

>
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Nope, with line #26 commented out all I get is:
Waiting for b to complete...

like I said before, the main thread is waiting perpetually.

You need to understand that other posters cannot see what is on your screen. That's why I asked for direct and exact confirmation.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Larry Olson wrote:
But I am not the one who wrote this code. This code has been presented as an example in the KB book for SCJP 1.6. This is in Chapter 9, page 748. . So this seems to be a flawed example they have given for wait() and notify().


Which is why you should have done a search first... this question seems to come up often. And generally, related to this example.

Henry
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Henry Wong wrote:
Larry Olson wrote:
But I am not the one who wrote this code. This code has been presented as an example in the KB book for SCJP 1.6. This is in Chapter 9, page 748. . So this seems to be a flawed example they have given for wait() and notify().


Which is why you should have done a search first... this question seems to come up often. And generally, related to this example.

Henry


Search first where? Could you please point the page or link that contains the information that already explains this on the JavaRanch forums somewhere? Sorry, I am not clear on what you are saying, since you haven't pointed to an existing discussion of this issue.
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Larry Chung wrote:Nope, with line #26 commented out all I get is:
Waiting for b to complete...

like I said before, the main thread is waiting perpetually.

You need to understand that other posters cannot see what is on your screen. That's why I asked for direct and exact confirmation.


For me that isn't the case. i.e. the main thread doesn't wait perpetually even if I comment out notify. So it means we are indeed getting different results. I am using jdk 1.6.0_19 on Windows Vista platform and this is the behavior I could confirm. What jdk version are you using? BTW did you read the reply/explanation by Henry Wong? His explanation seems to make sense and it does confirm to my observation and verify it. But not sure why you are seeing the opposite i.e. perpetual wait...beats me.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Larry Olson wrote:
For me that isn't the case. i.e. the main thread doesn't wait perpetually even if I comment out notify. So it means we are indeed getting different results. I am using jdk 1.6.0_19 on Windows Vista platform and this is the behavior I could confirm. What jdk version are you using? BTW did you read the reply/explanation by Henry Wong? His explanation seems to make sense and it does confirm to my observation and verify it. But not sure why you are seeing the opposite i.e. perpetual wait...beats me.

For the sake of comparing notes, I ran the code on my computer here at work, which has jdk 1.5.0_20 on a Mac OS X platform. I'll try it on another platform when I get home tonight and see what results I get there.

Do a keyword search on "page 748" and previous forum threads will come up with Henry's explanations as well that do make sense despite the results I got.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Larry Olson wrote:
Search first where? Could you please point the page or link that contains the information that already explains this on the JavaRanch forums somewhere? Sorry, I am not clear on what you are saying, since you haven't pointed to an existing discussion of this issue.


There is a search link on top of this page, right next to the FAQ link. Just did a quick search myself, and here are the related hits...

http://www.coderanch.com/t/487066/Threads-Synchronization/java/wait-notify-notifyAll
http://www.coderanch.com/t/483920/Programmer-Certification-SCJP/certification/Thread-synchronization
http://www.coderanch.com/t/480030/Programmer-Certification-SCJP/certification/with-wait-notify
http://www.coderanch.com/t/476444/Programmer-Certification-SCJP/certification/another-from-nikos
http://www.coderanch.com/t/269028/Programmer-Certification-SCJP/certification/notify-vs-notifyAll
http://www.coderanch.com/t/268246/Programmer-Certification-SCJP/certification/Thread-wait-notify
http://www.coderanch.com/t/267020/Programmer-Certification-SCJP/certification/Notify
http://www.coderanch.com/t/263680/Programmer-Certification-SCJP/certification/notify-wait
http://www.coderanch.com/t/260532/Programmer-Certification-SCJP/certification/About-notify-notifyAll-Thank-you
http://www.coderanch.com/t/260020/Programmer-Certification-SCJP/certification/Thread-interaction
http://www.coderanch.com/t/254231/Programmer-Certification-SCJP/certification/wait-notify

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Larry Chung wrote:
Do a keyword search on "page 748" and previous forum threads will come up with Henry's explanations as well that do make sense despite the results I got.


Keep in mind that this is an implementation detail. There is nothing in the specification that says that join() is implemented this way, and hence, it may not. It is possible that this behavior may be different with different JVMs, on different platforms, etc.

Henry
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Right, thank you for pointing that out, Henry and that's what makes this code example (and threading in general) so intriguing. With the join() method being an implementation detail and the Thread Scheduler being an implementation detail we could have a perfect storm of a non-portable application that is wildly unpredictable.
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
Yeah, thanks for the explanation, discussion and links. This was a good learning and useful discussion. Yeah, I agree this could make java applications un-portable to some extent. But i guess that is a small price to pay considering the other benefits that Java offers.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
For posteriority, so that future generations might know where this thread came from, I ran the following code verbatim from Chapter 9, page 478 of K&B:

on a Windows XP platform with Java version 1.6.0_17, the result was:
Waiting for b to complete...
Total is: 4950

whether or not notify() was removed from the code. Certainly, this is a fine example of variations in implementation detail of join().
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
Henry Wong wrote:
Basically, what is happening is that you are waiting on the same object used internally to implement join(). The join() method is implemented internally by doing a check on the isAlive() and doing a wait() on the thread object. When a thread completes, one of the tasks, after changing the thread to no longer be alive, is to send a notifyAll() on the thread object.

Or in other words, don't use the Thread object for wait() and notify() -- it is already being used.

Henry


Sir i want to know if this is the concept i want to ask one question, if you don't mind
Sir,
As we know that before the termination of main thread it waits for the other threads to terminate.. ie.. according to you it will implicitly call join() on each thread object.... that's right

also sir

this code..

This can result on

wait..
as because it is not guaranteed that every time 1 will get first cpu time to execute.... It may happen that 2 get first and ten it will wait... because ... you are calling wait on the thread that has already terminated

Lets see another modification of this code..


This will result in output

here 1
here 2
here 3
here 4

waiting for b to complete..
....................

SCJP1.6,SCWCD1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusing in wait notify