aspose file tools*
The moose likes Threads and Synchronization and the fly likes K&B book example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "K&B book example" Watch "K&B book example" New topic
Author

K&B book example

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
The following code is given in K&B book in chapter Threads.
When i tried Running it....It is giving Exception, i am not understanding why it is giving ???



Runtime error :-

java ThreadTesting
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at ThreadB.run(ThreadTesting.java:23)
4950



Moreover i am also not understanding why it is being assumed that after b.start() main thread continues and synchronized(b) would be executed.
It may also happen that threadB executes first, gets the lock and calculates total and then synchronized(b) would get executed. If it may happen then what is the use of b.wait() ....infact threadB is already done and now no notification would also come from it. A full chaos....

Please anyone explain this wait() notify() confusion.

All hell broke Lose !!!


SCJP 6.0 96%

(Connecting the Dots ....)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Sahil Kapoor wrote:The following code is given in K&B book in chapter Threads.
When i tried Running it....It is giving Exception, i am not understanding why it is giving ???

a call to notify is outside a synchronized block.
Look here: http://java.sun.com/javase/6/docs/api/java/lang/Object.html#notify%28%29
Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.


Sahil Kapoor wrote:It may also happen that threadB executes first, gets the lock and calculates total and then synchronized(b) would get executed. If it may happen then what is the use of b.wait() ....infact threadB is already done and now no notification would also come from it. A full chaos....

Yes, you are right - it may happen. In this case wait() will hang and will wait forever.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

What would happen?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

it won't notify any other threads waiting to hold lock on that objects monitor then


http://plainoldjavaobject.blogspot.in
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B book example