tribble.util
Class FifoQueue

java.lang.Object
  extended by tribble.util.FifoQueue

public class FifoQueue
extends java.lang.Object

Generic FIFO queue. Implements a first-in first-out (FIFO) circular queue, with get and put methods.

Puts (writes) and gets (reads) to the queue are synchronized, so that a producer thread can add items to the queue while another thread is simultaneously retrieving items from the queue, without contention. However, this implementation reduces the number of synchronization calls required, making it more efficient than a fully synchronized queue class.

Note that this class is designed to support a single writer thread that calls the put() method, and a single reader thread that calls the get() method. Multiple unsynchronized writers and multiple readers are not supported by this class.

Since:
2007-06-21
Version:
$Revision: 1.1 $ $Date: 2007/06/21 21:27:28 $
Author:
David R. Tribble (david@tribble.com).

Copyright 2007 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated by by the United States Department of State as a terrorist, or terrorist government or agency, to use and distribute this source code provided that the original copyright notice remains present and unaltered.


Constructor Summary
FifoQueue()
          Default constructor.
FifoQueue(int size)
          Constructor.
 
Method Summary
protected  void finalize()
          Finalization.
 java.lang.Object get()
          Remove an item from the queue.
 boolean put(java.lang.Object item)
          Insert an item into the queue.
 int size()
          Determine the number of items in this queue.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FifoQueue

public FifoQueue()
Default constructor. Creates a FIFO queue of the default size (10 items).

Since:
1.1, 2007-06-21

FifoQueue

public FifoQueue(int size)
Constructor. Creates a FIFO queue containing a specified number of items.

Parameters:
size - Maximum number of items that the queue can hold at one time.
Throws:
java.lang.IllegalArgumentException - (unchecked) Thrown if size is less than 1.
Since:
1.1, 2007-06-21
Method Detail

put

public boolean put(java.lang.Object item)
Insert an item into the queue. Note that the first item written to the queue will be the first item to be read from the queue.

Note that this method is not synchronized, and does not need to be as long as there is only one producer thread calling this method at any given moment.

Parameters:
item - Object to add to the tail of the queue. This cannot be null.
Returns:
True if the item was inserted into the queue, otherwise false because the queue is full.
Throws:
java.lang.NullPointerException - (unchecked) Thrown if item is null.
Since:
1.1, 2007-06-21

get

public java.lang.Object get()
Remove an item from the queue. Note that the first item read from the queue is the first item that was written to the queue.

Note that this method is not synchronized, and does not need to be as long as there is only one consumer thread calling this method at any given moment.

Returns:
Next object in the queue, or null if the queue is empty.
Since:
1.1, 2007-06-21

size

public int size()
Determine the number of items in this queue.

Returns:
The current number of items in the queue.
Since:
1.1, 2007-06-21

finalize

protected void finalize()
                 throws java.lang.Throwable
Finalization.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable
Since:
1.1, 2007-06-21