Results 1 to 5 of 5
  1. #1
    georges is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default Wait() notify() implementation

    Dear Java lovers,

    I am so thankful for this forum. I am new to the java world and its been just 3 months.. I am currently facing a situation in my program and would really appreciate some technical assistance.


    I am using 3 classes. First called application.java contains the main method. The second class, Window.java, contains methods to firstly read a file using filechooser and then performing the necessary calculations and then a method to display some graphs. The final class, dialogClass.java, is a GUI which I would like to implement between the filechooser and the method which displays the graphs (because I want to display some information in the file on the GUI frame which would affect the charts display method) in the Window.java class. I realized that I had to use wait() in the window class right after calling the filechooser method and I tried to perform notify() in the ok Jbutton ActionPerformed event of the GUI frame in the dialogClass.java. But I got an error called illegalmonitorstateexception . I am stuck!!!

    Do you have any solution or any better way to implement this??

    Thanks

  2. #2
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    The exception is thrown most likely because you have called either wait or notify outside a synchronized block.

    Here is a quote from the javadoc i just googled.
    Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.
    Java Code:
    synchronized(object){
    object.wait();
    object.notify();
    }
    Thread synchronization is not a simple task. You should have a read of the java tutorials to get a firmer understanding of the basic design patterns behind using wait and notify.

    In general the rules of thumb i use are for every wait call in the program there should be a corresponding notify in another thread.

    Every wait call should be enclosed in a while loop in case the thread is signalled before it reached the wait call. (this will manifest itself with the waiting thread being stuck at the wait and your program seemingly doing nothing).
    Java Code:
    synchronized(sharedObject){
    while(!signalled){
    sharedObject.wait();
    }
    /* reset the variable so that other thread and this thread know the signal has been caught */
    signalled = false;
    /* do work in mutex zone */
    }
    /* exit mutex zone thread no longer has the sharedObjects lock */
    The signalling thread needs to have something like this:
    Java Code:
    synchronized(sharedObject){
    signalled = true;
    sharedObject.notify();
    }
    It would be nice if you could throw up some code, as what you have described doesn't sound like you need to use wait() notify() as it does not mention that you a running concurrent threads.

    Hope this info helps

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    you can't just call wait and notify on an object outside of a synchronized context.
    for example, either the method that invokes these needs to have synchronized in as a modifier, or you wrap the wait inside a synchronized block.

    the wait can be on any object, as long as it is teh same ojbect later we notify.

    in the window,
    Java Code:
      MyObject obj = new MyObject();  // anything not null
    
      void someFunctionToShowTheDialog() {
        // show dialog here
        shwoDialog(obj);  // the dialog needs a reference to this object instance this thread has called wait on. - oh yea, we better hope the dialog object actually is running in its own thread.
       synchronized(obj) {
         obj.wait();
      }
    maybe the dialog:
    Java Code:
      theokbutton() {
        synchronized(obj) {  // because the dialog has the reference to the object we want to notify that was previously waited, right.
         obj.notify();
     }
    Edit: D'oh, too slow in my response.

  4. #4
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    never mind it still is correct travishein.

    But what do you think, I get the impression the op doesnt need to solve the problem with wait and notify. No mention of multi threading in the three classes he has put up. I would suggest some kind of event listener will do the job.

    So window listens for dialog events. When the dialog gets the information it firesevent, caught by window and window updates the graphs.

  5. #5
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    yea, i didn't see any mention of creating the dialog in a separate thread, i mean the default Swing processing thread won't work with our own wait notify for both the window and the dialog then, it would wait forever first, or notify (no one else waiting ) then wait forever after. unless the dialog is created in a thread somehow,
    but yes, an event handler is usually the best way to go. I ususally create my own event object, and an event manager, which sends events to configurable listeners in my application. kind of what the Swing or AWT events system have, but the your own event listener is good to route events that are not GUI based, or even out side of the GUI environment, such as a system log on failure, raise an event, or user logged in, connection has closed, and so on.

Similar Threads

  1. wait() and notify() trouble with UI
    By Atriamax in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-09-2009, 03:51 AM
  2. wait() and notify() problems
    By greyradio in forum Threads and Synchronization
    Replies: 1
    Last Post: 08-03-2009, 04:36 AM
  3. Make a program notify
    By McChill in forum New To Java
    Replies: 7
    Last Post: 06-03-2009, 10:17 PM
  4. What is the execution path of wait() and notify() ?
    By AegisCruiser in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •