-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproCon.java
More file actions
60 lines (58 loc) · 1.22 KB
/
proCon.java
File metadata and controls
60 lines (58 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
55
56
57
58
59
60
class first{
int num;
boolean value=false;
public synchronized void set(int num){
while(value){
try{wait();}
catch(Exception e){}
}
System.out.println("Produced:"+num);
this.num=num;
value=true;
notify();
}
public synchronized void get(){
while(!value){
try{wait();}
catch(Exception e){}
}
System.out.println("Consumed:"+num);
value=false;
notify();
}
}
class Producer implements Runnable{
first f;
public Producer(first f){
this.f=f;
Thread t =new Thread(this,"Producer");
t.start();
}
public void run(){
int i=0;
while(i<5){
f.set(i++);
}
}
}
class Consumer implements Runnable{
first f;
public Consumer(first f){
this.f=f;
Thread t =new Thread(this,"Consumer");
t.start();
}
public void run(){
int i=0;
while(i<5){
f.get();
}
}
}
public class proCon{
public static void main(String args[]){
first f=new first();
new Producer(f);
new Consumer(f);
}
}