Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Threads: wait and resume methods.

Reply
Thread Tools

Threads: wait and resume methods.

 
 
Ben
Guest
Posts: n/a
 
      07-26-2006
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
 
Reply With Quote
 
 
 
 
Vincent van Beveren
Guest
Posts: n/a
 
      07-26-2006
> 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
 
Reply With Quote
 
 
 
 
cp
Guest
Posts: n/a
 
      07-26-2006
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();
}


 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      07-26-2006

"Ben" <(E-Mail Removed)> wrote in message
news:ea7mfd$gae$(E-Mail Removed)...
>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/tutor...ithreaded.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 http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
Ben
Guest
Posts: n/a
 
      07-26-2006
Matt Humphrey wrote:
> "Ben" <(E-Mail Removed)> wrote in message
> news:ea7mfd$gae$(E-Mail Removed)...
>
>>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/tutor...ithreaded.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 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
exception throw and handle and resume Rahul C++ 4 12-08-2007 08:23 PM
Cisco VPN fails after suspend/resume and causes Matlab execution failures Dan Kohn Cisco 0 04-04-2005 08:30 PM
Break and Resume? Ron ASP .Net 8 09-30-2004 01:49 PM
How to make "fork/wait" to WAIT longer? Huey C Programming 1 03-01-2004 02:01 PM
Redirection and resume page execution Alex ASP .Net 0 01-20-2004 06:07 AM



Advertisments