Package jadex.commons.collection
Class BlockingQueue<T>
- java.lang.Object
-
- jadex.commons.collection.BlockingQueue<T>
-
- All Implemented Interfaces:
IBlockingQueue<T>
public class BlockingQueue<T> extends java.lang.Object implements IBlockingQueue<T>
A blocking queue allows to enqueue or dequeue elements. It blocks, when it is tried to dequeue an element, but the queue is empty.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface jadex.commons.collection.IBlockingQueue
IBlockingQueue.ClosedException
-
-
Constructor Summary
Constructors Constructor Description BlockingQueue()Create a blocking queue.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Tdequeue()Dequeue an element.Tdequeue(long timeout)Dequeue an element.voidenqueue(T element)Enqueue an element.booleanisClosed()Check if the queue is closed.static voidmain(java.lang.String[] args)Main for testing.Tpeek()Peek the topmost element without dequeuing it.java.util.ListsetClosed(boolean closed)Open/close the queue.intsize()Return the size of the queue.static voidtest1(java.lang.String[] args)Main for testing.static voidtest1b(java.lang.String[] args)Main for testing.static voidtest2(java.lang.String[] args)Main for testing.
-
-
-
Field Detail
-
elems
protected java.util.List<T> elems
The element storage.
-
closed
protected volatile boolean closed
The queue state.
-
monitor
protected java.lang.Object monitor
The monitor.
-
-
Method Detail
-
enqueue
public void enqueue(T element)
Enqueue an element.- Specified by:
enqueuein interfaceIBlockingQueue<T>- Parameters:
element- The element.
-
dequeue
public T dequeue(long timeout) throws IBlockingQueue.ClosedException, TimeoutException
Dequeue an element.- Specified by:
dequeuein interfaceIBlockingQueue<T>- Parameters:
timeout- the time to wait (in millis) or -1 for no timeout.- Returns:
- The element. When queue is empty the methods blocks until an element is added or the timeout occurs.
- Throws:
IBlockingQueue.ClosedExceptionTimeoutException
-
peek
public T peek() throws IBlockingQueue.ClosedException
Peek the topmost element without dequeuing it.- Specified by:
peekin interfaceIBlockingQueue<T>- Returns:
- The element. When queue is empty the methods blocks until an element is added.
- Throws:
IBlockingQueue.ClosedException
-
dequeue
public T dequeue() throws IBlockingQueue.ClosedException
Dequeue an element.- Specified by:
dequeuein interfaceIBlockingQueue<T>- Returns:
- The element. When queue is empty the methods blocks until an element is added.
- Throws:
IBlockingQueue.ClosedException
-
isClosed
public boolean isClosed()
Check if the queue is closed.- Specified by:
isClosedin interfaceIBlockingQueue<T>
-
setClosed
public java.util.List setClosed(boolean closed)
Open/close the queue.- Specified by:
setClosedin interfaceIBlockingQueue<T>- Parameters:
closed- The closed state.- Returns:
- The remaining elements after the queue has been closed.
-
size
public int size()
Return the size of the queue.- Specified by:
sizein interfaceIBlockingQueue<T>- Returns:
- The size.
-
main
public static void main(java.lang.String[] args) throws java.lang.InterruptedExceptionMain for testing.- Throws:
java.lang.InterruptedException
-
test1
public static void test1(java.lang.String[] args) throws java.lang.InterruptedExceptionMain for testing.- Throws:
java.lang.InterruptedException
-
test1b
public static void test1b(java.lang.String[] args) throws java.lang.InterruptedExceptionMain for testing.- Throws:
java.lang.InterruptedException
-
test2
public static void test2(java.lang.String[] args) throws java.lang.InterruptedExceptionMain for testing.- Throws:
java.lang.InterruptedException
-
-