Threads: wait and resume methods.

B

Ben

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
 
V

Vincent van Beveren

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();
}

You'll need to do the notify in the same synchronized block as the wait.
What I mean is that in synchronized (this) { wait() } the synchronized
makes sure that thread has the monitor for 'this'... wait() can be read
as this.wait(). The notify should be executed on the same object as
'this' refers too in the wait. The synchronized of the notify should be
on the object the notify is invoked on which is the same object the wait
is invoked on which is the same object the synchronized is applied to. So:

class X {

void waitOnMe() {
synchronized(this) {
wait();
}
}

void goOn() {
synchronized(this) {
notify();
}
}
}

class Y {

private X x = new X();

void goOnFromY() {
synchronized(x) {
x.notify();
}
}

void goOnInX() {
x.goOn();
}

}


Hope it helps,
Vincent
 
C

cp

You are not synchronizing on the notify() part. I'm not sure that may be all
but you definately need it.

try{
synchronize(instance of LinkQueueProcessor){
instance-of-linkqueueprocessor.notify();
}catch(IllegalMonitorStateException imse){
System.out.println("IllegalMonitorStateException thrown");
imse.printStackTrace();
}
 
M

Matt Humphrey

Ben said:
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 (e-mail address removed) http://www.iviz.com/
 
B

Ben

Matt said:
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...




Is this the linkFinder or brokenLinkFinder? To work, you must synchronize
on the same object.




At least print something.




This variable is being accessed by two different threads. It should really
be within a synchronized block to ensure timely updates




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.




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 (e-mail address removed) http://www.iviz.com/

Thanks for the help I got it working.

oh and broken link finder and link finder share the same run method,
that they inherit from LinkQueueProcessor. That's why I'm using the same
method to pause them. In reality they are in diffenent Threads, but I'm
only allowing one of those Threads to exist at one time. For now
anyways. I'm using this project to teach myself thread synchronization.

Thanks again.
Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top