Package org.jgroups.util
Class Queue
- java.lang.Object
-
- org.jgroups.util.Queue
-
public class Queue extends java.lang.ObjectElements are added at the tail and removed from the head. Class is thread-safe in that 1 producer and 1 consumer may add/remove elements concurrently. The class is not explicitely designed for multiple producers or consumers. Implemented as a linked list, so that removal of an element at the head does not cause a right-shift of the remaining elements (as in a Vector-based implementation).- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.apache.commons.logging.Loglog
-
Constructor Summary
Constructors Constructor Description Queue()creates an empty queue
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(java.lang.Object obj)adds an object to the tail of this queue If the queue has been closed with close(true) no exception will be thrown if the queue has not been flushed yet.voidaddAll(java.util.Collection c)voidaddAll(java.util.List<java.lang.Object> list)voidaddAtHead(java.lang.Object obj)Adds a new object to the head of the queue basically (obj.equals(queue.remove(queue.add(obj)))) returns true If the queue has been closed with close(true) no exception will be thrown if the queue has not been flushed yet.voidclear()Removes all elements from the queue.voidclose(boolean flush_entries)Marks the queues as closed.booleanclosed()returns true if the Queue has been closed however, this method will return false if the queue has been closed using the close(true) method and the last element has yet not been received.java.lang.ObjectgetFirst()Returns the first element.java.lang.ObjectgetLast()Returns the last element.java.lang.Objectpeek()returns the first object on the queue, without removing it.java.lang.Objectpeek(long timeout)returns the first object on the queue, without removing it.java.lang.Objectremove()Removes 1 element from head or blocks until next element has been added or until queue has been closedjava.lang.Objectremove(long timeout)Removes 1 element from the head.voidremoveElement(java.lang.Object obj)removes a specific object from the queue.voidreset()resets the queue.intsize()returns the number of objects that are currently in the queuejava.lang.StringtoString()prints the size of the queuejava.util.LinkedListvalues()Returns all the elements of the queuevoidwaitUntilClosed(long timeout)Waits until the queue has been closed.
-
-
-
Method Detail
-
getFirst
public java.lang.Object getFirst()
Returns the first element. Returns null if no elements are available.
-
getLast
public java.lang.Object getLast()
Returns the last element. Returns null if no elements are available.
-
closed
public boolean closed()
returns true if the Queue has been closed however, this method will return false if the queue has been closed using the close(true) method and the last element has yet not been received.- Returns:
- true if the queue has been closed
-
add
public void add(java.lang.Object obj) throws QueueClosedExceptionadds an object to the tail of this queue If the queue has been closed with close(true) no exception will be thrown if the queue has not been flushed yet.- Parameters:
obj- - the object to be added to the queue- Throws:
QueueClosedException- exception if closed() returns true
-
addAll
public void addAll(java.util.Collection c) throws QueueClosedException- Throws:
QueueClosedException
-
addAll
public void addAll(java.util.List<java.lang.Object> list) throws QueueClosedException- Throws:
QueueClosedException
-
addAtHead
public void addAtHead(java.lang.Object obj) throws QueueClosedExceptionAdds a new object to the head of the queue basically (obj.equals(queue.remove(queue.add(obj)))) returns true If the queue has been closed with close(true) no exception will be thrown if the queue has not been flushed yet.- Parameters:
obj- - the object to be added to the queue- Throws:
QueueClosedException- exception if closed() returns true
-
remove
public java.lang.Object remove() throws QueueClosedExceptionRemoves 1 element from head or blocks until next element has been added or until queue has been closed- Returns:
- the first element to be taken of the queue
- Throws:
QueueClosedException
-
remove
public java.lang.Object remove(long timeout) throws QueueClosedException, TimeoutExceptionRemoves 1 element from the head. If the queue is empty the operation will wait for timeout ms. if no object is added during the timeout time, a Timout exception is thrown- Parameters:
timeout- - the number of milli seconds this operation will wait before it times out- Returns:
- the first object in the queue
- Throws:
QueueClosedExceptionTimeoutException
-
removeElement
public void removeElement(java.lang.Object obj) throws QueueClosedExceptionremoves a specific object from the queue. the object is matched up using the Object.equals method.- Parameters:
obj- the actual object to be removed from the queue- Throws:
QueueClosedException
-
peek
public java.lang.Object peek() throws QueueClosedExceptionreturns the first object on the queue, without removing it. If the queue is empty this object blocks until the first queue object has been added- Returns:
- the first object on the queue
- Throws:
QueueClosedException
-
peek
public java.lang.Object peek(long timeout) throws QueueClosedException, TimeoutExceptionreturns the first object on the queue, without removing it. If the queue is empty this object blocks until the first queue object has been added or the operation times out- Parameters:
timeout- how long in milli seconds will this operation wait for an object to be added to the queue before it times out- Returns:
- the first object on the queue
- Throws:
QueueClosedExceptionTimeoutException
-
clear
public void clear()
Removes all elements from the queue. This method can succeed even when the queue is closed
-
close
public void close(boolean flush_entries)
Marks the queues as closed. When anaddorremoveoperation is attempted on a closed queue, an exception is thrown.- Parameters:
flush_entries- When true, a end-of-entries marker is added to the end of the queue. Entries may be added and removed, but when the end-of-entries marker is encountered, the queue is marked as closed. This allows to flush pending messages before closing the queue.
-
waitUntilClosed
public void waitUntilClosed(long timeout)
Waits until the queue has been closed. Returns immediately if already closed- Parameters:
timeout- Number of milliseconds to wait. A value <= 0 means to wait forever
-
reset
public void reset()
resets the queue. This operation removes all the objects in the queue and marks the queue open
-
values
public java.util.LinkedList values()
Returns all the elements of the queue- Returns:
- A copy of the queue
-
size
public int size()
returns the number of objects that are currently in the queue
-
toString
public java.lang.String toString()
prints the size of the queue- Overrides:
toStringin classjava.lang.Object
-
-