Project JXTA

net.jxta.impl.util
Class ProducerBiasedQueue

java.lang.Object
  extended bynet.jxta.impl.util.UnbiasedQueue
      extended bynet.jxta.impl.util.ProducerBiasedQueue

public class ProducerBiasedQueue
extends UnbiasedQueue

A queue who's implementation is biased towards effciency in adding elements to the queue.

Since:
JXTA 1.0

Nested Class Summary
 
Nested classes inherited from class net.jxta.impl.util.UnbiasedQueue
UnbiasedQueue.SynchronizedQueue
 
Field Summary
 
Fields inherited from class net.jxta.impl.util.UnbiasedQueue
closeFlag, DEFAULT_MAX_OBJECTS, DROP_OLDEST_OBJECT, dropOldestObject, DROPPED_OBJECT_WARNING_INTERVAL, maxObjects, nextDroppedWarn, numDequeued, numDropped, numEnqueued, queue, sumOfQueueSizesDequeue, sumOfQueueSizesEnqueue
 
Constructor Summary
ProducerBiasedQueue()
          Default constructor. 100 element FIFO queue which drops oldest element when full.
ProducerBiasedQueue(int size, boolean dropOldest)
          Full featured constructor for creating a new ConsumerBiasedQueue.
 
Method Summary
 void clear()
          Flush the queue of all pending objects.
 double getAvgInQueueAtDequeue()
          Return the average number of elements in the queue at dequeue time.
 double getAvgInQueueAtEnqueue()
          Return the average number of elements in the queue at Enqueue time.
 int getCurrentInQueue()
          Return the number of elements currently in the queue.
 Object pop()
          Return next obj in the queue if there is one.
 Object pop(long timeout)
          Gets a Object from the queue.
 Object[] popMulti(int maxObjs)
          Returns an array of objects, possibly empty, from the queue.
 boolean push(Object obj)
          Attempt to push an object onto the queue.
 void setMaxQueueSize(int maxObjs)
          Set how many objects this queue may store.
 
Methods inherited from class net.jxta.impl.util.UnbiasedQueue
close, getMaxQueueSize, getNumDequeued, getNumDropped, getNumEnqueued, interrupt, isClosed, peek, push, pushBack, pushBack, synchronizedQueue, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ProducerBiasedQueue

public ProducerBiasedQueue()
Default constructor. 100 element FIFO queue which drops oldest element when full.


ProducerBiasedQueue

public ProducerBiasedQueue(int size,
                           boolean dropOldest)
Full featured constructor for creating a new ConsumerBiasedQueue.

Parameters:
size - Queue will be not grow larger than this size. Use Integer.MAX_VALUE for "unbounded" queue size.
dropOldest - Controls behaviour of element insertion when the queue is full. If "true" and the queue is full upon a push operation then the oldest element will be dropped to be replaced with the element currently being pushed. If "false" then then newest item will be dropped.
Method Detail

clear

public void clear()
Flush the queue of all pending objects.

Overrides:
clear in class UnbiasedQueue

push

public boolean push(Object obj)
Description copied from class: UnbiasedQueue
Attempt to push an object onto the queue. If the queue is full then the object will not be pushed. This method does not use any synchronization and should not be used if other threads are using UnbiasedQueue.pop(long) to retrieve elements.

Overrides:
push in class UnbiasedQueue
Parameters:
obj - object to push
Returns:
true if the obj was pushed, otherwise false.

pop

public Object pop()
Return next obj in the queue if there is one.

Overrides:
pop in class UnbiasedQueue
Returns:
Object, null if the queue is empty

pop

public Object pop(long timeout)
           throws InterruptedException
Gets a Object from the queue. If no Object is immediately available, then wait the specified amount of time. Per Java convention, a timeout of zero (0) means wait an infinite amount of time. Negative values mean do not wait at all.

Overrides:
pop in class UnbiasedQueue
Parameters:
timeout - amount of time to wait in milliseconds for an object to be available. Per Java convention, a timeout of zero (0) means wait an infinite amount of time. Negative values mean do not wait at all.
Returns:
The next object in the queue.
Throws:
InterruptedException - if the operation is interrupted before the timeout interval is completed.

popMulti

public Object[] popMulti(int maxObjs)
Returns an array of objects, possibly empty, from the queue.

Overrides:
popMulti in class UnbiasedQueue
Parameters:
maxObjs - the maximum number of items to return.
Returns:
an array of objects, possibly empty containing the returned queue elements.

setMaxQueueSize

public void setMaxQueueSize(int maxObjs)
Set how many objects this queue may store. Note that if there are more objects already in the queue than the specified amount then the queue will retain its current capacity.

Overrides:
setMaxQueueSize in class UnbiasedQueue
Parameters:
maxObjs - The number of objects which the queue must be able to store.

getCurrentInQueue

public int getCurrentInQueue()
Return the number of elements currently in the queue. This method is useful for statistical sampling, but should not be used to determine program logic due to the multi-threaded behaviour of these queues. You should use the return values and timeout behaviour of the push() and pop() methods to regulate how you use the queue.

Overrides:
getCurrentInQueue in class UnbiasedQueue
Returns:
the number of elements currently in the queue. Be warned that even two sequential calls to this method may return different answers due to activity on other threads.

getAvgInQueueAtEnqueue

public double getAvgInQueueAtEnqueue()
Return the average number of elements in the queue at Enqueue time.

Overrides:
getAvgInQueueAtEnqueue in class UnbiasedQueue
Returns:
average number of elements which were in the queue at during all of the "push" operations which returned a "true" result. Does not include the item being pushed. If no elements have ever been enqueued then "NaN" will be returned.

getAvgInQueueAtDequeue

public double getAvgInQueueAtDequeue()
Return the average number of elements in the queue at dequeue time.

Overrides:
getAvgInQueueAtDequeue in class UnbiasedQueue
Returns:
average number of elements which were in the queue at during all of the "pop" operations which returned a non-null result. Includes the item being "pop"ed in the average. If no elements have ever been dequeued then "NaN" will be returned.

JXTA J2SE