IllegalMonitorStateException is a Runtime Exception and this is thrown when we are try to attempt wait or notify methods without acquiring the lock of the object.
Lets go further for better understanding of how the issue can occur. Let see the below sample code
In above code i am trying to call notify method in user object without acquiring the lock of the user class. Since you have only lock in current class due to this illegalmonitorstateexception being raised. In order to over come this issue first get into the lock of the class by using synchronization then call the methods.
ava.lang.IllegalMonitorStateException: null at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryRelease(ReentrantReadWriteLock.java:374) ~[na:1.7.0_75] at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260) ~[na:1.7.0_75] at java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.unlock(ReentrantReadWriteLock.java:1131) ~[na:1.7.0_75]
Lets go further for better understanding of how the issue can occur. Let see the below sample code
public class Test { /** * @param args */ public static void main(String[] args) { User user = new User(); user.notify(); System.out.println("done"); } }
In above code i am trying to call notify method in user object without acquiring the lock of the user class. Since you have only lock in current class due to this illegalmonitorstateexception being raised. In order to over come this issue first get into the lock of the class by using synchronization then call the methods.
public class Test { /** * @param args */ public static void main(String[] args) { User user = new User(); synchronized (user) { user.notify(); System.out.println("done"); } } }
0 Comments
Post a Comment