1

I'm trying to notify() a particular thread from another thread run method. I'm facing java.lang.IllegalMonitorStateException Both the threads are started in a main class.

Note: All three are different classes.

MAIN CLASS

public class queueCheck {

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    BlockingQueue<testObjectClass> queue = new LinkedBlockingQueue<testObjectClass>();
    Producer producer = new Producer(queue);
    Consumer consumer = new Consumer(queue);

    Thread produceThread = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                producer.produce();
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    });

    Thread consumerThread = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                consumer.consume();
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    });

    producer.setConsumerThread(consumerThread);
    consumer.setProducerThread(produceThread);

    produceThread.start();
    consumerThread.start();

}

}

PRODUCER CLASS

class Producer {
BlockingQueue<testObjectClass> queue;
testObjectClass objectSample = new testObjectClass("TEST", "TEST");
Thread consumerThread;


public void setConsumerThread(Thread t1) {
    this.consumerThread = t1;
}

public Producer(BlockingQueue<testObjectClass> outQueue) {
    // TODO Auto-generated constructor stub
    this.queue = outQueue;
}

public void produce() throws InterruptedException {
    int i = 0;
    while (true) {
        synchronized (this) {
            // Producer thread waits while list is full
            try {
                while (!queue.isEmpty()) {
                    // Notifies the consumer thread that now it can start consuming
                    this.wait();
                }
                i = i + 1;
                objectSample.setValues("String ", String.valueOf(i));
                queue.put(objectSample);
                System.out.println("Adding element: " + objectSample.stringMessage());
                consumerThread.notify();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

}

CONSUMER CLASS

 class Consumer {
BlockingQueue<testObjectClass> queue;
testObjectClass objectSample;
Thread producerThread;


public void setProducerThread(Thread t1) {
    this.producerThread = t1;
}

public Consumer(BlockingQueue<testObjectClass> outQueue) {
    // TODO Auto-generated constructor stub
    this.queue = outQueue;
}

// Function called by consumer thread
public void consume() throws InterruptedException {
    while (true) {
        synchronized (this) {
            //Consumer thread waits while list is empty.
            while (queue.isEmpty()) {
                this.wait();
            }
            testObjectClass objectX = queue.take();
            System.out.println("\t Taking element: " + objectX.stringMessage());
            producerThread.notify();
        }
    }
}

}

HELPER CLASS

class testObjectClass {
private String test1;
private String test2;
public testObjectClass(String testString1, String testString2) {
    this.test1 = testString1;
    this.test2 = testString2;
}
public void setValues(String testString1,String testString2) {
    this.test1 = testString1;
    this.test2 = testString2;
}
public String stringMessage() {
    return test1 + test2;
}

}

1 Answer 1

1

https://examples.javacodegeeks.com/java-basics/exceptions/java-lang-illegalmonitorstateexception-how-to-solve-illegalmonitorstateexception/

Use this link to get some detail information. Here you are acquiring a lock using one object and releasing the same lock by using a different object. Use a shared object to acquire a lock and release the lock.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.