@GwtCompatible(emulated=true) public final class Queues extends java.lang.Object
Queue and Deque instances. Also see this
class's counterparts Lists, Sets, and Maps.| Modifier and Type | Method and Description |
|---|---|
static <E> int |
drain(java.util.concurrent.BlockingQueue<E> q,
java.util.Collection<? super E> buffer,
int numElements,
long timeout,
java.util.concurrent.TimeUnit unit)
Drains the queue as
BlockingQueue.drainTo(Collection, int), but if the requested numElements elements are not available, it will wait for them up to the specified timeout. |
static <E> int |
drainUninterruptibly(java.util.concurrent.BlockingQueue<E> q,
java.util.Collection<? super E> buffer,
int numElements,
long timeout,
java.util.concurrent.TimeUnit unit)
Drains the queue as drain(BlockingQueue, Collection, int, long, TimeUnit), but
with a different behavior in case it is interrupted while waiting.
|
static <E> java.util.concurrent.ArrayBlockingQueue<E> |
newArrayBlockingQueue(int capacity)
Creates an empty
ArrayBlockingQueue with the given (fixed) capacity and nonfair access
policy. |
static <E> java.util.ArrayDeque<E> |
newArrayDeque()
Creates an empty
ArrayDeque. |
static <E> java.util.concurrent.ConcurrentLinkedQueue<E> |
newConcurrentLinkedQueue()
Creates an empty
ConcurrentLinkedQueue. |
static <E> java.util.concurrent.LinkedBlockingDeque<E> |
newLinkedBlockingDeque()
Creates an empty
LinkedBlockingDeque with a capacity of Integer.MAX_VALUE. |
static <E> java.util.concurrent.LinkedBlockingDeque<E> |
newLinkedBlockingDeque(int capacity)
Creates an empty
LinkedBlockingDeque with the given (fixed) capacity. |
static <E> java.util.concurrent.LinkedBlockingQueue<E> |
newLinkedBlockingQueue()
Creates an empty
LinkedBlockingQueue with a capacity of Integer.MAX_VALUE. |
static <E> java.util.concurrent.LinkedBlockingQueue<E> |
newLinkedBlockingQueue(int capacity)
Creates an empty
LinkedBlockingQueue with the given (fixed) capacity. |
static <E extends java.lang.Comparable> |
newPriorityBlockingQueue()
Creates an empty
PriorityBlockingQueue with the ordering given by its elements' natural
ordering. |
static <E extends java.lang.Comparable> |
newPriorityQueue()
Creates an empty
PriorityQueue with the ordering given by its elements' natural
ordering. |
static <E> java.util.concurrent.SynchronousQueue<E> |
newSynchronousQueue()
Creates an empty
SynchronousQueue with nonfair access policy. |
static <E> java.util.Deque<E> |
synchronizedDeque(java.util.Deque<E> deque)
Returns a synchronized (thread-safe) deque backed by the specified deque.
|
static <E> java.util.Queue<E> |
synchronizedQueue(java.util.Queue<E> queue)
Returns a synchronized (thread-safe) queue backed by the specified queue.
|
@GwtIncompatible public static <E> java.util.concurrent.ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity)
ArrayBlockingQueue with the given (fixed) capacity and nonfair access
policy.public static <E> java.util.ArrayDeque<E> newArrayDeque()
ArrayDeque.@GwtIncompatible public static <E> java.util.concurrent.ConcurrentLinkedQueue<E> newConcurrentLinkedQueue()
ConcurrentLinkedQueue.@GwtIncompatible public static <E> java.util.concurrent.LinkedBlockingDeque<E> newLinkedBlockingDeque()
LinkedBlockingDeque with a capacity of Integer.MAX_VALUE.@GwtIncompatible public static <E> java.util.concurrent.LinkedBlockingDeque<E> newLinkedBlockingDeque(int capacity)
LinkedBlockingDeque with the given (fixed) capacity.java.lang.IllegalArgumentException - if capacity is less than 1@GwtIncompatible public static <E> java.util.concurrent.LinkedBlockingQueue<E> newLinkedBlockingQueue()
LinkedBlockingQueue with a capacity of Integer.MAX_VALUE.@GwtIncompatible public static <E> java.util.concurrent.LinkedBlockingQueue<E> newLinkedBlockingQueue(int capacity)
LinkedBlockingQueue with the given (fixed) capacity.java.lang.IllegalArgumentException - if capacity is less than 1@GwtIncompatible public static <E extends java.lang.Comparable> java.util.concurrent.PriorityBlockingQueue<E> newPriorityBlockingQueue()
PriorityBlockingQueue with the ordering given by its elements' natural
ordering.E be Comparable since 15.0).public static <E extends java.lang.Comparable> java.util.PriorityQueue<E> newPriorityQueue()
PriorityQueue with the ordering given by its elements' natural
ordering.E be Comparable since 15.0).@GwtIncompatible public static <E> java.util.concurrent.SynchronousQueue<E> newSynchronousQueue()
SynchronousQueue with nonfair access policy.@Beta @CanIgnoreReturnValue @GwtIncompatible public static <E> int drain(java.util.concurrent.BlockingQueue<E> q, java.util.Collection<? super E> buffer, int numElements, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
BlockingQueue.drainTo(Collection, int), but if the requested numElements elements are not available, it will wait for them up to the specified timeout.q - the blocking queue to be drainedbuffer - where to add the transferred elementsnumElements - the number of elements to be waited fortimeout - how long to wait before giving up, in units of unitunit - a TimeUnit determining how to interpret the timeout parameterjava.lang.InterruptedException - if interrupted while waiting@Beta @CanIgnoreReturnValue @GwtIncompatible public static <E> int drainUninterruptibly(java.util.concurrent.BlockingQueue<E> q, java.util.Collection<? super E> buffer, int numElements, long timeout, java.util.concurrent.TimeUnit unit)
InterruptedException is thrown).q - the blocking queue to be drainedbuffer - where to add the transferred elementsnumElements - the number of elements to be waited fortimeout - how long to wait before giving up, in units of unitunit - a TimeUnit determining how to interpret the timeout parameterpublic static <E> java.util.Queue<E> synchronizedQueue(java.util.Queue<E> queue)
It is imperative that the user manually synchronize on the returned queue when accessing the queue's iterator:
Queue<E> queue = Queues.synchronizedQueue(MinMaxPriorityQueue.<E>create());
...
queue.add(element); // Needn't be in synchronized block
...
synchronized (queue) { // Must synchronize on queue!
Iterator<E> i = queue.iterator(); // Must be in synchronized block
while (i.hasNext()) {
foo(i.next());
}
}
Failure to follow this advice may result in non-deterministic behavior.
The returned queue will be serializable if the specified queue is serializable.
queue - the queue to be wrapped in a synchronized viewpublic static <E> java.util.Deque<E> synchronizedDeque(java.util.Deque<E> deque)
It is imperative that the user manually synchronize on the returned deque when accessing any of the deque's iterators:
Deque<E> deque = Queues.synchronizedDeque(Queues.<E>newArrayDeque());
...
deque.add(element); // Needn't be in synchronized block
...
synchronized (deque) { // Must synchronize on deque!
Iterator<E> i = deque.iterator(); // Must be in synchronized block
while (i.hasNext()) {
foo(i.next());
}
}
Failure to follow this advice may result in non-deterministic behavior.
The returned deque will be serializable if the specified deque is serializable.
deque - the deque to be wrapped in a synchronized viewCopyright © 2007–2023. All rights reserved.