A new queue has come

"Jean Morissette" <[email protected]> Thu, 02 Dec 2004 10:32:42 -0500
Newsgroups gmane.comp.java.seda.user
Message-ID <[email protected]>
<html><div style='background-color:'><DIV class=RTE>I post here a queue prototype that this backed by a resizable array.&nbsp; I don't have tested it a lot yet, but it should be more scalable than actual FiniteQueue and consume&nbsp;less memory.&nbsp; Please let me know if you find bugs or have comments.</DIV>
<DIV class=RTE>Jean M</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE><BR>/**<BR>&nbsp;* QueueIF implementation that use an array, so no memory allocation occur<BR>&nbsp;* on queue operations apart from situations where array needs to be resized.<BR>&nbsp;*/<BR>public class DynamicArrayBlockingQueue implements BlockingQueueIF {</DIV>
<DIV class=RTE>&nbsp; protected QueueElementIF[] array;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the elements</DIV>
<DIV class=RTE>&nbsp; protected int takePtr = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // circular indices<BR>&nbsp; protected int putPtr = 0;</DIV>
<DIV class=RTE>&nbsp; protected int usedSlots = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // length<BR>&nbsp; protected int emptySlots;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // capacity - length</DIV>
<DIV class=RTE>&nbsp; /**<BR>&nbsp;&nbsp; * Helper monitor to handle puts.<BR>&nbsp;&nbsp; */<BR>&nbsp; protected final Object putMonitor = new Object();</DIV>
<DIV class=RTE>&nbsp; protected int maxCap;</DIV>
<DIV class=RTE>&nbsp; // transactional map<BR>&nbsp; protected Map txnMap = Collections.synchronizedMap(new HashMap());</DIV>
<DIV class=RTE>&nbsp; protected EnqueuePredicateIF pred;</DIV>
<DIV class=RTE>&nbsp; /**<BR>&nbsp;&nbsp; * Create a BoundedBuffer with the given capacity.<BR>&nbsp;&nbsp; *<BR>&nbsp;&nbsp; * @throws IllegalArgumentException if capacity less or equal to zero<BR>&nbsp;&nbsp; */<BR>&nbsp; public DynamicArrayBlockingQueue(int capacity) throws IllegalArgumentException {<BR>&nbsp;&nbsp;&nbsp; if (capacity &lt;= 0) throw new IllegalArgumentException();<BR>&nbsp;&nbsp;&nbsp; array = new QueueElementIF[capacity];<BR>&nbsp;&nbsp;&nbsp; emptySlots = capacity;<BR>&nbsp;&nbsp;&nbsp; this.maxCap = capacity;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; /**<BR>&nbsp;&nbsp; * Create a queue with the current default capacity<BR>&nbsp;&nbsp; */<BR>&nbsp; public DynamicArrayBlockingQueue() {<BR>&nbsp;&nbsp;&nbsp; this(10);<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; /**<BR>&nbsp;&nbsp; * Return the number of elements in the buffer.<BR>&nbsp;&nbsp; * This is only a snapshot value, that may change<BR>&nbsp;&nbsp; * immediately after returning.<BR>&nbsp;&nbsp; */<BR>&nbsp; public synchronized int size() {<BR>&nbsp;&nbsp;&nbsp; return usedSlots;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public int capacity() {<BR>&nbsp;&nbsp;&nbsp; return maxCap;<BR>&nbsp; }</DIV>
<DIV class=RTE><BR>&nbsp; public void setCapacity(int newCap) {<BR>&nbsp;&nbsp;&nbsp; if (newCap &lt;= 0) throw new IllegalArgumentException();<BR>&nbsp;&nbsp;&nbsp; if (newCap == maxCap) return;<BR>&nbsp;&nbsp;&nbsp; synchronized(putMonitor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; synchronized(this) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (newCap &gt; maxCap) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QueueElementIF[] newBuf = new QueueElementIF[newCap];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (takePtr &lt;= putPtr) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.arraycopy(array, takePtr, newBuf, 0, takePtr - putPtr);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putPtr -= takePtr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; takePtr = 
0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int length = array.length - takePtr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.arraycopy(array, takePtr, newBuf, 0, length);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.arraycopy(array, 0, newBuf, length, putPtr);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putPtr = length + putPtr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; takePtr = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array = newBuf;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emptySlots +=
  
newCap - maxCap;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxCap = newCap;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public void trimToSize() {<BR>&nbsp;&nbsp;&nbsp; throw new UnsupportedOperationException("Not implemented yet");<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public QueueElementIF peek() {<BR>&nbsp;&nbsp;&nbsp; synchronized (this) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (usedSlots &gt; 0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return array[takePtr];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public void blocking_enqueue(QueueElementIF x) throws InterruptedException {<BR>&nbsp;&nbsp;&nbsp; if (x == null) throw new IllegalArgumentException();<BR>&nbsp;&nbsp;&nbsp; if (Thread.interrupted()) throw new InterruptedException();</DIV>
<DIV class=RTE>&nbsp;&nbsp;&nbsp; synchronized (putMonitor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (emptySlots &lt;= 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putMonitor.wait();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (InterruptedException ex) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putMonitor.notify();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw ex;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert(x);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; incUsedSlots();<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public boolean enqueue_lossy(QueueElementIF x, long msecs) throws InterruptedException {<BR>&nbsp;&nbsp;&nbsp; if (x == null) throw new IllegalArgumentException();<BR>&nbsp;&nbsp;&nbsp; if (Thread.interrupted()) throw new InterruptedException();</DIV>
<DIV class=RTE>&nbsp;&nbsp;&nbsp; synchronized (putMonitor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long start = (msecs &lt;= 0) ? 0 : System.currentTimeMillis();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long waitTime = msecs;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (emptySlots &lt;= 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (waitTime &lt;= 0) return false;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putMonitor.wait(waitTime);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (InterruptedException ex) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putMonitor.notify();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw ex;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; waitTime = msecs - (System.currentTimeMillis() - 
start);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert(x);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; incUsedSlots();<BR>&nbsp;&nbsp;&nbsp; return true;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public QueueElementIF blocking_dequeue(int timeout_millis) {<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (timeout_millis == -1)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return blocking_dequeue();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return blocking_dequeue0(timeout_millis);<BR>&nbsp;&nbsp;&nbsp; } catch (InterruptedException e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; private QueueElementIF blocking_dequeue() throws InterruptedException {<BR>&nbsp;&nbsp;&nbsp; if (Thread.interrupted()) throw new InterruptedException();<BR>&nbsp;&nbsp;&nbsp; QueueElementIF old = null;<BR>&nbsp;&nbsp;&nbsp; synchronized (this) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (usedSlots &lt;= 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wait();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (InterruptedException ex) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; notify();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw ex;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; old = extract();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; incEmptySlots();<BR>&nbsp;&nbsp;&nbsp; return old;<B
 R>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; private QueueElementIF blocking_dequeue0(long msecs) throws InterruptedException {<BR>&nbsp;&nbsp;&nbsp; if (Thread.interrupted()) throw new InterruptedException();<BR>&nbsp;&nbsp;&nbsp; QueueElementIF old = null;<BR>&nbsp;&nbsp;&nbsp; synchronized (this) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long start = (msecs &lt;= 0) ? 0 : System.currentTimeMillis();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long waitTime = msecs;</DIV>
<DIV class=RTE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (usedSlots &lt;= 0) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (waitTime &lt;= 0) return null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wait(waitTime);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (InterruptedException ex) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; notify();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw ex;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; waitTime = msecs - (System.currentTimeMillis() - start);</DIV>
<DIV class=RTE>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; old = extract();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; incEmptySlots();<BR>&nbsp;&nbsp;&nbsp; return old;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public Object enqueue_prepare(QueueElementIF[] elements) throws SinkException {<BR>&nbsp;&nbsp;&nbsp; synchronized (putMonitor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (emptySlots &lt; elements.length) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new SinkFullException();&nbsp; /////// The queue is not necessary full ...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emptySlots -= elements.length;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; Object key = new Object();<BR>&nbsp;&nbsp;&nbsp; txnMap.put(key, elements);<BR>&nbsp;&nbsp;&nbsp; return key;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public void enqueue_commit(Object enqueue_key) {<BR>&nbsp;&nbsp;&nbsp; QueueElementIF[] elements = (QueueElementIF[]) txnMap.get(enqueue_key);<BR>&nbsp;&nbsp;&nbsp; synchronized (putMonitor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; elements.length; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[putPtr] = elements[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (++putPtr &gt;= array.length) putPtr = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; synchronized (this) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usedSlots += elements.length;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; notify();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /////////// notify or notifyAll ???<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public void enqueue_abort(Object enqueue_key) {<BR>&nbsp;&nbsp;&nbsp; QueueElementIF[] elements = (QueueElementIF[]) txnMap.remove(enqueue_key);<BR>&nbsp;&nbsp;&nbsp; if (elements == null) throw new IllegalArgumentException("unknown key");<BR>&nbsp;&nbsp;&nbsp; synchronized (putMonitor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emptySlots += elements.length;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putMonitor.notify();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /////////// notify or notifyAll ???<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public void enqueue(QueueElementIF element) throws SinkException {<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!enqueue_lossy(element, 0))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new SinkFullException();<BR>&nbsp;&nbsp;&nbsp; } catch (InterruptedException e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new SinkFullException();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public boolean enqueue_lossy(QueueElementIF element) {<BR>&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return enqueue_lossy(element, 0);<BR>&nbsp;&nbsp;&nbsp; } catch (InterruptedException e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public void enqueue_many(QueueElementIF[] elements) throws SinkException {<BR>&nbsp;&nbsp;&nbsp; synchronized (putMonitor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (emptySlots &lt; elements.length) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new SinkFullException();&nbsp; /////// The queue is not necessary full ...<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insertMany(elements);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; synchronized (this) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usedSlots += elements.length;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; notify();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /////////// notify or notifyAll ???<BR>&nbsp;&nbsp;&nbsp; }</DIV>
<DIV class=RTE>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public QueueElementIF[] dequeue_all() {<BR>&nbsp;&nbsp;&nbsp; throw new UnsupportedOperationException("Not implemented yet");<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public QueueElementIF[] dequeue(int num) {<BR>&nbsp;&nbsp;&nbsp; throw new UnsupportedOperationException("Not implemented yet");<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public QueueElementIF[] blocking_dequeue_all(int timeout_millis) {<BR>&nbsp;&nbsp;&nbsp; throw new UnsupportedOperationException("Not implemented yet");<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public QueueElementIF[] blocking_dequeue(int timeout_millis, int num) {<BR>&nbsp;&nbsp;&nbsp; throw new UnsupportedOperationException("Not implemented yet");<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public void setEnqueuePredicate(EnqueuePredicateIF pred) {<BR>&nbsp;&nbsp;&nbsp; this.pred = pred;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public EnqueuePredicateIF getEnqueuePredicate() {<BR>&nbsp;&nbsp;&nbsp; return pred;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; protected void incEmptySlots() {<BR>&nbsp;&nbsp;&nbsp; synchronized (putMonitor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++emptySlots;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putMonitor.notify();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; protected synchronized void incUsedSlots() {<BR>&nbsp;&nbsp;&nbsp; ++usedSlots;<BR>&nbsp;&nbsp;&nbsp; notify();<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; protected final void insert(QueueElementIF x) { // mechanics of put<BR>&nbsp;&nbsp;&nbsp; --emptySlots;<BR>&nbsp;&nbsp;&nbsp; array[putPtr] = x;<BR>&nbsp;&nbsp;&nbsp; if (++putPtr &gt;= array.length) putPtr = 0;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; protected final void insertMany(QueueElementIF[] elements) {<BR>&nbsp;&nbsp;&nbsp; emptySlots -= elements.length;<BR>&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; elements.length; i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[putPtr] = elements[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (++putPtr &gt;= array.length) putPtr = 0;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; protected final QueueElementIF extract() { // mechanics of take<BR>&nbsp;&nbsp;&nbsp; --usedSlots;<BR>&nbsp;&nbsp;&nbsp; QueueElementIF old = array[takePtr];<BR>&nbsp;&nbsp;&nbsp; array[takePtr] = null;<BR>&nbsp;&nbsp;&nbsp; if (++takePtr &gt;= array.length) takePtr = 0;<BR>&nbsp;&nbsp;&nbsp; return old;<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; public static void main(String[] args) throws InterruptedException {<BR>&nbsp;&nbsp;&nbsp; new Thread(new Consumer()).start();<BR>&nbsp;&nbsp;&nbsp; new Thread(new Producer()).start();<BR>&nbsp;&nbsp;&nbsp; while (true) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread.sleep(1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q.setCapacity(10);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; static DynamicArrayBlockingQueue q = new DynamicArrayBlockingQueue();</DIV>
<DIV class=RTE>&nbsp; static class Producer implements Runnable {<BR>&nbsp;&nbsp;&nbsp; public void run() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (true) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q.blocking_enqueue(new QueueElementIF() {});<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (InterruptedException e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }</DIV>
<DIV class=RTE>&nbsp; static class Consumer implements Runnable {<BR>&nbsp;&nbsp;&nbsp; public void run() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (true) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q.blocking_dequeue();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (InterruptedException e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }<BR>}<BR></DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>&nbsp;</DIV></div></html>



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/