Threads: wait and resume methods.

Discussion in 'Java' started by Ben, Jul 26, 2006.

  1. Ben

    Ben Guest

    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
    Ben, Jul 26, 2006
    #1
    1. Advertising

  2. > 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
    Vincent van Beveren, Jul 26, 2006
    #2
    1. Advertising

  3. Ben

    cp Guest

    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();
    }
    cp, Jul 26, 2006
    #3
  4. "Ben" <> wrote in message
    news:ea7mfd$gae$...
    >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 http://www.iviz.com/
    Matt Humphrey, Jul 26, 2006
    #4
  5. Ben

    Ben Guest

    Matt Humphrey wrote:
    > "Ben" <> wrote in message
    > news:ea7mfd$gae$...
    >
    >>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 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
    Ben, Jul 26, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobias Merler
    Replies:
    0
    Views:
    434
    Tobias Merler
    Aug 25, 2003
  2. Renato Battaglia
    Replies:
    3
    Views:
    2,785
    Jon Skeet
    Oct 24, 2003
  3. eeyimaya
    Replies:
    3
    Views:
    552
    eeyimaya
    Feb 18, 2004
  4. Huey

    How to make "fork/wait" to WAIT longer?

    Huey, Mar 1, 2004, in forum: C Programming
    Replies:
    1
    Views:
    1,941
    -berlin.de
    Mar 1, 2004
  5. Kenneth McDonald
    Replies:
    5
    Views:
    297
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page