- Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathBlockingQueue.java
More file actions
Latest commit
49 lines (42 loc) · 1.1 KB
/
Copy pathBlockingQueue.java
File metadata and controls
49 lines (42 loc) · 1.1 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
packagethreads.blockingqueue;
importjava.util.LinkedList;
importjava.util.Queue;
/**
* Implement a blocking queue.
* User: rpanjrath
* Date: 10/14/13
* Time: 2:07 PM
*/
publicclassBlockingQueue {
privatefinalQueue<Integer> queue = newLinkedList<Integer>();
privateintlimit = 10;
publicBlockingQueue(intsize) {
this.limit = size;
}
publicvoidenqueue(intinput) throwsInterruptedException {
synchronized (queue) {
// keep on waiting until its full
while (queue.size() == limit) {
wait();
}
// if empty then notifyAll
if (queue.size() == 0) {
notifyAll();
}
queue.add(input);
//notifyAll();
}
}
publicintdequeue() throwsInterruptedException {
synchronized (queue) {
while (queue.size() == 0) {
wait();
}
if (queue.size() == limit) {
notifyAll();
}
returnqueue.remove();
//notifyAll();
}
}
}