-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterThread.java
More file actions
85 lines (70 loc) · 1.39 KB
/
InterThread.java
File metadata and controls
85 lines (70 loc) · 1.39 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
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 Threads;
class XQuantity {
boolean isProduce = false;
int quan;
synchronized void put(int quan) {
if (isProduce) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println("In Put " + e);
e.printStackTrace();
}
}
this.quan = quan;
System.out.println("Quantity Put By Producer " + this.quan);
isProduce = true;
this.notify();
}
synchronized int get() {
if (isProduce == false) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println("In Get " + e);
e.printStackTrace();
}
}
System.out.println("Quantity Get By Consumer " + this.quan);
isProduce = false;
notify();
return this.quan;
}
}
class Producer1 implements Runnable {
XQuantity quan = null;
Producer1(XQuantity q) {
this.quan = q;
Thread t = new Thread(this, "Producer");
t.start();
}
public void run() {
int i = 0;
while (true) {
this.quan.put(i++);
}
}
}
class Consumer1 implements Runnable {
XQuantity quan = null;
Consumer1(XQuantity q) {
this.quan = q;
Thread t = new Thread(this, "Consumer");
t.start();
}
public void run() {
int i = 0;
while (true) {
this.quan.get();
}
}
}
public class InterThread {
public InterThread() {
}
public static void main(String[] args) {
XQuantity obj = new XQuantity();
Producer1 producer = new Producer1(obj);
Consumer1 consumer = new Consumer1(obj);
}
}