class Methods {
int n;
enum States { Init, ValueProduced, ValueInStore, ValueConsumed };
volatile States state = States.Init;
synchronized void producer(int n) {
while (state != States.ValueConsumed && state != States.Init) {
try {
wait();
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
this.n = n;
state = States.ValueProduced;
System.out.println("Producer made: " + n);
notifyAll();
}
synchronized int storehouse() {
while (state != States.ValueProduced) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
state = States.ValueInStore;
notifyAll();
return n;
}
synchronized int user() {
while (state != States.ValueInStore) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
notifyAll();
state = States.ValueConsumed;
return n;
}
}