-
Notifications
You must be signed in to change notification settings - Fork 366
/
InterProcess.java
85 lines (70 loc) · 1.36 KB
/
InterProcess.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package interprocess;
class MyData
{
int value;
boolean flag=true;
synchronized public void set(int v)
{
while(flag!=true)
try {wait();}catch(Exception e){}
value=v;
flag=false;
notify();
}
synchronized public int get()
{
int x=0;
while(flag!=false)
try {wait();}catch(Exception e){}
x=value;
flag=true;
notify();
return x;
}
}
class Producer extends Thread
{
MyData data;
public Producer(MyData d)
{
data=d;
}
public void run()
{
int count=1;
while(true)
{
data.set(count);
System.out.println("Producer "+count);
count++;
}
}
}
class Consumer extends Thread
{
MyData data;
public Consumer(MyData d)
{
data=d;
}
public void run()
{
int value;
while(true)
{
value=data.get();
System.out.println("Consumer "+value);
}
}
}
public class InterProcess
{
public static void main(String[] args)
{
MyData data=new MyData();
Producer p=new Producer(data);
Consumer c=new Consumer(data);
p.start();
c.start();
}
}