IllegalMonitorStateException on wait() call

后端 未结 10 1456
感动是毒
感动是毒 2020-11-22 11:37

I am using multi-threading in java for my program. I have run thread successfully but when I am using Thread.wait(), it is throwing java.lang.IllegalMonit

10条回答
  •  深忆病人
    2020-11-22 12:06

    I know this thread is almost 2 years old but still need to close this since I also came to this Q/A session with same issue...

    Please read this definition of illegalMonitorException again and again...

    IllegalMonitorException is 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.

    This line again and again says, IllegalMonitorException comes when one of the 2 situation occurs....

    1> wait on an object's monitor without owning the specified monitor.

    2> notify other threads waiting on an object's monitor without owning the specified monitor.

    Some might have got their answers... who all doesn't, then please check 2 statements....

    synchronized (object)

    object.wait()

    If both object are same... then no illegalMonitorException can come.

    Now again read the IllegalMonitorException definition and you wont forget it again...

提交回复
热议问题