aspose file tools*
The moose likes Threads and Synchronization and the fly likes java.lang.IllegalMonitorStateException: current thread not owner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "java.lang.IllegalMonitorStateException: current thread not owner" Watch "java.lang.IllegalMonitorStateException: current thread not owner" New topic
Author

java.lang.IllegalMonitorStateException: current thread not owner

Rahulmitt Mittal
Greenhorn

Joined: May 28, 2012
Posts: 3
I am learning thread synchronization and working on producer/consumer problem. Although, I am using jdk 1.5 but for simplicity, I am avoiding using ExecutorService and BlockingQueue and just trying to concentrate on thread synchronization. There are 3 producers and 4 consumers that communicate among themselves via a queue.

Here is my code:

1. Buffer.java




2. Producer.java


3. Consumer.java


4. ProducerConsumerDemo.java



Upon running this code, I get the following exceptions in my eclipse console:



Any ideas/suggestions are welcome.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4491
    
    8

Hi Rahulmitt. Welcome to the Ranch!

To be able to call wait() on an object, you need to be synchronized on that object. Otherwise, you get that IllegalMonitorStateException.

Now, look at your Buffer.get() method. It's marked as synchronized, which means it's synchronized on the Buffer object. But then you call producerLock.wait(). So you need to either change the object you're synchronizing on, or change the object you're calling wait/notify on.
Rahulmitt Mittal
Greenhorn

Joined: May 28, 2012
Posts: 3
Thanks Matthew. It worked. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.IllegalMonitorStateException: current thread not owner