-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.java
257 lines (230 loc) · 5.53 KB
/
PriorityQueue.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class PriorityQueue {
class Node{
String name;
int pri;
Node next;
ReentrantLock nodeLock;
public Node(String name, int priority){
this.name = name;
this.pri = priority;
next = null;
nodeLock = new ReentrantLock();
}
public String getName(){
return name;
}
public int getPriority(){
return pri;
}
}
int maxSize;
int size;
Node head;
//Node tail;
final ReentrantLock lock1 = new ReentrantLock();
final ReentrantLock lock2 = new ReentrantLock();
final ReentrantLock sizeLock = new ReentrantLock();
final Condition notFull = lock2.newCondition();
final Condition notEmpty = lock1.newCondition();
public PriorityQueue(int maxSize) {
// Creates a Priority queue with maximum allowed size as capacity
this.maxSize = maxSize;
size = 1;
head = new Node(" ", -1);
head.next = null;
//tail = head;
}
// Adds the name with its priority to this queue.
// Returns the current position in the list where the name was inserted;
// otherwise, returns -1 if the name is already present in the list.
// This method blocks when the list is full.
public int add(String name, int priority) {
System.out.println(size);
Node newNode = new Node(name, priority);
if(head == null){
head = newNode;
head.next = null;
sizeLock.lock();
size++;
sizeLock.unlock();
return 0;
}
Node current = head;
current.nodeLock.lock();
int i = 0;
//if duplicate name
if(findDuplicate(name)){
current.nodeLock.unlock();
// current.next.nodeLock.unlock();
return -1;
}
//if first node, doesn't change size
if(size == 1 && current.getName() == " "){
current.name = name;
current.pri=priority;
current.nodeLock.unlock();
return 0;
}
/* priority of new node is higher than priority of head*/
if(head.getPriority() < priority){
while(size == (maxSize)){
try {
System.out.println("is FUll");
lock2.lock();
notFull.await();
lock2.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
newNode.next = head;
head.nodeLock.unlock();
head = newNode;
sizeLock.lock();
size++;
sizeLock.unlock();
return 0;
}
else{
while(current.next != null && current.getPriority() >= priority && current.next.getPriority() >= priority){
while(size == (maxSize)){
try {
System.out.println("is FUll");
lock2.lock();
notFull.await();
lock2.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
current.nodeLock.lock();
current.next.nodeLock.lock();
current.nodeLock.unlock();
current = current.next;
i++;
}
newNode.next = current.next;
current.next = newNode;
sizeLock.lock();
size++;
sizeLock.unlock();
lock1.lock();
notEmpty.signal();
lock1.unlock();
current.nodeLock.unlock();
}
return i;
}
// Returns the position of the name in the list;
// otherwise, returns -1 if the name is not found.
public int search(String name) {
if(head == null){
return -1;
}
Node current = head;
head.nodeLock.lock();
int i = 0;
while(current.next != null){
current.nodeLock.lock();
current.next.nodeLock.lock();
if(current.getName().equals(name)){
current.nodeLock.unlock();
current.next.nodeLock.unlock();
return i;
}
current.nodeLock.unlock();
current = current.next;
i++;
}
if(current.getName().equals(name)){
return i;
}
return -1;
}
// Retrieves and removes the name with the highest priority in the list,
// or blocks the thread if the list is empty.
public String getFirst() {
while(head == null){
try {
System.out.println("ain't got no sheeeeit");
lock1.lock();
notEmpty.await();
lock1.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
head.nodeLock.lock();
// if(head.next != null){
// head.next.nodeLock.lock();
// }
while(size == 0){
try {
System.out.println("no elements");
lock1.lock();
notEmpty.await();
lock1.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String retStr = head.getName();
head.nodeLock.unlock();
head = head.next;
sizeLock.lock();
size--;
sizeLock.unlock();
lock2.lock();
notFull.signal();
lock2.unlock();
// head.nodeLock.unlock();
return retStr;
}
public void print() {
if(head != null){
Node current = head;
head.nodeLock.lock();
while(current.next != null){
System.out.println("name: " + current.getName() + " priority: " + current.getPriority());
current = current.next;
}
System.out.println("name: " + current.getName() + " priority: " + current.getPriority());
}
}
public int getSize(){
int size = 1;
Node current = head;
while(current.next != null){
current.nodeLock.lock();
current.next.nodeLock.lock();
current.nodeLock.unlock();
current = current.next;
size++;
}
return size;
}
public boolean findDuplicate(String n){
Node current = head;
while(current.next != null){
current.nodeLock.lock();
current.next.nodeLock.lock();
if(current.name.equals(n)){
current.nodeLock.unlock();
System.out.println("found");
return true;
}
current.nodeLock.unlock();
current = current.next;
}
if(current.name.equals(n)){
System.out.println("found");
return true;
}
else{
//current.nodeLock.unlock();
return false;
}
}
}