IllegalMonitorStateException - CyclicBarrier

I have a method which uses CyclicBarrier as shown below:

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?

Answers


In order to call wait() on any object, the current thread has to own its monitor. You're calling barrier.wait() without any synchronized(barrier).

However, you may have meant to use the await() method (on CyclicBarrier) instead of wait(). It's hard to say, as it's not clear what you're trying to achieve.


Need Your Help

C - Build dynamically allocated array of pointers to structures filled with input from file

c pointers memory-management data-structures

I need to build an array of pointers to dynamically allocated structures (DBrecord) and fill that array with input from another file. Not sure how to approach this.

Use of DateTime in PHP echo

php date datetime

I have set variables to display current year like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.