Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wait() Example

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* 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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should work correctly, if you put that wait call inside a synchronized block
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dawn,

It is generally not a good idea to WakeTheZombies
In all possibilities the original poster is not really waiting for inputs anymore!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic