Re: Threads: wait and resume methods.
"Ben" <bescott1@vt.edu> wrote in message
news:ea7mfd$gae$1@solaris.cc.vt.edu...
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:
You can invoke notify only on an object for which you already hold the lock.
You acquire the lock by synchronizing on that object, as in
synchronized (linkFinder) {
// Change state to pause / continue
// notifyAll
}
Your wait is within a loop that checks the waiting condition--this is as it
should be because the thread may be awakened for reasons other than notify.
It is also frequently better to use notifyAll rather than notify, especially
if you're using the main objects themselves on which to synchronize rather
than private locks.
more below...
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) {
Is this the linkFinder or brokenLinkFinder? To work, you must synchronize
on the same object.
while (pause)
{ System.out.println("paused...");
boss.displayStatus("Paused...");
wait(); }
}
} catch (InterruptedException e) {}
At least print something.
process();
}
System.out.println("Done.");
boss.displayStatus("Done.");
}
The method, in a different Thread, that calls the notify method:
public void continu()
{
paused = false;
This variable is being accessed by two different threads. It should really
be within a synchronized block to ensure timely updates
LinkQueueProcessor.getMonitor();
This isn't how you get a monitor. I think at this point you may want to
check out
http://java.sun.com/docs/books/tutorial/essential/threads/multithreaded.html
You need to begin to think about what happens to objects (memory) that are
accessed from two different threads at the same time so as to ensure that
the accesses do not conflict.
if (checkerStarted)
{
checker.toggleState();
brokenLinkFinder.notify();
}
if (finderStarted)
{
finder.toggleState();
linkFinder.notify();
}
You're tying to continue two different processes here and have two different
locks. Each notify must take place while owning the lock, but you should
also be modifying the pause state only within the appropriate lock also. Is
the paused variable is used by the link finder and the broken link finder?
Are they two different threads? If they are, you'll probably be better off
by completely separating them--give them each their own flags and
encapsulate their pause / continue and thread handling within the object
itself or within a task-handling wrapper.
Proper synchronization can be daunting and difficult to get correct. There
are some excellent books out there (Doug Lea's Concurrent Programming in
Java) that can show you the details of how this works.
Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/