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 T
dequeue()
Dequeue an element.T
dequeue(long timeout)
Dequeue an element.void
enqueue(T element)
Enqueue an element.boolean
isClosed()
Check if the queue is closed.static void
main(java.lang.String[] args)
Main for testing.T
peek()
Peek the topmost element without dequeuing it.java.util.List
setClosed(boolean closed)
Open/close the queue.int
size()
Return the size of the queue.static void
test1(java.lang.String[] args)
Main for testing.static void
test1b(java.lang.String[] args)
Main for testing.static void
test2(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:
enqueue
in interfaceIBlockingQueue<T>
- Parameters:
element
- The element.
-
dequeue
public T dequeue(long timeout) throws IBlockingQueue.ClosedException, TimeoutException
Dequeue an element.- Specified by:
dequeue
in 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.ClosedException
TimeoutException
-
peek
public T peek() throws IBlockingQueue.ClosedException
Peek the topmost element without dequeuing it.- Specified by:
peek
in 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:
dequeue
in 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:
isClosed
in interfaceIBlockingQueue<T>
-
setClosed
public java.util.List setClosed(boolean closed)
Open/close the queue.- Specified by:
setClosed
in 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:
size
in interfaceIBlockingQueue<T>
- Returns:
- The size.
-
main
public static void main(java.lang.String[] args) throws java.lang.InterruptedException
Main for testing.- Throws:
java.lang.InterruptedException
-
test1
public static void test1(java.lang.String[] args) throws java.lang.InterruptedException
Main for testing.- Throws:
java.lang.InterruptedException
-
test1b
public static void test1b(java.lang.String[] args) throws java.lang.InterruptedException
Main for testing.- Throws:
java.lang.InterruptedException
-
test2
public static void test2(java.lang.String[] args) throws java.lang.InterruptedException
Main for testing.- Throws:
java.lang.InterruptedException
-
-