Threads: wait and resume methods.
I am trying to pause the execution of one thread till a particular
action happenes. When that action happens I use the notify method to
"wake up" my thread. Unfortunatly I always get the
IllegalStateMonitorException when I use the method notify. My question
is how do I become the monitor of the object that I need to wake up?
Here is some tidbits of code that may help you in answering my question:
The run method I try to notify: in class LinkQueueProcessor:
public void run()
{
linkQueue.addAll(parser.process());
pageParsed++;
while( !terminated && !linkQueue.isEmpty())
{
try
{
synchronized (this) {
while (pause)
{
System.out.println("paused...");
boss.displayStatus("Paused...");
wait();
}
}
} catch (InterruptedException e) {}
process();
}
System.out.println("Done.");
boss.displayStatus("Done.");
}
The method, in a different Thread, that calls the notify method:
public void continu()
{
paused = false;
LinkQueueProcessor.getMonitor();
if (checkerStarted)
{
checker.toggleState();
brokenLinkFinder.notify();
}
if (finderStarted)
{
finder.toggleState();
linkFinder.notify();
}
displayState();
}
The method LinkQueueProcessor.getMonitor(); is a synchronized static
method that does nothing. The only reason it's there is becaused I read
in the API that to become a monitor of an object I needed this:
"For objects of type Class, by executing a synchronized static method of
that class. (JAVA API, Thread API"
hopefully there is someone out there knowledgeable enough to help me out.
The purpose of those statement is to provide the user with a pause and
continue functionality.
Thank you for any help in advance.
Ben