-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockingQueue.java
46 lines (32 loc) · 901 Bytes
/
BlockingQueue.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
/*
BlockingQueue.java
Borrowed from
http://stackoverflow.com/questions/10329896/implementing-a-blocking-queue-in-javame-how-to-optimize-it
*/
import java.util.Vector;
public class BlockingQueue
{
private Vector queue;
public BlockingQueue(int limit)
{
queue = new Vector(limit);
}
public synchronized void put(Object o) throws InterruptedException
{
queue.addElement(o);
notifyAll();
} /* put */
public synchronized Object take() throws InterruptedException
{
Object ret = null;
while (queue.isEmpty()) {
try {
wait();
} catch (InterruptedException e) {}
}
ret = queue.elementAt(0);
queue.removeElementAt(0);
return ret;
} /* take */
public synchronized int size() { return queue.size(); }
} /* BlockingQueue */