• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Synchronizing on main method arguments

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code below, in the main method I am synchronizing on the String array parameter of the main method. The code gives an IllegalMonitorStateException when trying to call wait on commented line.



I know that a thread has to be owner of the object on which it is synchronizing but in this case I am unable to figure out the reason of this exception as to how main thread is not the owner of the array.
Probably I could not understand the conditions as to when a thread owns an object's lock. Please clarify.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Being the owner" means "owning the synchronization lock" in this case. So you can only call wait() on an object inside a block synchronized on that object.

Here you synchronize on args, but you call wait() on e. The thread doesn't have a lock on e.
 
ujjawal rohra
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh yes I missed that the object on which wait is called should be the same on which the block is synchronized.

Thanks Matthew .
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic