This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem with wait()- notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem with wait()- notify()" Watch "problem with wait()- notify()" New topic
Author

problem with wait()- notify()

saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
The following code is from K & B Page 748 ( a little bit modified). I was trying to understand the concept of threads :



In the above code if i delete notify() from ThreadB class , i get the same output as the previous one .



and the output in both cases is
total = 45

According to me , the second code without notify() should add the ints but should not print the total and wait for ever because there is no notify(). But the code prints the total , just like the previous code which has notify();

can anyone explain this to me , what 's happening here ?


SCJP 6.0 (86%) SCWCD 5 (94%)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

saima kanwal wrote:
can anyone explain this to me , what 's happening here ?


Basically, you are encountering an implementation detail. The join() mechanism is using the thread object for notification. Threads that are waiting to join() call the wait() method on the thread object -- and when a thread terminates, one of the tasks that is done is a notifyAll().

This is an implementation detail, and can't be dependent to behave this way with all JVMs.

Henry


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

Joined: Oct 05, 2009
Posts: 148
Henry Wong wrote:
saima kanwal wrote:
can anyone explain this to me , what 's happening here ?


Basically, you are encountering an implementation detail. The join() mechanism is using the thread object for notification. Threads that are waiting to join() call the wait() method on the thread object -- and when a thread terminates, one of the tasks that is done is a notifyAll().

This is an implementation detail, and can't be dependent to behave this way with all JVMs.

Henry


What do you mean by implementation detail?? i simply cannot understand your explanation. where has join() come in this code ?? Do you mean to say that the code should run in the manner I expected( should wait forever) but it is due to my particular JVM's behaviour?? or what ?? please explain.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

saima kanwal wrote:
What do you mean by implementation detail?? i simply cannot understand your explanation. where has join() come in this code ?? Do you mean to say that the code should run in the manner I expected( should wait forever) but it is due to my particular JVM's behaviour?? or what ?? please explain.


An "implementation detail" is a design decision made during implementation, and not during specification, yielding an effect which is not intended.

In this case, when Sun implemented the join() method they used the Thread object to signal that the thread has terminated. Because of this, any other thread, that waits on the thread object (and not for join()), will have an unintended effect of getting an notification too.


In other words, don't use the thread object for the wait() method. If you do, you will be sharing it with the join() mechanism.

Henry
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
lets see it this way Saima,
In first code, when you call notify main thread is notified and acquires lock, I think this you understood.
Now, in second code you did not make a call for notify but as soon as the run method completes the thread releases its lock which is now available to all the threads waiting for that lock. I think I read somewhere at Java Ranch that if there is no notify in the run method then after the run method a call to notifyAll is implicitly made.


SCJP 1.6 96%
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
thanks Henry & Neha . I got it now
Joss Armstrong
Greenhorn

Joined: Jul 29, 2004
Posts: 17
Please can someone explain to me why the following code gives an IllegalMonitorStateException when the notify method is called?

N.B. It works correctly when a public Object o is instantiated in the Example1 class and this is used to synchronize on.



Thanks

Joss Armstrong


SCJP 6, OCPWCD JEE5, OCEWSD JEE6, OCMJEA
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Joss Armstrong wrote:Please can someone explain to me why the following code gives an IllegalMonitorStateException when the notify method is called?


Please don't hijack topics. Start a new topic for a new (different) question.

Henry
Joss Armstrong
Greenhorn

Joined: Jul 29, 2004
Posts: 17
I have created a new topic with this question
http://www.coderanch.com/t/480381/Programmer-Certification-SCJP/certification/IllegalMonitorStateException-from-notify#2155023

Joss Armstrong
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with wait()- notify()