-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathInterClassComm.java
55 lines (47 loc) · 1.12 KB
/
InterClassComm.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
class Buffer {
int data;
boolean empty = true;
synchronized void put(int d) {
data = d;
System.out.println("Produced :" + data);
}
synchronized void get() {
System.out.println("Consumed :" + data);
}
}
class ProducerThread implements Runnable {
Buffer buff;
public ProducerThread(Buffer buff) {
this.buff = buff;
}
public void run() {
for (int i = 1; i <= 10; i++) {
buff.put(i);
}
}
}
class ConsumerThread implements Runnable {
Buffer buff;
public ConsumerThread(Buffer buff) {
this.buff = buff;
}
public void run() {
for (int i = 1; i <= 10; i++) {
buff.get();
}
}
}
public class InterClassComm {
public static void main(String args[]) {
Thread t1, t2;
Buffer buff = new Buffer();
ProducerThread pt = new ProducerThread(buff);
ConsumerThread ct = new ConsumerThread(buff);
t1 = new Thread(pt);
t2 = new Thread(ct);
t1.setName("Producer");
t2.setName("Consumer");
t1.start();
t2.start();
}
}