-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProCon.java
More file actions
120 lines (111 loc) · 2.58 KB
/
ProCon.java
File metadata and controls
120 lines (111 loc) · 2.58 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.LinkedList;
import java.util.List;
class Producer implements Runnable
{
private List<Integer> sharedQueue;
private int maxSize=2;
public Producer(List<Integer> sharedQueue)
{
this.sharedQueue = sharedQueue;
}
@Override
public void run()
{
for (int i = 1; i <= 5; i++)
{
try
{
produce(i);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
private void produce(int i) throws InterruptedException
{
synchronized (sharedQueue)
{
while (sharedQueue.size() == maxSize)
{
System.out.println("Queue is full, producerThread is waiting for "
+ "consumerThread to consume, sharedQueue's size= "+maxSize);
sharedQueue.wait();
}
}
synchronized (sharedQueue)
{
System.out.println("Produced : " + i);
sharedQueue.add(i);
Thread.sleep((long)(Math.random() * 1000));
sharedQueue.notify();
}
}
}
class Consumer implements Runnable
{
private List<Integer> sharedQueue;
public Consumer(List<Integer> sharedQueue)
{
this.sharedQueue = sharedQueue;
}
public void run()
{
while (true)
{
try
{
consume();
Thread.sleep(100);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
private void consume() throws InterruptedException {
synchronized (sharedQueue) {
while (sharedQueue.size() == 0) {
System.out.println("Queue is empty, consumerThread is waiting for "
+ "producerThread to produce, sharedQueue's size= 0");
sharedQueue.wait();
}
}
synchronized (sharedQueue)
{
Thread.sleep((long)(Math.random() * 1000));
System.out.println("CONSUMED : "+ sharedQueue.remove(0));
sharedQueue.notify();
}
}
}
public class ProCon
{
public static void main(String args[])
q{
List<Integer> sharedQueue = new LinkedList<Integer>();
Producer producer=new Producer(sharedQueue);
Consumer consumer=new Consumer(sharedQueue);
Thread producerThread = new Thread(producer, "ProducerThread");
Thread consumerThread = new Thread(consumer, "ConsumerThread");
producerThread.start();
consumerThread.start();
}
}
/*OUTPUT
Produced : 1
Produced : 2
Queue is full, producerThread is waiting for consumerThread to consume, sharedQueue's size= 2
CONSUMED : 1
Produced : 3
Queue is full, producerThread is waiting for consumerThread to consume, sharedQueue's size= 2
CONSUMED : 2
Produced : 4
Queue is full, producerThread is waiting for consumerThread to consume, sharedQueue's size= 2
CONSUMED : 3
Produced : 5
CONSUMED : 4
CONSUMED : 5
Queue is empty, consumerThread is waiting for producerThread to produce, sharedQueue's size= 0
*/