Questions tagged [illegalmonitorstateexcep]

An exception thrown in Java to indicate that an object called notify(), notifyAll(), or wait() without the current thread owning its monitor.

An exception thrown in Java to indicate that an object called notify(), notifyAll(), or wait() without the current thread owning its monitor.

The object in question must be the target of a synchronized statement so that the current thread can obtain a lock on the monitor associated with it. If the thread does not lock on the object's monitor yet the object invokes the notify, notifyAll, or wait method, an IllegalMonitorStateException will be thrown.

See also:

Java Language Specification references

Java Tutorials

Related tags:

35 questions
41
votes
4 answers

Why does notifyAll() raise IllegalMonitorStateException when synchronized on Integer?

Why does this test program result in a java.lang.IllegalMonitorStateException? public class test { static Integer foo = new Integer(1); public static void main(String[] args) { synchronized(foo) { foo++; …
jjvainio
  • 571
  • 1
  • 5
  • 7
30
votes
4 answers

What can cause IllegalMonitorStateException from inside a synchronized block?

We hit an extremely surprising exception today. Inside of a synchronized block, we call wait() and it throws IllegalMonitorStateException. What can cause this? This is happening in well-tested open source…
Chris Dolan
  • 8,905
  • 2
  • 35
  • 73
14
votes
1 answer

IllegalMonitorStateException

What can cause that i get IllegalMonitorStateException in this code synchronized(syncCount){ syncCount--; syncCount.notify(); } I'm little confused, since, as far as I know running thread must have monitor on object who's notify is called.…
user249150
11
votes
2 answers

IllegalMonitorStateException on awaitTermination function

I'm having a problem with using threads in Java (I have little experience with threads in Java, but much in C++, so i understand basic concept of threads). I've used example code for threads in Java, and the code is next: ExecutorService…
jozai1
  • 111
  • 3
10
votes
1 answer

IllegalMonitorStateException notify() and wait()

I have a problem. When I use notify() in a synchronized block I get IllegalMonitorStateException. Can anyone help me solve this problem? I need one thread to send a char to a second thread, then this thread has to wait and second thread print this…
Sylwek
  • 856
  • 1
  • 9
  • 24
4
votes
2 answers

Getting IllegalMonitorStateException while using wait() in Job scheduler

I'm working on a Java RMI based project which has Client-->Job Scheduler--> Server structure. I have two methods in Job scheduler class as given below. Commenting explains the purpose of each line of code. private ConcurrentLinkedQueue
3
votes
0 answers

Active waiting replacement - Is my approach correct by design?

I am getting rid of waiting: public void run() { while(!running) {} //active waiting //some action after running is true } My code: class MyThread implements Runnable { protected boolean running = false; public Object lock =…
user1097772
  • 3,499
  • 15
  • 59
  • 95
2
votes
1 answer

java.lang.IllegalMonitorStateException on notifyAll() method

I am still learning on Threads following java tutorials of oracle website. With regarding to the wait() and notifyAll(), I have written some code. My expected output is print the message in run() 10 times and print the "Fun stopped by StopFun…
Ran_Macavity
  • 154
  • 2
  • 21
1
vote
0 answers

This inter-thread-communication code stopped working when i tried changing it's architecture a little bit

So I'm trying to implement a multi-threading java code to simulate a Message server and client. they share a common resource to communicate. Client should only be able to read the message when server notifies it, after receiving it from the user.…
1
vote
1 answer

Can anybody explain why this program is showing an IllegalMonitorStateException?

class Lock { public int l=0; } class Numbers extends Thread { final Lock lock; Numbers(Lock l,String name) { super(name); lock=l; } public void run() { synchronized(lock) { …
1
vote
1 answer

Why is IllegalMonitorStateException not thrown when notify is in class

I was wondering why I can't directly write what's in my battle.resume() method right into my frame.keyPressed() method? If I do so I get a IllegalMonitorStateException, I've found out on the net that this exception is "Thrown to indicate that a…
1
vote
1 answer

Illegal monitor state Exception when two treads are waiting

HI wrote a sample program for testing the behavior of wait in java. My implementation of Runnable: class ThreadWait implements Runnable { Object lock = new Object(); ThreadWait(Object lock){ this.lock = lock; } @Override …
robin
  • 1,893
  • 1
  • 18
  • 38
1
vote
2 answers

Why multiple threads are able to access a synchronized block?

While running this I'm getting IllegalMonitorStateException because Even thread is trying to notify when it does not have lock on the object isEven. Why is this happening? A thread should only be able to go inside the synchronized block if it has…
1
vote
1 answer

IllegalMonitorStateException on notify() when synchronized on an Integer

I'm new to using wait() and notify() in Java and I'm getting an IllegalMonitorStateException. Main Code public class ThreadTest { private static Integer state = 0; public static void main(String[] args) { synchronized(state) { …
user3217229
  • 13
  • 1
  • 4
1
vote
1 answer

IllegalMonitorStateException when creating an object

I'm getting IllegalMonitorStateException when I'm trying to create an instance of an object. The code looks like the following: public int signIn(parameters...) { ...check some stuff... new Thread(... just a simple log…
1
2 3