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

    Default InterruptedException and Locks

    Java Code:
    synchronized (signallingObject) {
                                while (!signalled) {
                                    try {
                                        signallingObject.wait();
                                    } catch (InterruptedException ex) {
                                        /*@Todo LOGME */
                                        System.err.println("Value Updater Interrupted");
                                        /* Check to see if i have been told to quit */
                                        if (!isRunning) {
                                            /* Signal before exit */
                                            signalled = false;
                                            signallingObject.notify();
                                            break mainloop;
                                        }
                                    }
                                }
                            }
    I could not find it in the java doc, and the sources seem to lack an implementation as the wait method has a native tag on it. So quick questions

    the try catch block is within the signallingobject synch scope. When wait is called we give up the lock. What happens to the lock if we are interrupted, are we blocked until we can regain the lock (Assuming another thread has the objects lock when the exception is caught). Or have we found ourselves in strange state where two threads are inside one objects synch block.

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

    Default

    My own tests seem to indicate the lock is recovered after the catch.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The wait can not exit without reaquiring the lock, even to handle an exception. Rest assured that it is impossible for two threads synchronized on the same object to run simultaneously under any condition.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I'm not sure what you are doing, but you can't notify() unless you already own the monitor. Since you released the monitor with wait(), you must not be executing the notify(). Further, I doubt that the monitor is reacquired within the loop.

    First, move your synchronized block as close to the wait() as you can. In this case, it should at least be within the while block, if not the try.

    Second, you must reacquire the monitor by adding a second synchronized around the notify(), since wait() released it.

    Third, all that activity inside a catch block looks suspicious. It may end up doing what you want, but if you can't look at your code and know immediately what it is doing, you probably need to come up with a cleaner solution.

Similar Threads

  1. Replies: 2
    Last Post: 05-28-2009, 05:09 PM
  2. Thread Safe Methods / Locks
    By mrhyman in forum Threads and Synchronization
    Replies: 16
    Last Post: 10-24-2008, 09:57 PM
  3. Replies: 1
    Last Post: 07-31-2007, 05:52 AM

Posting Permissions

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