Forums Register Login

Thread synchronization and locks doubt.

+Pie Number of slices to send: Send


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?





+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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..

 
+Pie Number of slices to send: Send
 

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...

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
1
+Pie Number of slices to send: Send
Wrapper objects are immutable. Please refer page-239 on K&B book.
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 954 times.
Similar Threads
Thread - Wait Doubt
Threads Exam 2 D.Chisholm
Threads again
Dan Chisholm Thread Mock test Q
IllegalMonitorStateException
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:29:36.