Jump to content
  • Advertisement
Sign in to follow this  
Halsafar

[java] Java Pause a thread

This topic is 4594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay I got this class which extends Thread. I create an instance of this class, call start(), over-ride run() and it works. The thread runs fine and the main thread is free to continue. Now I need to have this new thread pause itself and wait for a notify call. Everytime I call .wait() I get a runtime exception: java.lang.IllegalMonitorStateException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:474) Now, obviously, I am trying to call .wait() in a thread which doesn't own the object (my guess). But I am calling .wait() from within the thread itself... I am confused. Anyone care to code up a quick sample which shows the use of .wait() and .notify()? Provide any tips? btw, I love the new gamedev look :)

Share this post


Link to post
Share on other sites
Advertisement
Use the API docs

Quote:

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

By executing a synchronized instance method of that object.
By executing the body of a synchronized statement that synchronizes on the object.
For objects of type Class, by executing a synchronized static method of that class.

Share this post


Link to post
Share on other sites
Quote:
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
... // Perform action appropriate to condition
}

and more precisselly this code.
Obj could be this. Of course you should call notify only... It would spoil the fun of debugging, if I would write it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!