|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--com.softplc.Queue
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 |
protected Semaphore notEmpty
protected Semaphore notFull
protected java.lang.Object readLock
protected java.lang.Object writeLock
protected int count
protected int head
protected int tail
protected java.lang.Object[] array
protected int size
protected int localCountIn
protected int localCountOut
| Constructor Detail |
public Queue(int size)
int - size of the Queue.| Method Detail |
public java.lang.Object getElem()
throws java.lang.InterruptedException
java.lang.InterruptedException - if the Queue was empty and while
blocking, another Thread interrupted the caller.
public void putElem(java.lang.Object o)
throws java.lang.InterruptedException
java.lang.InterruptedException - if the Queue is full and while blocking
another Thread interrupted the caller.public int getCount()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||