简体   繁体   English

IllegalMonitorStateException-CyclicBarrier

[英]IllegalMonitorStateException - CyclicBarrier

I have a method which uses CyclicBarrier as shown below: 我有一个使用CyclicBarrier的方法,如下所示:

public void getMessage(Message obj){
    CyclicBarrier barrier = new CyclicBarrier(1, new Runnable() {
        @Override
        public void run() {
            synchronized(obj){ 
                System.out.println("--The End --");
            }
        }
    });

    executor.execute(new Runnable() {
        @Override
        public void run() {
            synchronized(obj){
                //Perform some routine with message object
            }
            try {
                barrier.wait();//java.lang.IllegalMonitorStateException thrown on this line
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
}

At the point where i wait for the routine to finish executing, i get: 在等待例程完成执行的时刻,我得到:

Exception in thread "pool-2-thread-3"
java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:502)

Do anyone knows what I am doing wrong here? 有人知道我在这里做错了吗?

In order to call wait() on any object, the current thread has to own its monitor. 为了在任何对象上调用wait() ,当前线程必须拥有其监视器。 You're calling barrier.wait() without any synchronized(barrier) . 您正在调用barrier.wait()而没有任何barrier.wait() synchronized(barrier)

However, you may have meant to use the await() method (on CyclicBarrier) instead of wait() . 但是,您可能打算使用await()方法(在CyclicBarrier上)而不是wait() It's hard to say, as it's not clear what you're trying to achieve. 很难说,因为不清楚您要实现什么。

yeah, you need to gain the monittor of barrier like so: 是的,您需要像这样获得屏障的监督员:

synchhronized(barrier){
try {
     barrier.wait();//java.lang.IllegalMonitorStateException not thrown on this line
    } catch (InterruptedException e) {
    e.printStackTrace();
}
}

Maybe you did want to use await() instead that wait() ? 也许您确实想使用await()而不是那个wait()吗?

wait is used to block a thread over a specific object and it is a feature of every object, but in your case you are calling it without taking the monitor of it. wait用于阻止特定对象上的线程,并且它是每个对象的功能,但是在您的情况下,您在调用它时不使用它的监视器。 You should call wait from inside the same obect or use a synchronized block over barrier itself. 您应该从同一对象内部调用wait ,或者在barrier本身上使用synchronized块。

You need to acquire lock before using the barrier object. 在使用障碍物对象之前,您需要获取锁。

Regards, Dheeraj Joshi 此致Dheeraj Joshi

The cyclicBarrier is not intended to be used as you do here : participating threads are expected to call the blocking "await()" method. 不想像您在此处那样使用CyclicBarrier:参与线程应调用阻塞的“ await()”方法。

As a side note, a CyclicBarrier with a count of 1 is totally useless : its intent is to allow a certain number of threads (the barrier count) to wait for each other before continuing. 附带一提,计数为1的CyclicBarrier完全没有用:其目的是允许一定数量的线程(障碍计数)在继续之前互相等待。 Maybe you should consider changing your whole algorithm, especially if you're not sure about how concurrency stuff works. 也许您应该考虑更改整个算法,尤其是在不确定并发事物如何工作的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM