I was trying to read up on Producer Consumer solution and I came across this code:
package SampleProjects;
public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) {
}
}
contents = value;
available = true;
notifyAll();
}
}
class Consumer extends Thread {
private final CubbyHole cubbyhole;
private final int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
int value = cubbyhole.get();
System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
class Producer extends Thread {
private final CubbyHole cubbyhole;
private final int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number + " put: " + i);
try {
sleep((int) (Math.random() * 100));
} catch (InterruptedException e) {
}
}
}
}
In this code both the threads calling different methods, i.e producer thread is only concerned about put method and Consumer thread only concerned with get method, so I was wondering why do we need to synchronize them when we have a loop inside to check for contents "while(available)"
Then I removed the synchronized keyword and it threw an IllegalStateMonitor Exception
So my question is do we need to have synchronized keyword only so that we can call notifyAll()