wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread synchronization and locks doubt. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread synchronization and locks doubt. " Watch "Thread synchronization and locks doubt. " New topic
Author

Thread synchronization and locks doubt.

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430



When i run the above program i get the following exception:



A couple of questions:

- Why the IllegalMonitorStateException? Because Test.sa is assigned to a new String object, i was expecting the ZiggyTest2 thread to wait indefinately because sa.notify() will be called on a different lock than the one used in ZiggyTest2.

- In the above example, wait() & notify() are called on the "sa" object. What is the difference in say calling notify() on its own and calling notify()/wait() using an object i.e. sa.wait() and sa.notify() or just calling notify(), wait()?

- Does it matter that in the Test class the synchronised block has the lock for the sa object and the sa object is static but in the ZiggyTest2 class, the synchronised block uses the same sa object reference but using a non static reference? Given that one is static and the other is not, will they both still be using the same lock?





Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

O. Ziggy wrote:
- Why the IllegalMonitorStateException? Because Test.sa is assigned to a new String object, i was expecting the ZiggyTest2 thread to wait indefinately because sa.notify() will be called on a different lock than the one used in ZiggyTest2.


You need to own the lock that you call wait() and notify() on. And locks are based on objects, not references.

So.... hint... is this a case of using the same object for synchronization and calling the notify method?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Henry Wong wrote:
O. Ziggy wrote:
So.... hint... is this a case of using the same object for synchronization and calling the notify method?
Henry


No it is using a different object. Thanks got it..
mukhan myrzakulov
Greenhorn

Joined: Jan 16, 2008
Posts: 13
Henry Wong wrote:
O. Ziggy wrote:
- Why the IllegalMonitorStateException? Because Test.sa is assigned to a new String object, i was expecting the ZiggyTest2 thread to wait indefinately because sa.notify() will be called on a different lock than the one used in ZiggyTest2.


You need to own the lock that you call wait() and notify() on. And locks are based on objects, not references.

So.... hint... is this a case of using the same object for synchronization and calling the notify method?

Henry


Hi, once the lock was aquired on an original String object ("Not Done"), "sa" was assigned to a different String object ("Done"), since String is immutable, "sa" now references to a different object.
IF I comment out the line "sa = new String("Done")" the program runs without IlligalMOnitorException.

Now if I change the type of "sa" from String to Integer, changeing the value in the Main method's synchronized block still throws an IllegalMonitorException. Why would it do that ? I just have changed value of the Integer object...

Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
synchronize(obj1) -> at this point you acquire the lock of object referred by obj1.
notify needs to be called on the object which is being locked. but when you assign a diff object to the reference obj1 and invoke notify,
the jvm tries to notify threads waiting for object refference by obj1, which is a different object now. probably there would be no thread
waiting for this object. Hence you get IllegalMonitorStateException.
mukhan myrzakulov
Greenhorn

Joined: Jan 16, 2008
Posts: 13
Larsen Raja wrote:synchronize(obj1) -> at this point you acquire the lock of object referred by obj1.
notify needs to be called on the object which is being locked. but when you assign a diff object to the reference obj1 and invoke notify,
the jvm tries to notify threads waiting for object refference by obj1, which is a different object now. probably there would be no thread
waiting for this object. Hence you get IllegalMonitorStateException.

On line 11. (sa = 2), why would it assign reference sa to a new Object? Integer objects are mutable. i.e. I would expect not assign 'sa' to a new Object, but instead just change the int value of the object 'sa' already referencing...

Thanks.
Larsen Raja
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Wrapper objects are immutable. Please refer page-239 on K&B book.
 
wood burning stoves
 
subject: Thread synchronization and locks doubt.
 
Similar Threads
Thread - Wait Doubt
Threads Exam 2 D.Chisholm
Threads again
IllegalMonitorStateException
Dan Chisholm Thread Mock test Q