org.exolab.core.util
Class FifoQueue

java.lang.Object
  extended by org.exolab.core.util.FifoQueue

public class FifoQueue
extends java.lang.Object

This is a very simple implementation of a FIFO queue. The queue only holds object types, and is created with a fixed size, and will NOT grow during its life. The queue is thread safe.

Items can be added or removed to and from the queue either one at a time or in bulk. When the queue is full any attempt to add another object will block until the queue has space for it. Any attempt to get an object from an empty queue will block until at least one item has been added.

Clients can check to see if the queue is full or empty with available helper methods.

When objects are added or removed from the queue, a notify all is called to wake any sleeping threads waiting on a queue resource.

Version:
$Revision: 1.1 $ $Date: 2000/03/13 10:29:15 $
Author:
Jim Mourikis

Constructor Summary
FifoQueue(int capacity)
          The queue constructor.
 
Method Summary
 void add(java.lang.Object obj)
          Add a new object to the head of the queue.
 void add(java.lang.Object[] list)
          Add a new list of objects to the head of the queue.
 java.lang.Object get()
          Remove and return the object at the end of the queue.
 java.lang.Object[] getAll()
          Remove and return all the objects in the queue in the order they were added.
 int getCapacity()
          Get the queues max capacity.
 int getSize()
          Get the number of elements the queue currently holds
 boolean isEmpty()
          Return true if the queue currently holds no elements.
 boolean isFull()
          Return true if the queue is currently full.
 void waitUntilEmpty()
          Wait until the queue becomes empty.
 boolean waitUntilEmpty(long timeout)
          Wait until timeout if the queue is empty.
 void waitUntilFull()
          Wait until the queue becomes full.
 void waitWhileEmpty()
          Wait while the queue is empty.
 void waitWhileFull()
          Wait while the queue is full.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FifoQueue

public FifoQueue(int capacity)
The queue constructor. Can only be created with a maximum capacity. An empty queue is initially created.

Parameters:
capacity - the total number of elements this queue is allowed to hold
Method Detail

getCapacity

public int getCapacity()
Get the queues max capacity. Does not need to be synchronized, since it cannot be changed once the queue is created.


getSize

public int getSize()
Get the number of elements the queue currently holds


isEmpty

public boolean isEmpty()
Return true if the queue currently holds no elements.


isFull

public boolean isFull()
Return true if the queue is currently full.


add

public void add(java.lang.Object obj)
         throws java.lang.InterruptedException
Add a new object to the head of the queue. And inform any waiting threads. No checks are performed for duplicates. This call will block if the queue is full, until space is available.

Parameters:
obj - The new object to add to the queue.
Throws:
java.lang.InterruptedException - If interupted while waiting

add

public void add(java.lang.Object[] list)
         throws java.lang.InterruptedException
Add a new list of objects to the head of the queue. And inform any waiting threads. No checks are performed for duplicates. This call will block if the queue is full, until space is available.

Parameters:
list - The list of objects to add to the queue.
Throws:
java.lang.InterruptedException - If interupted while waiting

get

public java.lang.Object get()
                     throws java.lang.InterruptedException
Remove and return the object at the end of the queue. Notify any waiting threads, once an object has been removed. If the queue is currently empty this call will block until an item is added.

Throws:
java.lang.InterruptedException - If interupted while waiting

getAll

public java.lang.Object[] getAll()
                          throws java.lang.InterruptedException
Remove and return all the objects in the queue in the order they were added. Notify any waiting threads, once an object has been removed. If the queue is currently empty this call will block until an item is added.

If the queue is empty an empty list is returned.

Throws:
java.lang.InterruptedException - If interupted while waiting

waitUntilEmpty

public boolean waitUntilEmpty(long timeout)
                       throws java.lang.InterruptedException
Wait until timeout if the queue is empty. If timteout is 0 this call will block until an item is inserted into the queue. If there are items in the queue this call will return imediately. The return value is true if there are items available in the queue else false is return if the timeout was exceeded.

Note: when there are multiple threads waiting on a queue there is no guarantee which one will succeed first.

Parameters:
timeout - The time to wait for in ms.
Throws:
java.lang.InterruptedException - If interupted while waiting

waitUntilEmpty

public void waitUntilEmpty()
                    throws java.lang.InterruptedException
Wait until the queue becomes empty.

Throws:
java.lang.InterruptedException - If interupted while waiting

waitWhileEmpty

public void waitWhileEmpty()
                    throws java.lang.InterruptedException
Wait while the queue is empty.

Throws:
java.lang.InterruptedException - If interupted while waiting

waitUntilFull

public void waitUntilFull()
                   throws java.lang.InterruptedException
Wait until the queue becomes full.

Throws:
java.lang.InterruptedException - If interupted while waiting

waitWhileFull

public void waitWhileFull()
                   throws java.lang.InterruptedException
Wait while the queue is full.

Throws:
java.lang.InterruptedException - If interupted while waiting


Copyright © 1999-2012 The Exolab Group. All Rights Reserved.