I want to have a boolean to notify some sections of the system that a specific service started.
For some strange reason I'm getting the error java.lang.IllegalMonitorStateException: object not locked by thread before notifyAll()
.
What is strange is that the notifyAll() is inside a synchronized block that takes control over the object that I call notifyAll() on.
My class starts like this:
public class MyService {
public static Boolean notifier = Boolean.valueOf(false);
@Override
public void start() {
synchronized (MyService.notifier) {
MyService.notifier = Boolean.valueOf(true);
MyService.notifier.notifyAll();
}
}
@Override
public void stop() {
synchronized (MyService.notifier) {
MyService.notifier = Boolean.valueOf(false);
MyService.notifier.notifyAll();
}
}
...
}
I'm working on an android application. I don't think it should affect anything, but I'm complementing the question with that comment in case that affects the way that java works.
Why am I getting the exception if the object is locked inside a synchronized block?
The line
MyService.notifier = Boolean.valueOf(true);
swaps out the object you're locking on, it overwrites the variable with a reference to a new object. So the object you acquired the lock on upon entering the block is not the same one that you're calling notifyAll
on. All notifyAll
knows is it hasn't acquired the lock on the object it's being called on, which is the new object created after the synchronize block was entered.
All the threads need to be using the same lock. Like Ian Roberts said, the lock belongs to the object. If you overwrite the object you have a new lock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With