class Methods int enum States Init ValueProduced ValueInStore ValueCon

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
}
}