This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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:
Upon running this code, I get the following exceptions in my eclipse console:
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.