-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFactory.java
More file actions
54 lines (48 loc) · 1.22 KB
/
Copy pathFactory.java
File metadata and controls
54 lines (48 loc) · 1.22 KB
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
50
51
52
53
54
package com.dac.itc;
public class Factory {
int capacity = 5;
int current = 0;
Object lock = new Object();
public void produce(){
synchronized (lock) {
System.out.println("Going to Produce the Items");
while ( current < capacity){
current++;
System.out.println("Producing Item :: "+current);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Production is done, going awake the waiting thread");
lock.notifyAll();
System.out.println("Production is done!");
}
}
public void consume(){
synchronized (lock) {
System.out.println("Going to consuem the itesm");
if(current == 0)
{
try {
System.out.println("No Items to consume, going to wait state");
lock.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
while ( current > 0){
current--;
System.out.println("Consumeing the Item : "+current);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Consumption is done!");
}
}
}