Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Beginning Java
IllegalMonitorStateException doubt
Deepak Borania
Ranch Hand
Posts: 45
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
GIven the following code :
public class watever{ private static Thread t1 = new Thread("Thread1"){ public void run(){ synchronized (watever.class){ try{ wait(100); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println ("Done"); } } }; public static void main(String args[]) { t1.start(); try{ t1.join(); }catch(InterruptedException e){ e.printStackTrace(); } } }
Can someone please tell me why does the above code result in IllegalMonitorStateException at runtime?
And please explain a little about synchronized code blocks. I just can't seem to get hang of them.
Thanks
Rob Spoor
Sheriff
Posts: 22815
132
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You synchronize on whatever.class, then try to get the lock on "this". You can only call wait, notify and notifyAll on objects you synchronize on.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
IllegalMonitorStateException from notify()
wait( ) method behind the scene ...
problem with wait()- notify()
wait() and notifyAll()
Thread -IllegalMonitorStateException and IllegalThreadStateException
More...