1 4 package nl.justobjects.pushlet.core; 5 6 22 public class EventQueue { 23 24 private int capacity = 8; 25 private Event[] queue = null; 26 private int front, rear; 27 28 29 public EventQueue() { 30 this(8); 31 } 32 33 34 public EventQueue(int capacity) { 35 this.capacity = capacity; 36 queue = new Event[capacity]; 37 front = rear = 0; 38 } 39 40 41 public synchronized boolean enQueue(Event item) throws InterruptedException { 42 return enQueue(item, -1); 43 } 44 45 46 public synchronized boolean enQueue(Event item, long maxWaitTime) throws InterruptedException { 47 48 while (isFull()) { 50 if (maxWaitTime > 0) { 51 wait(maxWaitTime); 53 54 if (isFull()) { 57 return false; 58 } 59 } else { 60 wait(); 61 } 62 } 63 64 queue[rear] = item; 66 rear = next(rear); 67 68 notifyAll(); 70 return true; 71 } 72 73 74 public synchronized Event deQueue() throws InterruptedException { 75 return deQueue(-1); 76 } 77 78 79 public synchronized Event deQueue(long maxWaitTime) throws InterruptedException { 80 while (isEmpty()) { 81 if (maxWaitTime >= 0) { 82 wait(maxWaitTime); 83 84 if (isEmpty()) { 87 return null; 88 } 89 } else { 90 wait(); 92 } 93 } 94 95 Event result = fetchNext(); 97 98 notifyAll(); 100 101 return result; 103 } 104 105 106 public synchronized Event[] deQueueAll(long maxWaitTime) throws InterruptedException { 107 while (isEmpty()) { 108 if (maxWaitTime >= 0) { 109 wait(maxWaitTime); 110 111 if (isEmpty()) { 114 return null; 115 } 116 } else { 117 wait(); 119 } 120 } 121 122 Event[] events = new Event[getSize()]; 124 for (int i = 0; i < events.length; i++) { 125 events[i] = fetchNext(); 126 } 127 128 notifyAll(); 130 131 return events; 133 } 134 135 public synchronized int getSize() { 136 return (rear >= front) ? (rear - front) : (capacity - front + rear); 137 } 138 139 140 public synchronized boolean isEmpty() { 141 return front == rear; 142 } 143 144 145 public synchronized boolean isFull() { 146 return (next(rear) == front); 147 } 148 149 150 private int next(int index) { 151 return (index + 1 < capacity ? index + 1 : 0); 152 } 153 154 155 private Event fetchNext() { 156 Event temp = queue[front]; 157 queue[front] = null; 158 front = next(front); 159 return temp; 160 } 161 162 public static void p(String s) { 163 System.out.println(s); 164 } 165 166 public static void main(String [] args) { 167 EventQueue q = new EventQueue(8); 168 Event event = new Event("t"); 169 try { 170 q.enQueue(event); 171 p("(1) size = " + q.getSize()); 172 q.enQueue(event); 173 p("(2) size = " + q.getSize()); 174 q.deQueue(); 175 p("(1) size = " + q.getSize()); 176 q.deQueue(); 177 p("(0) size = " + q.getSize()); 178 179 q.enQueue(event); 180 q.enQueue(event); 181 q.enQueue(event); 182 p("(3) size = " + q.getSize()); 183 q.deQueue(); 184 p("(2) size = " + q.getSize()); 185 q.enQueue(event); 186 q.enQueue(event); 187 q.enQueue(event); 188 p("(5) size = " + q.getSize()); 189 q.enQueue(event); 190 q.enQueue(event); 191 p("(7) size = " + q.getSize()); 192 q.deQueue(); 193 q.deQueue(); 194 q.deQueue(); 195 p("(4) size = " + q.getSize()); 196 q.deQueue(); 197 q.deQueue(); 198 q.deQueue(); 199 ; 200 q.deQueue(); 201 p("(0) size = " + q.getSize()); 202 203 q.enQueue(event); 204 q.enQueue(event); 205 q.enQueue(event); 206 q.enQueue(event); 207 q.enQueue(event); 208 p("(5) size = " + q.getSize()); 209 210 q.deQueue(); 211 q.deQueue(); 212 q.deQueue(); 213 ; 214 q.deQueue(); 215 p("(1) size = " + q.getSize()); 216 } catch (InterruptedException ie) { 217 } 218 } 219 } 220 221 243 | Popular Tags |