1 9 package com.ziclix.python.sql.util; 10 11 import java.util.LinkedList ; 12 13 21 public class Queue { 22 23 26 protected boolean closed; 27 28 31 protected LinkedList queue; 32 33 36 protected int capacity, threshold; 37 38 41 public Queue() { 42 this(0); 43 } 44 45 48 public Queue(int capacity) { 49 50 this.closed = false; 51 this.capacity = capacity; 52 this.queue = new LinkedList (); 53 this.threshold = (int) (this.capacity * 0.75f); 54 } 55 56 59 public synchronized void enqueue(Object element) throws InterruptedException { 60 61 if (closed) { 62 throw new QueueClosedException(); 63 } 64 65 this.queue.addLast(element); 66 this.notify(); 67 68 71 while ((this.capacity > 0) && (this.queue.size() >= this.capacity)) { 72 this.wait(); 73 74 if (closed) { 75 throw new QueueClosedException(); 76 } 77 } 78 } 79 80 83 public synchronized Object dequeue() throws InterruptedException { 84 85 while (this.queue.size() <= 0) { 86 this.wait(); 87 88 if (closed) { 89 throw new QueueClosedException(); 90 } 91 } 92 93 Object object = this.queue.removeFirst(); 94 95 if (this.queue.size() < this.threshold) { 97 this.notify(); 98 } 99 100 return object; 101 } 102 103 106 public synchronized void close() { 107 108 this.closed = true; 109 110 this.notifyAll(); 111 } 112 } 113 | Popular Tags |