-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyBlockingQueue.java
More file actions
38 lines (32 loc) · 909 Bytes
/
MyBlockingQueue.java
File metadata and controls
38 lines (32 loc) · 909 Bytes
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
import java.util.LinkedList;
import java.util.List;
public class MyBlockingQueue {
private List queue = new LinkedList();
private int limit;
public MyBlockingQueue(){
this.limit = 10;
}
public void setLimit(int limit){
this.limit=limit;
}
public synchronized void enqueue(Received_Mail mail)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
this.queue.add(mail);
if(this.queue.size() == 1) {
notifyAll();
}
}
public synchronized Received_Mail dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return (Received_Mail)this.queue.remove(0);
}
}