Package fr.dyade.aaa.common
Class Queue
- java.lang.Object
-
- fr.dyade.aaa.common.Queue
-
- All Implemented Interfaces:
Serializable
public class Queue extends Object implements Serializable
TheQueueclass implements a First-In-First-Out (FIFO) list of objects.A queue is for the exclusive use of one single consumer, whereas many producers may access it. It is ready for use after instantiation. A producer may wait for the queue to be empty by calling the
stop()method. This method returns when the queue is actually empty, and prohibits any further call to thepushmethod. To be able to use the queue again, it must be re-started through thestart()method.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private booleanclosedtrueif the queue has been closed.private List<Object>elementsThe list holding queue elements.private static longserialVersionUIDdefine serialVersionUID for interoperabilityprivate booleanstoppingtrueif a producer called thestop()method.
-
Constructor Summary
Constructors Constructor Description Queue()Constructs aQueueinstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all of the elements from this queue.voidclose()Closes the queue.Objectget()Waits for an object to be pushed in the queue, and eventually returns it without removing it.Objectget(long timeout)Waits for an object to be pushed in the queue or a specified amount of time has elapsed.ObjectgetAndPop()Waits for an object to be pushed in the queue, then removes and returns the object at the top of this queue.booleanisEmpty()Returns true if this queue contains no elements.Stringlist()Returns a stringified form of the queue.Objectpop()Removes and returns the object at the top of this queue.voidpush(Object item)Pushes an item at the end of this queue.booleansearch(Comparator<Object> c, Object o2)intsize()Returns the number of elements in this list.voidstart()Authorizes the use of the queue by producers.voidstop()Stops the queue by returning when it is empty and prohibiting any further producers call to thepushmethod.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
define serialVersionUID for interoperability- See Also:
- Constant Field Values
-
stopping
private boolean stopping
trueif a producer called thestop()method.
-
closed
private boolean closed
trueif the queue has been closed.
-
-
Method Detail
-
search
public boolean search(Comparator<Object> c, Object o2)
-
push
public void push(Object item)
Pushes an item at the end of this queue.- Parameters:
item- The item to be pushed at the end of this queue.- Throws:
StoppedQueueException- If the queue is stopping or stopped.
-
get
public Object get() throws InterruptedException
Waits for an object to be pushed in the queue, and eventually returns it without removing it.- Returns:
- The object at the top of this queue.
- Throws:
InterruptedException- interrupted.
-
get
public Object get(long timeout) throws InterruptedException
Waits for an object to be pushed in the queue or a specified amount of time has elapsed. The first object is returned without removing it.- Parameters:
timeout- The maximum time to wait in milliseconds.- Returns:
- The object at the top of this queue.
- Throws:
InterruptedException- interrupted.
-
pop
public Object pop()
Removes and returns the object at the top of this queue.- Returns:
- The object at the top of this queue.
- Throws:
EmptyQueueException- If the queue is empty.
-
getAndPop
public Object getAndPop() throws InterruptedException
Waits for an object to be pushed in the queue, then removes and returns the object at the top of this queue.- Returns:
- The object at the top of this queue.
- Throws:
InterruptedException- interrupted.
-
start
public void start()
Authorizes the use of the queue by producers.
-
stop
public void stop() throws InterruptedExceptionStops the queue by returning when it is empty and prohibiting any further producers call to thepushmethod.- Throws:
InterruptedException- interrupted.
-
close
public void close()
Closes the queue. Interrupts all threads blocked onget()with anInterruptedException.
-
isEmpty
public boolean isEmpty()
Returns true if this queue contains no elements.- Returns:
- true if this queue contains no elements, false otherwise.
-
clear
public void clear()
Removes all of the elements from this queue.
-
size
public int size()
Returns the number of elements in this list.- Returns:
- the number of elements in this list.
-
list
public String list()
Returns a stringified form of the queue.- Returns:
- a stringified form of the queue.
-
-