File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.IllegalMonitorStateException: current thread not owner

 
Rahulmitt Mittal
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew. It worked. :-)
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic