1 /**
2
3 */
4 public class BoundedQueue<E>
5 {
6 /**
7
8 @param capacity
9 */
10 public BoundedQueue(int capacity)
11 {
12 elements = new Object[capacity];
13 head = 0;
14 tail = 0;
15 size = 0;
16 }
17
18 /**
19
20 @return
21 */
22 public synchronized E remove()
23 throws InterruptedException
24 {
25 while (size == 0) wait();
26 @SuppressWarnings("unchecked") E r = (E) elements[head];
27 head++;
28 size--;
29 if (head == elements.length)
30 head = 0;
31 notifyAll();
32 return r;
33 }
34
35 /**
36
37 @param newValue
38 */
39 public synchronized void add(E newValue)
40 throws InterruptedException
41 {
42 while (size == elements.length) wait();
43 elements[tail] = newValue;
44 tail++;
45 size++;
46 if (tail == elements.length)
47 tail = 0;
48 notifyAll();
49 }
50
51 private Object[] elements;
52 private int head;
53 private int tail;
54 private int size;
55 }