Synchronized Queue
/*
* Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
*
* Project: OpenSubsystems
*
* $Id: SynchronizedQueue.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.util.LinkedList;
import java.util.List;
/**
* Class that implement unlimited queue, that is synchronized. It means that
* the consumer of the objects from the queue waits/is blocked in the get
* method until there is an object available.
*
* @version $Id: SynchronizedQueue.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
* @author Miro Halas
* @code.reviewer Miro Halas
* @code.reviewed Initial revision
*/
public class SynchronizedQueue
{
// Attributes ///////////////////////////////////////////////////////////////
/**
* Cache of object produced by producer and consumed by consumer.
*/
protected List m_lstObjects;
// Constructors /////////////////////////////////////////////////////////////
/**
* Constructor for Synchronized Queue Object.
*/
public SynchronizedQueue(
)
{
super();
m_lstObjects = new LinkedList();
}
// Logic ////////////////////////////////////////////////////////////////////
/**
* Destructor for Synchronized Queue. It is called when no other
* object holds reference to it.
*
* @exception Throwable - default destructor exception
*/
protected void finalize(
) throws Throwable
{
// Explicitely remove this just to help garbage collector
m_lstObjects.clear();
m_lstObjects = null;
super.finalize();
}
/**
* Get the object from the beginning of the queue
*
* @return Object - object from the queue, if the thread is blocked in this
* function and you call interrupt method, an InterruptedException
* will be thrown.
* @exception InterruptedException - if the thread is blocked in this
* function and you call interrupt method,
* an InterruptedException will be thrown.
*/
public synchronized Object get(
) throws InterruptedException
{
Object objReturn = null;
if (m_lstObjects.isEmpty())
{
// There is no object in the queue, go to sleep
try
{
wait();
}
catch (InterruptedException ieException)
{
// Somebody woke us up, that means all threads waiting on this
// object competed for the lock and this one won and the object is
// locked again
// The thread can be woken up in two conditions, producer put new
// object into the queue or somebody called interrupt - to interrupt
// the wait - in this case rethrow an exception
if (m_lstObjects.isEmpty())
{
throw ieException;
}
}
}
// Remove the first object in the queue
objReturn = m_lstObjects.remove(0);
return objReturn;
}
/**
* Put the object to the end of the queue.
*
* @param objNew - new object, can be null
*/
public synchronized void put(
Object objNew
)
{
m_lstObjects.add(objNew);
// New object in the queue, notify others
notifyAll();
}
/**
* Test if the queue is empty.
*
* @return boolean - true if the queue is empty
*/
public synchronized boolean isEmpty(
)
{
return m_lstObjects.isEmpty();
}
}
Related examples in the same category