Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Threading Syncronization

  Asked By: Techguy    Date: May 03    Category: Java    Views: 607
  

I have a class set up (public class Update extends Runnable) which is
being instanciated from another class by

Update updater = new Update();
new Thread(updater).start();
wait();

and then in Updater there is a ActionListener
public void actionPreformed(ActionEvent event)
{
notify();
}

but when I run this thread, I get
java.lang.IllecalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
...

What am I doing wrong?

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Randy Warren     Answered On: May 03

I had this problem too. I got my own conclusions, but I don't know if is 100%
correctly!
I'll tell you my solution, if it's wrong  in any aspect someone correct me ok?

When a event occurs, its action run  at the Event dispath thread  to avoid that
other user action interrupt the event. So you can't unlock any other thread. In
my code, I've created another thread to notify().

public void  actionPreformed(ActionEvent event)
{
Thread theNewThread = new  Thread(new Runnable() {
public void run() {
notify();
}
});
}

Try this, use the code segment that follow to debug your program and understand
better in which thread it's running:
System.out.println(Thread.currentThread().getName());

Also, try to find some documentation about the event dispath thread.

I don't really know if this solution will work, but I hope that it help you, at
least, in some aspect.

 
Answer #2    Answered By: Frederick Greene     Answered On: May 03

ok...that addresses how to resume, but I first need to firgure out
how to halt the main process (the parent) so the child will run.

 
Answer #3    Answered By: Kelly Bell     Answered On: May 03

wait(), notify() and notifyAll() should be called only if the current thread  is
the owner of the object, else it throws illigalmonitorstate exception

check out the java  API for wait  notify and notifyall in the object  class.


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.

 
Answer #4    Answered By: Angel Harris     Answered On: May 03

You should not have to halt the main process. Most systems support
time-slicing so you don't even have to worry about yield(ing) your
processes. If your system does not perform time-slicing, add
Thread.currentThread().yield() in your main thread  and that will give
the other threads a chance to run.

 
Didn't find what you were looking for? Find more on Threading Syncronization Or get search suggestion and latest updates.