File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Wait() Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Wait() Example" Watch "Wait() Example" New topic
Author

Wait() Example

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
Hi,
I tried an example for wait() after reading a tutorial.

The Example is as below:



On executing theabove I get the below error.

Exception in thread "main" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:474)
at com.aciworldwide.application.TestApp.main(TestApp.java:19)



Where am I doing wrong.What does the error mean how can I correct it?


Thanks,
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
wait() must be called from within a synchronized context and a thread cannot invoke wait() on an object until it owns that object's lock.
sarvesh meens
Ranch Hand

Joined: Mar 31, 2006
Posts: 43

* Thread should obtain the object's monitor before invoking wait() or notify() on it.
* Locks,synchronized blocks/methods are the techniques to acquire object monitor. Depending on application requirement,suitable technique should be adopted.
* wait() should always be matched with a notify(). If you get rid of java.lang.IllegalMonitorStateException in the above example by aquiring the object's(one) monitor,main-thread will wait for-ever.
* You may find this discussion helpful.
[ June 05, 2008: Message edited by: sarvesh meens ]
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
should work correctly, if you put that wait call inside a synchronized block
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Hi Dawn,

It is generally not a good idea to WakeTheZombies
In all possibilities the original poster is not really waiting for inputs anymore!


apigee, a better way to API!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wait() Example