Package org.jivesoftware.smack.util
Class ArrayBlockingQueueWithShutdown<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown<E>
-
- Type Parameters:
E
- the type of elements held in this collection
- All Implemented Interfaces:
java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.concurrent.BlockingQueue<E>
,java.util.Queue<E>
public class ArrayBlockingQueueWithShutdown<E> extends java.util.AbstractQueue<E> implements java.util.concurrent.BlockingQueue<E>
Like ArrayBlockingQueue but with additionalshutdown()
andstart()
methods. Will throwInterruptedException
if Queue has been shutdown ontake()
andpoll(long, TimeUnit)
.Based on ArrayBlockingQueue of OpenJDK by Doug Lea (who released ArrayBlockingQueue as public domain).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ArrayBlockingQueueWithShutdown.TryPutResult
static class
ArrayBlockingQueueWithShutdown.TryTakeResult<E>
static class
ArrayBlockingQueueWithShutdown.TryTakeResultCode
-
Constructor Summary
Constructors Constructor Description ArrayBlockingQueueWithShutdown(int capacity)
ArrayBlockingQueueWithShutdown(int capacity, boolean fair)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
drainTo(java.util.Collection<? super E> c)
int
drainTo(java.util.Collection<? super E> c, int maxElements)
boolean
isShutdown()
Returns true if the queue is currently shut down.java.util.Iterator<E>
iterator()
boolean
offer(E e)
boolean
offer(E e, long timeout, java.util.concurrent.TimeUnit unit)
boolean
offerAndShutdown(E e)
E
peek()
E
poll()
E
poll(long timeout, java.util.concurrent.TimeUnit unit)
void
put(E e)
Inserts the specified element into this queue, waiting if necessary for space to become available.void
putAll(java.util.Collection<? extends E> elements)
boolean
putIfNotShutdown(E e)
Put if the queue has not been shutdown yet.int
remainingCapacity()
void
shutdown()
Shutdown the Queue.int
size()
boolean
start()
Start the queue.E
take()
ArrayBlockingQueueWithShutdown.TryPutResult
tryPut(E e)
ArrayBlockingQueueWithShutdown.TryTakeResult<E>
tryTake()
-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Constructor Detail
-
ArrayBlockingQueueWithShutdown
public ArrayBlockingQueueWithShutdown(int capacity)
-
ArrayBlockingQueueWithShutdown
public ArrayBlockingQueueWithShutdown(int capacity, boolean fair)
-
-
Method Detail
-
shutdown
public void shutdown()
Shutdown the Queue. Will method currently waiting for a not full/empty condition will unblock (and usually throw a InterruptedException).
-
start
public boolean start()
Start the queue. Newly created instances will be started automatically, thus this only needs to be called aftershutdown()
.- Returns:
true
if the queues was shutdown before,false
if not.
-
isShutdown
public boolean isShutdown()
Returns true if the queue is currently shut down.- Returns:
- true if the queue is shut down.
-
offerAndShutdown
public boolean offerAndShutdown(E e)
-
put
public void put(E e) throws java.lang.InterruptedException
Inserts the specified element into this queue, waiting if necessary for space to become available.This may throw an
InterruptedException
in two cases- If the queue was shut down.
- If the thread was was interrupted.
isShutdown()
.- Specified by:
put
in interfacejava.util.concurrent.BlockingQueue<E>
- Parameters:
e
- the element to add.- Throws:
java.lang.InterruptedException
- if interrupted while waiting or if the queue was shut down.
-
putIfNotShutdown
public boolean putIfNotShutdown(E e) throws java.lang.InterruptedException
Put if the queue has not been shutdown yet.- Parameters:
e
- the element to put into the queue.- Returns:
true
if the element has been put into the queue,false
if the queue was shutdown.- Throws:
java.lang.InterruptedException
- if the calling thread was interrupted.- Since:
- 4.4
-
putAll
public void putAll(java.util.Collection<? extends E> elements) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
tryPut
public ArrayBlockingQueueWithShutdown.TryPutResult tryPut(E e)
-
offer
public boolean offer(E e, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
- Specified by:
offer
in interfacejava.util.concurrent.BlockingQueue<E>
- Throws:
java.lang.InterruptedException
-
take
public E take() throws java.lang.InterruptedException
- Specified by:
take
in interfacejava.util.concurrent.BlockingQueue<E>
- Throws:
java.lang.InterruptedException
-
tryTake
public ArrayBlockingQueueWithShutdown.TryTakeResult<E> tryTake()
-
poll
public E poll(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
- Specified by:
poll
in interfacejava.util.concurrent.BlockingQueue<E>
- Throws:
java.lang.InterruptedException
-
remainingCapacity
public int remainingCapacity()
- Specified by:
remainingCapacity
in interfacejava.util.concurrent.BlockingQueue<E>
-
drainTo
public int drainTo(java.util.Collection<? super E> c)
- Specified by:
drainTo
in interfacejava.util.concurrent.BlockingQueue<E>
-
drainTo
public int drainTo(java.util.Collection<? super E> c, int maxElements)
- Specified by:
drainTo
in interfacejava.util.concurrent.BlockingQueue<E>
-
size
public int size()
-
-