• 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

Synchronized problem

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ThreadDemo{
public static void main(String[]args) throws InterruptedException{
synchronized(ThreadDemo.class){
args.wait(12);
}
}
}

Why this program throw IllegalMonitorException?
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prasun,



Here in the line args.wait(12); the thread that currently executes the line says it would like to give away the lock it has on the object "args"...

But to give away the lock the thread should have had the lock, rite?

Here it doesn't. This is synchronized on the ThreadDemo.class and not on the object "args" ==> implies it has taken the class's lock and not the args-object's lock.

So without even having the lock, how can you give it away? ==> Hence the IllegalMonitorException

To get the args-objects' lock, replace synchronized(ThreadDemo.class) with synchronized(args)


Regards,
Vishwa
[ January 18, 2008: Message edited by: Vishwanath Murthi ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic