com.softplc
Class Queue

java.lang.Object
  |
  +--com.softplc.Queue

public class Queue
extends java.lang.Object

A Queue is a data Object that allows one Thread to pass an Object to another Thread.

A Queue can store any type of Object. When the Queue is full, the producer will block on the next call to putElem(). The producer Thread stays blocked until a consumer Thread calls getElem(). Likewise if the Queue is empty, the consumer Thread(s) will block on the next call to getElem(), and will stay blocked until the producer calls putElem().

This implementation is fully "thread safe" for any number of putter and any number of getter Threads. If more than one Thread is getting elements off the Queue, there is a bit of a free for all, and which Thread actually retrieves the next Object is unpredictable. Any Threads that don't get the next Object remain blocked. For a situation where you have a pool of Threads all waiting to service a similar type of event, they can all block in getElem() until event(s) arrive.

The behavior is that of a FIFO, with a maximum holding capacity provided to the constructor.


Field Summary
protected  java.lang.Object[] array
           
protected  int count
           
protected  int head
           
protected  int localCountIn
           
protected  int localCountOut
           
protected  Semaphore notEmpty
          Signaled whenever the count goes from 0 to 1
protected  Semaphore notFull
          Signaled whenever the count goes from size to size-1
protected  java.lang.Object readLock
          A critical section lock for getElem().
protected  int size
           
protected  int tail
           
protected  java.lang.Object writeLock
          A critical section lock for putElem().
 
Constructor Summary
Queue(int size)
          Construct an Object Queue of a given size.
 
Method Summary
 int getCount()
          Return the count of elements in the Queue
 java.lang.Object getElem()
          Remove an element from the Queue, if the Queue is empty, block until another Thread puts an Object into the Queue.
 void putElem(java.lang.Object o)
          Put an Object into the Queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

notEmpty

protected Semaphore notEmpty
Signaled whenever the count goes from 0 to 1

notFull

protected Semaphore notFull
Signaled whenever the count goes from size to size-1

readLock

protected java.lang.Object readLock
A critical section lock for getElem(). This lock makes the Queue fully thread safe.

writeLock

protected java.lang.Object writeLock
A critical section lock for putElem(). This lock makes the Queue fully thread safe.

count

protected int count

head

protected int head

tail

protected int tail

array

protected java.lang.Object[] array

size

protected int size

localCountIn

protected int localCountIn

localCountOut

protected int localCountOut
Constructor Detail

Queue

public Queue(int size)
Construct an Object Queue of a given size.
Parameters:
int - size of the Queue.
Method Detail

getElem

public java.lang.Object getElem()
                         throws java.lang.InterruptedException
Remove an element from the Queue, if the Queue is empty, block until another Thread puts an Object into the Queue.
Throws:
java.lang.InterruptedException - if the Queue was empty and while blocking, another Thread interrupted the caller.

putElem

public void putElem(java.lang.Object o)
             throws java.lang.InterruptedException
Put an Object into the Queue. If the Queue is full, block until another Thread calls getElem() in this Queue.
Throws:
java.lang.InterruptedException - if the Queue is full and while blocking another Thread interrupted the caller.

getCount

public int getCount()
Return the count of elements in the Queue