Results 1 to 2 of 2
  1. #1
    svdeepha is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Post IllegalMonitorStateException from synchronized block

    My code throws an IllegalMonitorStateException on 'object.wait()'
    call, even though wait is invoked inside a synchronized block. And
    this exception occurs randomly, not always.

    below is my code snippet:
    while (!mFinish) {
    synchronized (mResponse) {
    if (!mResponse.isEmpty()) {
    do something
    } else {
    try {
    mResponse.wait(1000);
    } catch (InterruptedException ie) {
    ......
    }
    }
    }
    }

    Also all the 'notify' methods are in synchronized block.
    Please point out if there is anything wrong in my code?

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    you probably have another thread that changes mResponse. If that's the case, what happens is this:

    - Thread 1 synchronizes on mResponse=Object A
    - Thread 2 sets mResponse=Object B
    - now Thread 1 attempts to execute wait() on Object B, on which it hasn't synchronized.

Similar Threads

  1. calling yield() method in synchronized block
    By Ash-infinity in forum New To Java
    Replies: 2
    Last Post: 12-04-2012, 06:35 PM
  2. Synchronized(this)?
    By kiza in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-07-2009, 02:20 PM
  3. synchronized
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-28-2007, 11:33 AM
  4. doubt about synchronized
    By simon in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 04:49 AM
  5. Return in the Middle of synchronized Block
    By ariak in forum Advanced Java
    Replies: 1
    Last Post: 07-26-2007, 11:24 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
  •