--- projects/cms/source/util/uk/org/iscream/cms/util/Queue.java 2001/03/01 01:05:49 1.14 +++ projects/cms/source/util/uk/org/iscream/cms/util/Queue.java 2002/05/21 16:47:19 1.25 @@ -1,11 +1,31 @@ +/* + * i-scream central monitoring system + * http://www.i-scream.org.uk + * Copyright (C) 2000-2002 i-scream + * + * 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; either version 2 + * of the License, or (at your option) any later version. + * + * 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. + */ + //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.util; +package uk.org.iscream.cms.server.util; //---IMPORTS--- import java.util.LinkedList; import java.util.NoSuchElementException; import java.util.Random; -import uk.ac.ukc.iscream.util.*; +import uk.org.iscream.cms.server.util.*; /** * A Queue class designed to operate in a multi-threaded environment, with @@ -14,7 +34,7 @@ import uk.ac.ukc.iscream.util.*; * actually contains some elements. * * @author $Author: tdb $ - * @version $Id: Queue.java,v 1.14 2001/03/01 01:05:49 tdb Exp $ + * @version $Id: Queue.java,v 1.25 2002/05/21 16:47:19 tdb Exp $ */ public class Queue { @@ -23,7 +43,7 @@ public class Queue { /** * The current CVS revision of this class */ - public static final String REVISION = "$Revision: 1.14 $"; + public static final String REVISION = "$Revision: 1.25 $"; /** * Pass to constructor to remove a RANDOM item from @@ -49,6 +69,11 @@ public class Queue { */ public static final int DROP = 3; + /** + * To allow opposite lookups. + */ + public static final String[] algorithms = {"RANDOM", "FIRST", "LAST", "DROP"}; + //---STATIC METHODS--- //---CONSTRUCTORS--- @@ -89,26 +114,26 @@ public class Queue { for(int i=0; i < _lists.size(); i++) { // skip over any gaps left in the list if(_lists.get(i) != null) { - // get size before adding to the Queue - int s = ((LinkedList) _lists.get(i)).size(); - // check whether we need to remove an item from the current Queue - if(_maxSize!=-1 && s==_maxSize && _removeAlgorithm!=DROP) { - // we need to remove an item - removeQueueItem((LinkedList) _lists.get(i)); - } - // check if we should add (not if Queue full, and using DROP algorithm) - if(!(s==_maxSize && _removeAlgorithm==DROP)) { - // add the next item, ensuring we lock - synchronized(this) { - // LinkedList.add() does the same thing, but this ensures behaviour - ((LinkedList) _lists.get(i)).addLast(o); + synchronized(((LinkedList) _lists.get(i))) { + // get size before adding to the Queue + int s = ((LinkedList) _lists.get(i)).size(); + // check whether we need to remove an item from the current Queue + if(_maxSize!=-1 && s>=_maxSize && _removeAlgorithm!=DROP) { + // we need to remove an item + removeQueueItem((LinkedList) _lists.get(i)); } - } - // if the queue was empty before the add it is possible - // that a consumer is waiting... so we notify them - if (s == 0) { - synchronized(((LinkedList) _lists.get(i))) { - ((LinkedList) _lists.get(i)).notifyAll(); + // check if we should add (not if Queue full, and using DROP algorithm) + if(!(s>=_maxSize && _removeAlgorithm==DROP)) { + // add the next item, ensuring we lock + synchronized(this) { + // LinkedList.add() does the same thing, but this ensures behaviour + ((LinkedList) _lists.get(i)).addLast(o); + } + // if the queue was empty before the add it is possible + // that a consumer is waiting... so we notify them + //synchronized(((LinkedList) _lists.get(i))) { + ((LinkedList) _lists.get(i)).notifyAll(); + //} } } } @@ -131,8 +156,8 @@ public class Queue { throw new InvalidQueueException("Requested queue "+queue+" does not exist"); } // block if the queue is empty - if (((LinkedList) _lists.get(queue)).size() == 0) { - synchronized(((LinkedList) _lists.get(queue))) { + synchronized(((LinkedList) _lists.get(queue))) { + if (((LinkedList) _lists.get(queue)).size() == 0) { try { ((LinkedList) _lists.get(queue)).wait(); } catch(Exception e) {} } } @@ -201,7 +226,7 @@ public class Queue { if(_maxSize != -1) { status += " maxSize=\""+_maxSize+"\""; } - status += ""; + status += ">"; return status; } @@ -223,7 +248,8 @@ public class Queue { /** * Returns the total numer of elements to have passed - * through this queue (ie. a counter on the add method). + * through this queue. This is essentially a counter + * on the add method. * * @return the element-ometer. */ @@ -242,7 +268,7 @@ public class Queue { * * @return An integer to be passed to the get() method. */ - public int getQueue() { + public synchronized int getQueue() { int pos = -1; for(int i=0; i < _lists.size(); i++) { if(_lists.get(i) == null) { @@ -266,7 +292,7 @@ public class Queue { * * @param queue The integer identifier for the queue, given by getQueue(). */ - public void removeQueue(int queue) { + public synchronized void removeQueue(int queue) { _lists.set(queue, null); } @@ -327,7 +353,7 @@ public class Queue { * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * - * This uses the uk.ac.ukc.iscream.util.FormatName class + * This uses the uk.org.iscream.cms.server.util.FormatName class * to format the toString() * * @return the name of this class and its CVS revision.