notifyAll() throws IllegalMonitorStateException

谁都会走 提交于 2019-11-27 19:30:13

问题


I am designing two threads: one has to get the name of players, and the second thread has to wait for the name being set in order to continue, but notify() all in the first thread is throwing the IllegalMonitorStateException error.

private NameFecth nameFetch;
private UseName useName;
private Object nameSetLock; 
public static void method{
   nameSetLock = new Object()
   nameFetch = new NameFetch(nameSetLock);
   useName = new UseName(nameSetLock);
   Thread nameFetchThread = new Thread(nameFetch);
   nameFetchThread.start();
   Thread useNameThread = new Thread(useName);
   useNameThread.start();
}

public class NameFetch implements Runnable{
    /*variables and constructers*/

    public void run(){
       /*get name and set the variable somehow*/
       synchronized(nameSetLock){
         notifyAll();
       }
    }
}

public class UseName implements Runnable{
    /*variables and constructers*/

   public void run(){
     while(!nameBeenSet){
       synchronized(nameSetLock){
         try{
           wait();
         }catch(InterruptedException e) {}
       }
     }

}

What have I done wrong?


回答1:


You're calling wait and notify without synchronizing on the thing you're waiting on or notifying. As documented in Object.notifyAll:

Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.

So this:

synchronized(nameSetLock){
  notifyAll();
}

should be:

synchronized(nameSetLock){
  nameSetLock.notifyAll();
}

... and ditto for wait. Note that your current code wouldn't even compile as you're using syncronized rather than synchronized, which suggests that you didn't post your actual code. It's possible that in typing out the code you've actually changed the problem - in which case you should edit your question to be more representative.




回答2:


It looks like your issue that you are using the lock incorectly. You synchronized block is on nameSetLock and you are calling your notifyall on your NameFetch object instance (which is the sayme a synchronized(this).

You should do nameSetLock.wait when you want to use the lock and nameSetLock.notifyAll to notify.




回答3:


From the JavaDoc of IllegalStateException

  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. 

You are trying to invoke the wait() and notifyAll() without having that object lock.

Please try what @Jon has suggested it will work.




回答4:


This happens to me when I forgot to add the synchronized in the method call.



来源:https://stackoverflow.com/questions/18209552/notifyall-throws-illegalmonitorstateexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!