This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 4344
    
    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. :-)
 
jQuery in Action, 2nd edition
 
subject: java.lang.IllegalMonitorStateException: current thread not owner
 
Similar Threads
Wait() Example
object.wait()
Why am I getting IllegalMonitorStateException?
Two questions uppon a deitel's project
Thread Madness - please help!