Package com.google.common.collect
Class Queues
- java.lang.Object
-
- com.google.common.collect.Queues
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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 asBlockingQueue.drainTo(Collection, int)
, but if the requestednumElements
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 emptyArrayBlockingQueue
with the given (fixed) capacity and nonfair access policy.static <E> java.util.ArrayDeque<E>
newArrayDeque()
Creates an emptyArrayDeque
.static <E> java.util.ArrayDeque<E>
newArrayDeque(java.lang.Iterable<? extends E> elements)
Creates anArrayDeque
containing the elements of the specified iterable, in the order they are returned by the iterable's iterator.static <E> java.util.concurrent.ConcurrentLinkedQueue<E>
newConcurrentLinkedQueue()
Creates an emptyConcurrentLinkedQueue
.static <E> java.util.concurrent.ConcurrentLinkedQueue<E>
newConcurrentLinkedQueue(java.lang.Iterable<? extends E> elements)
Creates aConcurrentLinkedQueue
containing the elements of the specified iterable, in the order they are returned by the iterable's iterator.static <E> java.util.concurrent.LinkedBlockingDeque<E>
newLinkedBlockingDeque()
Creates an emptyLinkedBlockingDeque
with a capacity ofInteger.MAX_VALUE
.static <E> java.util.concurrent.LinkedBlockingDeque<E>
newLinkedBlockingDeque(int capacity)
Creates an emptyLinkedBlockingDeque
with the given (fixed) capacity.static <E> java.util.concurrent.LinkedBlockingDeque<E>
newLinkedBlockingDeque(java.lang.Iterable<? extends E> elements)
Creates aLinkedBlockingDeque
with a capacity ofInteger.MAX_VALUE
, containing the elements of the specified iterable, in the order they are returned by the iterable's iterator.static <E> java.util.concurrent.LinkedBlockingQueue<E>
newLinkedBlockingQueue()
Creates an emptyLinkedBlockingQueue
with a capacity ofInteger.MAX_VALUE
.static <E> java.util.concurrent.LinkedBlockingQueue<E>
newLinkedBlockingQueue(int capacity)
Creates an emptyLinkedBlockingQueue
with the given (fixed) capacity.static <E> java.util.concurrent.LinkedBlockingQueue<E>
newLinkedBlockingQueue(java.lang.Iterable<? extends E> elements)
Creates aLinkedBlockingQueue
with a capacity ofInteger.MAX_VALUE
, containing the elements of the specified iterable, in the order they are returned by the iterable's iterator.static <E extends java.lang.Comparable>
java.util.concurrent.PriorityBlockingQueue<E>newPriorityBlockingQueue()
Creates an emptyPriorityBlockingQueue
with the ordering given by its elements' natural ordering.static <E extends java.lang.Comparable>
java.util.concurrent.PriorityBlockingQueue<E>newPriorityBlockingQueue(java.lang.Iterable<? extends E> elements)
Creates aPriorityBlockingQueue
containing the given elements.static <E extends java.lang.Comparable>
java.util.PriorityQueue<E>newPriorityQueue()
Creates an emptyPriorityQueue
with the ordering given by its elements' natural ordering.static <E extends java.lang.Comparable>
java.util.PriorityQueue<E>newPriorityQueue(java.lang.Iterable<? extends E> elements)
Creates aPriorityQueue
containing the given elements.static <E> java.util.concurrent.SynchronousQueue<E>
newSynchronousQueue()
Creates an emptySynchronousQueue
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.
-
-
-
Method Detail
-
newArrayBlockingQueue
public static <E> java.util.concurrent.ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity)
Creates an emptyArrayBlockingQueue
with the given (fixed) capacity and nonfair access policy.
-
newArrayDeque
public static <E> java.util.ArrayDeque<E> newArrayDeque()
Creates an emptyArrayDeque
.- Since:
- 12.0
-
newArrayDeque
public static <E> java.util.ArrayDeque<E> newArrayDeque(java.lang.Iterable<? extends E> elements)
Creates anArrayDeque
containing the elements of the specified iterable, in the order they are returned by the iterable's iterator.- Since:
- 12.0
-
newConcurrentLinkedQueue
public static <E> java.util.concurrent.ConcurrentLinkedQueue<E> newConcurrentLinkedQueue()
Creates an emptyConcurrentLinkedQueue
.
-
newConcurrentLinkedQueue
public static <E> java.util.concurrent.ConcurrentLinkedQueue<E> newConcurrentLinkedQueue(java.lang.Iterable<? extends E> elements)
Creates aConcurrentLinkedQueue
containing the elements of the specified iterable, in the order they are returned by the iterable's iterator.
-
newLinkedBlockingDeque
public static <E> java.util.concurrent.LinkedBlockingDeque<E> newLinkedBlockingDeque()
Creates an emptyLinkedBlockingDeque
with a capacity ofInteger.MAX_VALUE
.- Since:
- 12.0
-
newLinkedBlockingDeque
public static <E> java.util.concurrent.LinkedBlockingDeque<E> newLinkedBlockingDeque(int capacity)
Creates an emptyLinkedBlockingDeque
with the given (fixed) capacity.- Throws:
java.lang.IllegalArgumentException
- ifcapacity
is less than 1- Since:
- 12.0
-
newLinkedBlockingDeque
public static <E> java.util.concurrent.LinkedBlockingDeque<E> newLinkedBlockingDeque(java.lang.Iterable<? extends E> elements)
Creates aLinkedBlockingDeque
with a capacity ofInteger.MAX_VALUE
, containing the elements of the specified iterable, in the order they are returned by the iterable's iterator.- Since:
- 12.0
-
newLinkedBlockingQueue
public static <E> java.util.concurrent.LinkedBlockingQueue<E> newLinkedBlockingQueue()
Creates an emptyLinkedBlockingQueue
with a capacity ofInteger.MAX_VALUE
.
-
newLinkedBlockingQueue
public static <E> java.util.concurrent.LinkedBlockingQueue<E> newLinkedBlockingQueue(int capacity)
Creates an emptyLinkedBlockingQueue
with the given (fixed) capacity.- Throws:
java.lang.IllegalArgumentException
- ifcapacity
is less than 1
-
newLinkedBlockingQueue
public static <E> java.util.concurrent.LinkedBlockingQueue<E> newLinkedBlockingQueue(java.lang.Iterable<? extends E> elements)
Creates aLinkedBlockingQueue
with a capacity ofInteger.MAX_VALUE
, containing the elements of the specified iterable, in the order they are returned by the iterable's iterator.- Parameters:
elements
- the elements that the queue should contain, in order- Returns:
- a new
LinkedBlockingQueue
containing those elements
-
newPriorityBlockingQueue
public static <E extends java.lang.Comparable> java.util.concurrent.PriorityBlockingQueue<E> newPriorityBlockingQueue()
Creates an emptyPriorityBlockingQueue
with the ordering given by its elements' natural ordering.- Since:
- 11.0 (requires that
E
beComparable
since 15.0).
-
newPriorityBlockingQueue
public static <E extends java.lang.Comparable> java.util.concurrent.PriorityBlockingQueue<E> newPriorityBlockingQueue(java.lang.Iterable<? extends E> elements)
Creates aPriorityBlockingQueue
containing the given elements. Note: If the specified iterable is aSortedSet
or aPriorityQueue
, this priority queue will be ordered according to the same ordering.- Since:
- 11.0 (requires that
E
beComparable
since 15.0).
-
newPriorityQueue
public static <E extends java.lang.Comparable> java.util.PriorityQueue<E> newPriorityQueue()
Creates an emptyPriorityQueue
with the ordering given by its elements' natural ordering.- Since:
- 11.0 (requires that
E
beComparable
since 15.0).
-
newPriorityQueue
public static <E extends java.lang.Comparable> java.util.PriorityQueue<E> newPriorityQueue(java.lang.Iterable<? extends E> elements)
Creates aPriorityQueue
containing the given elements. Note: If the specified iterable is aSortedSet
or aPriorityQueue
, this priority queue will be ordered according to the same ordering.- Since:
- 11.0 (requires that
E
beComparable
since 15.0).
-
newSynchronousQueue
public static <E> java.util.concurrent.SynchronousQueue<E> newSynchronousQueue()
Creates an emptySynchronousQueue
with nonfair access policy.
-
drain
@Beta 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
Drains the queue asBlockingQueue.drainTo(Collection, int)
, but if the requestednumElements
elements are not available, it will wait for them up to the specified timeout.- Parameters:
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 ofunit
unit
- aTimeUnit
determining how to interpret the timeout parameter- Returns:
- the number of elements transferred
- Throws:
java.lang.InterruptedException
- if interrupted while waiting
-
drainUninterruptibly
@Beta 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)
Drains the queue as drain(BlockingQueue, Collection, int, long, TimeUnit), but with a different behavior in case it is interrupted while waiting. In that case, the operation will continue as usual, and in the end the thread's interruption status will be set (noInterruptedException
is thrown).- Parameters:
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 ofunit
unit
- aTimeUnit
determining how to interpret the timeout parameter- Returns:
- the number of elements transferred
-
synchronizedQueue
@Beta public static <E> java.util.Queue<E> synchronizedQueue(java.util.Queue<E> queue)
Returns a synchronized (thread-safe) queue backed by the specified queue. In order to guarantee serial access, it is critical that all access to the backing queue is accomplished through the returned 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.
- Parameters:
queue
- the queue to be wrapped in a synchronized view- Returns:
- a synchronized view of the specified queue
- Since:
- 14.0
-
synchronizedDeque
@Beta public static <E> java.util.Deque<E> synchronizedDeque(java.util.Deque<E> deque)
Returns a synchronized (thread-safe) deque backed by the specified deque. In order to guarantee serial access, it is critical that all access to the backing deque is accomplished through the returned 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.
- Parameters:
deque
- the deque to be wrapped in a synchronized view- Returns:
- a synchronized view of the specified deque
- Since:
- 15.0
-
-