org.progeeks.util.thread
Class CircularQueue

java.lang.Object
  extended by org.progeeks.util.thread.CircularQueue

public class CircularQueue
extends java.lang.Object

A thread safe FIFO data structure. Internally this uses an Object array to store objects. Objects are enqueued and dequeued in round-robin fashion. Once this queue is created then there is no further allocation done. Enqueues and dequeues are very efficient since it only requires two integer modifications and an array referencing.

Version:
$Revision: 1.1 $
Author:
Paul Speed

Field Summary
protected  int capacity
           
protected  java.lang.Object[] data
           
static int DEFAULT_CAPACITY
           
protected  int head
           
protected  int tail
           
 
Constructor Summary
CircularQueue()
          Creates a new CircularQueue object with the DEFAULT_CAPACITY.
CircularQueue(int capacity)
          Creates a new CircularQueue object with the specified capacity.
 
Method Summary
 java.lang.Object dequeue()
          Removes an object from the front of the queue.
 void enqueue(java.lang.Object obj)
          Adds an object to the end of the queue.
protected  int next(int current)
          Returns the next index based on the passed index, wrapping as appropriate.
 void waitForEmpty()
          Blocks until the queue is empty.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CAPACITY

public static final int DEFAULT_CAPACITY
See Also:
Constant Field Values

capacity

protected int capacity

head

protected int head

tail

protected int tail

data

protected java.lang.Object[] data
Constructor Detail

CircularQueue

public CircularQueue()
Creates a new CircularQueue object with the DEFAULT_CAPACITY.


CircularQueue

public CircularQueue(int capacity)
Creates a new CircularQueue object with the specified capacity.

Method Detail

enqueue

public void enqueue(java.lang.Object obj)
Adds an object to the end of the queue. This method will block if the queue is full.


dequeue

public java.lang.Object dequeue()
                         throws java.lang.InterruptedException
Removes an object from the front of the queue. This method will block if the queue is empty.

Throws:
java.lang.InterruptedException

waitForEmpty

public void waitForEmpty()
                  throws java.lang.InterruptedException
Blocks until the queue is empty.

Throws:
java.lang.InterruptedException

next

protected final int next(int current)
Returns the next index based on the passed index, wrapping as appropriate. This method is final to give the compiler the best chance to inline.



Copyright © 2002-2003 Paul Speed. All Rights Reserved.