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
| class Queue{ Element[] array; int enqueueIndex; int dequeueIndex; int size; Queue(int length){ array = new Element[length]; size = length; enqueueIndex = 0; dequeueIndex = 0; } synchronized Element dequeue(){ if (dequeueIndex == enqueueIndex){ return null; }else{ Element cur = array[dequeuIndex]; array[dequeueIndex] = null; dequeueIndex = (dequeueIndex + 1) % size; return cur; } } synchronized void enqueue(Element e){ if ((enqueueIndex + 1) % size == dequeueIndex){ throw new ArrayIndexOutOfBoundsException }else{ array[enqueueIndex] = e; enqueueIndex = (enqueueIndex + 1) % size; } } }
|