package UK.co.demon.xinit.utility;
/*
* NAME : Fifo - a thread safe fifo implementation.
*
* NOTES : This Fifo example is shamelessly taken from
* "Concurrent Programming in Java" by Doug Lea.
* Only the names have been simplified. Read the
* book, it's well worth the effort
*/
public
class Fifo
{
private FIFOnode head_;
private FIFOnode last_;
private Object lockOnLast_;
public
Fifo()
{
head_ = last_ = new Fifo(null, null);
lockOnLast_ = new Object();
}
public
void
put(Object x)
{
Fifo newNode = new Fifo(x, null);
synchronized (lockOnLast_)
{
last_.next = newNode;
last_ = newNode;
}
}
public
synchronized
Object
take(Object x)
{
Object x = null;
Fifo first = head_.next;
if (first != null)
{
x = first.value;
head_ = first;
}
return x;
}
}
final
class
FIFOnode
{
Object value;
FIFOnode next;
FIFOnode(Object x, FIFOnode n)
{
value = x;
next = n;
}
}
Last Updated