-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProducerConsumer.java
144 lines (131 loc) · 4.02 KB
/
ProducerConsumer.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.concurrent.Semaphore;
class Producer extends Thread
{
ProducerConsumer obj;
Producer(ProducerConsumer obj1)
{
this.obj = obj1;
}
public void run()
{
int i = 0;
int k = obj.items;
while(k!=0)
{
if(obj.empty_Count.availablePermits() == 0)
{
try{
System.out.println("Putting the Producer to sleep since Item Buffer is full");
this.suspend();}catch(Exception e){System.out.println(e); }
}
try{
obj.mutex.acquire();
obj.empty_Count.acquire();
System.out.println("Item produced and added to buffer. Item No: " + (++i));
//System.out.println(obj.empty_Count.availablePermits());
obj.mutex.release();
}
catch(Exception e)
{
System.out.println(e);
}
if(obj.empty_Count.availablePermits() < obj.buff_size )
{
//System.out.println("here");
try{
obj.c_thread.resume();}catch(Exception e){System.out.println(e); }
}
k--;
}
}
}
class Consumer extends Thread
{
ProducerConsumer obj;
Consumer(ProducerConsumer obj1)
{
this.obj = obj1;
}
public void run()
{
int i = 0;
int k = obj.items;
while(k!=0)
{
if(obj.empty_Count.availablePermits() == obj.buff_size)
{System.out.println("Putting the Consumer to sleep since Item Buffer is empty");
this.suspend();
}try{
obj.mutex.acquire();
obj.empty_Count.release();
System.out.println("Item consumed: " + (++i));
//System.out.println(obj.empty_Count.availablePermits());
obj.mutex.release();
}catch(Exception e){System.out.println(e); }
if(obj.empty_Count.availablePermits() >= 1)
obj.p_thread.resume();
k--;
}
}
}
class ProducerConsumer
{
static Semaphore empty_Count;
static Semaphore mutex;
static Semaphore full_count;
static int items,buff_size;
static Producer p_thread;
static Consumer c_thread;
public static void main (String[] args) throws java.lang.Exception
{
System.out.println("Enter the no. of items to be produced");
Scanner sc = new Scanner(System.in);
items = sc.nextInt();
System.out.println("Enter the Buffer Size");
buff_size = sc.nextInt();
if(buff_size<1 || items < 1)
{
System.out.println("Invalid Input/s");
System.exit(12);
}
empty_Count = new Semaphore(buff_size);
mutex = new Semaphore(1);
full_count = new Semaphore(0);
ProducerConsumer obj2 = new ProducerConsumer();
p_thread = new Producer(obj2);
c_thread = new Consumer(obj2);
p_thread.start();
c_thread.start();
try{
p_thread.join();
c_thread.join();
}
catch(Exception e){System.out.println(e);}
System.out.println("All items were succesfully produced and consumed");
}
}
/* OUTPUT
*
*--------------------Configuration: <Default>--------------------
Enter the no. of items to be produced
5
Enter the Buffer Size
3
Item produced and added to buffer. Item No: 1
Item produced and added to buffer. Item No: 2
Item produced and added to buffer. Item No: 3
Putting the Producer to sleep since Item Buffer is full
Item consumed: 1
Item consumed: 2
Item consumed: 3
Putting the Consumer to sleep since Item Buffer is empty
Item produced and added to buffer. Item No: 4
Item produced and added to buffer. Item No: 5
Item consumed: 4
Item consumed: 5
All items were succesfully produced and consumed
Process completed.
*/