synchronized object not locked by thread before notifyAll()

柔情痞子 提交于 2019-11-30 09:23:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/14073516/synchronized-object-not-locked-by-thread-before-notifyall

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