Added some notes about which append to use when you have multiple consumers.

Moved redundant code into a method where it can be shared.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1587 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2005-02-09 22:40:43 +00:00
parent 10430edeec
commit 066260a3cb
@@ -77,21 +77,13 @@ public class Queue
} }
/** /**
* Appends the supplied item to the end of the queue. * Appends the supplied item to the end of the queue, and notify a
* consumer if and only if the queue was previously empty.
*/ */
public synchronized void append (Object item) public synchronized void append (Object item)
{ {
if (_count == _size) { // only notify if the queue was previously empty
makeMoreRoom(); append0(item, _count == 0);
}
_items[_end] = item;
_end = (_end + 1) % _size;
_count++;
if (_count == 1) {
notify();
}
} }
/** /**
@@ -100,22 +92,29 @@ public class Queue
*/ */
public synchronized void appendSilent (Object item) public synchronized void appendSilent (Object item)
{ {
if (_count == _size) { append0(item, false);
makeMoreRoom();
}
_items[_end] = item;
_end = (_end + 1) % _size;
_count++;
} }
/** /**
* Appends an item to the queue and notify any listeners regardless of * Appends an item to the queue and notify a listener regardless of
* how many items are on the queue. Use this for the last item you * how many items are on the queue. Use this for the last item you
* append to a queue in a batch via <code>appendSilent</code> because * append to a queue in a batch via <code>appendSilent</code> because
* the regular <code>append</code> will think it doesn't need to * the regular <code>append</code> will think it doesn't need to
* notify anyone because the queue size isn't zero prior to this add. * notify anyone because the queue size isn't zero prior to this add.
* You should also use this method if you have mutiple consumers
* listening waiting on the queue, to guarantee that one will be woken
* for every element added.
*/ */
public synchronized void appendLoud (Object item) public synchronized void appendLoud (Object item)
{
append0(item, true);
}
/**
* Internal append method. If subclassing queue, be sure to call
* this method from inside a synchronized block.
*/
protected void append0 (Object item, boolean notify)
{ {
if (_count == _size) { if (_count == _size) {
makeMoreRoom(); makeMoreRoom();
@@ -123,7 +122,10 @@ public class Queue
_items[_end] = item; _items[_end] = item;
_end = (_end + 1) % _size; _end = (_end + 1) % _size;
_count++; _count++;
notify();
if (notify) {
notify();
}
} }
/** /**