• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait() not syncronized on this but not throwing illegalMonitorStateException

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The above snippet of code doesnt throw illegalMonitorStateException eventhough the method is synchronized the obj on which wait() is called is not synchronized .Why it is not throwing exception.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why will it throw an exception. The method itself is synchronized. Suppose this method is called on an object obj, then the method will obtain a lock on that object and wait() will also be called on that object i.e. obj (or this inside the method)...
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically wait() is this.wait() you are calling on this.Shouldn't it be synchronized on this






 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the method declaration, it is synchronized



is similar to

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thiyagarajan Kamala wrote:Basically wait() is this.wait() you are calling on this.Shouldn't it be synchronized on this



synchronized modificator for a non-static method synchronize on the instance of the class itself, on this object, so

is the same as
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Look at the method declaration, it is synchronized



is similar to



Precisely.

You might also want to think about what happens when the method is static
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well here the important to be noted is its not important for a snchronized block to be present whenever a wait is called but to acquire the lock on the object on which the wait was called.
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I did a small change to wait to synchronize on class level but the notification is not happening.Kind of confused ...asusual on this things......
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thiyagarajan Kamala wrote:
I did a small change to wait to synchronize on class level but the notification is not happening.Kind of confused ...asusual on this things......



if you will put some output in the block where you notifying, you will see that this block realy runs before the waiting block runs. So you first notify on object and then lock and wait on it. You put 1st thread to sleep 1000ms, then 2nd thread runs a bit and goes sleep 1000ms, whilst 1st wakes first and does it's notify evil thing. After that 2nd thread wakes and procedes to its wait, but in vain, no notifies left there for him.

 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anastasia I increased the sleep time and saw the difference.
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thiyagarajan Kamala wrote:Thanks Anastasia I increased the sleep time and saw the difference.


thank you too, it was a nice example for practicing
 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic