simple.util
Class BlockingQueue

java.lang.Object
  extended by simple.util.BlockingQueue
All Implemented Interfaces:
java.io.Serializable

public class BlockingQueue
extends java.lang.Object
implements java.io.Serializable

This provides a quick implementation for a thread safe queue. This ensures that if there are several threads enqueuing or dequeuing from this queue that no thread will dequeue a null.

This will block a thread from dequeuing when there is not items in the queue. It will also block an enqueuer if there is no space left in the queue.

When a thread is waiting and gets notified by another thread the thread that has been notified reasserts the check for the full or empty case, this means that there will be no problems if there are several enqueuing threads and several dequeuing threads.

Author:
Niall Gallagher
See Also:
Serialized Form

Constructor Summary
BlockingQueue()
          When the size of the BlockingQueue is not specified then the default size of 30 is set, this BlockingQueue cannot resize so care is need when picking a size.
BlockingQueue(int capacity)
          If the capacity given is less than 1 then the size of the queue is increased so that it becomes 1 this means that the queue can tolerate at least one add by a thread.
 
Method Summary
 java.lang.Object dequeue()
          If this dequeues from a full queue it notifys any enqueuers who were waiting to add to the full queue, so that they do not block.
 void enqueue(java.lang.Object object)
          If this enqueues to an empty queue it notifys any dequeuers that were trying to take from the empty queue.
 int length()
          This returns the number of objects that are actually enqueued into the BlockingQueue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BlockingQueue

public BlockingQueue()
When the size of the BlockingQueue is not specified then the default size of 30 is set, this BlockingQueue cannot resize so care is need when picking a size.


BlockingQueue

public BlockingQueue(int capacity)
If the capacity given is less than 1 then the size of the queue is increased so that it becomes 1 this means that the queue can tolerate at least one add by a thread.

Parameters:
capacity - the initial size that this queue can handle
Method Detail

enqueue

public void enqueue(java.lang.Object object)
             throws java.lang.InterruptedException
If this enqueues to an empty queue it notifys any dequeuers that were trying to take from the empty queue. This ensures that a dequeuer can wake-up an will not block indefinitely.

Parameters:
object - the object to enqueue into this BlockingQueue.
Throws:
java.lang.InterruptedException - if an interrupt is issued.

dequeue

public java.lang.Object dequeue()
                         throws java.lang.InterruptedException
If this dequeues from a full queue it notifys any enqueuers who were waiting to add to the full queue, so that they do not block.

Returns:
an object from the queue, this will never be a null object
Throws:
java.lang.InterruptedException - is thrown if an interrupt is issued

length

public int length()
This returns the number of objects that are actually enqueued into the BlockingQueue. If the length is zero then a dequeue method will block.

Returns:
the number of objects that is currently in the queue