E
- Type of element object to be queued.public class ThreadSafeQueue<E>
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
ThreadSafeQueue.Poison
Class for the termination token
|
Modifier and Type | Field and Description |
---|---|
private static ThreadSafeQueue.Poison |
POISON |
private java.util.concurrent.BlockingQueue<java.lang.Object> |
queue
Internal storage for the objects.
|
private boolean |
terminated
Flag indicating if all access to the queue should be ended.
|
Constructor and Description |
---|
ThreadSafeQueue()
Construct an empty queue.
|
Modifier and Type | Method and Description |
---|---|
E |
dequeue()
Get the next object from the queue.
|
void |
enqueue(E obj)
Put an object into the queue and notify one waiting thread.
|
boolean |
isTerminated()
Report if the queue processing has been terminated.
|
void |
terminate()
Terminate queue processing and release all waiting threads.
|
private static final ThreadSafeQueue.Poison POISON
private final java.util.concurrent.BlockingQueue<java.lang.Object> queue
private volatile boolean terminated
public void enqueue(E obj) throws java.lang.InterruptedException
obj
- An object to place into queue.java.lang.InterruptedException
- When the queue is terminated.public E dequeue() throws java.lang.InterruptedException
java.lang.InterruptedException
- When the queue is terminated or if interrupted while waiting.public void terminate()
public boolean isTerminated()
true
if the queue can no longer be used.