E - 在这个集合中保存的元素的类型
public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
可选容量绑定构造函数参数用作防止过多队列扩展的一种方法。 容量,如果未指定,等于Integer.MAX_VALUE 。 链接节点在每次插入时动态创建,除非这将使队列高于容量。
该类及其迭代器实现了Collection和Iterator接口的所有可选方法。
| Constructor and Description |
|---|
LinkedBlockingQueue()
创建一个
LinkedBlockingQueue ,容量为
Integer.MAX_VALUE 。
|
LinkedBlockingQueue(Collection<? extends E> c)
创建一个
LinkedBlockingQueue ,容量为
Integer.MAX_VALUE ,最初包含给定集合的元素,以集合的迭代器的遍历顺序添加。
|
LinkedBlockingQueue(int capacity)
创建一个具有给定(固定)容量的
LinkedBlockingQueue 。
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
从这个队列中原子地删除所有的元素。
|
boolean |
contains(Object o)
如果此队列包含指定的元素,则返回
true 。
|
int |
drainTo(Collection<? super E> c)
从该队列中删除所有可用的元素,并将它们添加到给定的集合中。
|
int |
drainTo(Collection<? super E> c, int maxElements)
最多从该队列中删除给定数量的可用元素,并将它们添加到给定的集合中。
|
Iterator<E> |
iterator()
以适当的顺序返回该队列中的元素的迭代器。
|
boolean |
offer(E e)
如果可以在不超过队列的容量的情况下立即将其指定的元素插入到队列的尾部,如果队列已满,则返回
true和
false 。
|
boolean |
offer(E e, long timeout, TimeUnit unit)
在该队列的尾部插入指定的元素,必要时等待指定的等待时间才能使空间变得可用。
|
E |
peek()
检索但不删除此队列的头,如果此队列为空,则返回
null 。
|
E |
poll()
检索并删除此队列的头部,如果此队列为空,则返回
null 。
|
E |
poll(long timeout, TimeUnit unit)
检索并删除此队列的头,等待指定的等待时间(如有必要)使元素变为可用。
|
void |
put(E e)
在该队列的尾部插入指定的元素,如果需要,等待空格变为可用。
|
int |
remainingCapacity()
返回此队列可以理想地(在没有内存或资源限制)的情况下接受而不阻止的附加元素数。
|
boolean |
remove(Object o)
从该队列中删除指定元素的单个实例(如果存在)。
|
int |
size()
返回此队列中的元素数。
|
Spliterator<E> |
spliterator()
在此队列中的元素上返回 Spliterator 。
|
E |
take()
检索并删除此队列的头,如有必要,等待元素可用。
|
Object[] |
toArray()
以适当的顺序返回一个包含此队列中所有元素的数组。
|
<T> T[] |
toArray(T[] a)
以适当的顺序返回包含此队列中所有元素的数组;
返回的数组的运行时类型是指定数组的运行时类型。
|
String |
toString()
返回此集合的字符串表示形式。
|
add, addAll, element, removecontainsAll, isEmpty, removeAll, retainAllclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitaddaddAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, streampublic LinkedBlockingQueue()
LinkedBlockingQueue ,容量为
Integer.MAX_VALUE 。
public LinkedBlockingQueue(int capacity)
LinkedBlockingQueue 。
capacity - 这个队列的容量
IllegalArgumentException - 如果
capacity不大于零
public LinkedBlockingQueue(Collection<? extends E> c)
LinkedBlockingQueue ,容量为
Integer.MAX_VALUE ,最初包含给定集合的元素,以集合的迭代器的遍历顺序添加。
c - 最初包含的元素的集合
NullPointerException - 如果指定的集合或其任何元素为空
public int size()
size在界面
Collection<E>
size在类别
AbstractCollection<E>
public int remainingCapacity()
size 。
请注意,您不能总是通过检查remainingCapacity来确定插入元素的尝试是否成功,因为可能是另一个线程即将插入或删除元素的情况。
remainingCapacity在界面
BlockingQueue<E>
public void put(E e) throws InterruptedException
put在界面
BlockingQueue<E>
e - 要添加的元素
InterruptedException - 如果在等待时中断
NullPointerException - 如果指定的元素为空
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
offer在界面
BlockingQueue<E>
e - 要添加的元素
timeout - 放弃之前等待多久,以
unit为单位
unit - a
TimeUnit确定如何解释
timeout参数
true如果成功,或
false如果在空间可用之前经过指定的等待时间
InterruptedException - 如果等待中断
NullPointerException - 如果指定的元素为空
public boolean offer(E e)
false如果该队列已满,则返回true 。
当使用容量限制队列时,该方法通常比方法add更为方便 ,只能通过抛出异常来插入元素。
offer在界面
BlockingQueue<E>
offer在接口
Queue<E>
e - 要添加的元素
true如果元素被添加到这个队列,否则
false
NullPointerException - 如果指定的元素为空
public E take() throws InterruptedException
BlockingQueue复制
take在界面
BlockingQueue<E>
InterruptedException - 如果在等待时中断
public E poll(long timeout, TimeUnit unit) throws InterruptedException
BlockingQueue复制
poll在界面
BlockingQueue<E>
timeout - 放弃之前等待多久,以
unit为单位
unit - a
TimeUnit确定如何解释
timeout参数
null如果在元素可用之前经过指定的等待时间
InterruptedException - 如果在等待时中断
public boolean remove(Object o)
e使o.equals(e) ,如果这个队列包含一个或多个这样的元素。
如果此队列包含指定的元素(或等效地,如果此队列作为调用的结果而更改),则返回true 。
remove在界面
Collection<E>
remove在界面
BlockingQueue<E>
remove在类别
AbstractCollection<E>
o - 要从此队列中删除的元素(如果存在)
true如果此队列由于调用而更改
public boolean contains(Object o)
true 。
更正式地,返回true当且仅当这个队列包含至少一个元素e ,使o.equals(e) 。
contains在界面
Collection<E>
contains在界面
BlockingQueue<E>
contains在类别
AbstractCollection<E>
o - 要检查此队列中的遏制的对象
true如果此队列包含指定的元素
public Object[] toArray()
返回的数组将是“安全的”,因为该队列不保留对它的引用。 (换句话说,这个方法必须分配一个新的数组)。 因此,调用者可以自由地修改返回的数组。
此方法充当基于阵列和基于集合的API之间的桥梁。
toArray在界面
Collection<E>
toArray在类别
AbstractCollection<E>
public <T> T[] toArray(T[] a)
如果这个队列符合指定的数组空间(即数组中有比此队列更多的元素),则队列结束后的数组中的元素设置为null 。
像toArray()方法一样,此方法充当基于阵列和基于集合的API之间的桥梁。 此外,该方法允许精确地控制输出阵列的运行时类型,并且在某些情况下可以用于节省分配成本。
假设x是一个已知只包含字符串的队列。 以下代码可用于将队列转储到新分配的数组中: String :
String[] y = x.toArray(new String[0]);
请注意, toArray(new Object[0])的功能与toArray() 。
toArray在界面
Collection<E>
toArray在类别
AbstractCollection<E>
T - 包含集合的数组的运行时类型
a - 要存储队列的元素的阵列,如果它足够大;
否则,为此目的分配相同运行时类型的新数组
ArrayStoreException - 如果指定数组的运行时类型不是此队列中每个元素的运行时类型的超类型
NullPointerException - 如果指定的数组为空
public String toString()
AbstractCollection复制
String.valueOf(Object) 。
toString在类别
AbstractCollection<E>
public void clear()
clear在界面
Collection<E>
clear在类别
AbstractQueue<E>
public int drainTo(Collection<? super E> c)
BlockingQueue复制
c添加元素时遇到的c可能会导致在抛出关联的异常时,元素既不在两个集合中也不在两个集合中。
尝试将队列排入自身导致IllegalArgumentException 。
此外,如果在操作进行中修改了指定的集合,则此操作的行为是未定义的。
drainTo在界面
BlockingQueue<E>
c - 将元素传输到的集合
UnsupportedOperationException - 如果指定集合不支持元素的添加
ClassCastException - 如果此队列的元素的类阻止将其添加到指定的集合
NullPointerException - 如果指定的集合为空
IllegalArgumentException - 如果指定的集合是此队列,或此队列的某个元素的某些属性会阻止将其添加到指定的集合
public int drainTo(Collection<? super E> c, int maxElements)
BlockingQueue复制
c添加元素时遇到的c可能会导致元素在抛出关联的异常时既不在两个集合中,也可能不是两个集合。
尝试将队列排入自身会导致IllegalArgumentException 。
此外,如果在操作进行中修改了指定的集合,则此操作的行为是未定义的。
drainTo在界面
BlockingQueue<E>
c - 传输元素的集合
maxElements - 要传输的元素的最大数量
UnsupportedOperationException - 如果指定集合不支持添加元素
ClassCastException - 如果此队列的元素的类阻止将其添加到指定的集合
NullPointerException - 如果指定的集合为空
IllegalArgumentException - 如果指定的集合是此队列,或此队列的某个元素的某些属性阻止将其添加到指定的集合
public Iterator<E> iterator()
iterator在界面
Iterable<E>
iterator在界面
Collection<E>
iterator在类别
AbstractCollection<E>
public Spliterator<E> spliterator()
Spliterator 。
返回的拼接器是weakly consistent 。
该Spliterator报告Spliterator.CONCURRENT , Spliterator.ORDERED和Spliterator.NONNULL 。
spliterator在界面
Iterable<E>
spliterator在界面
Collection<E>
Spliterator实现
trySplit以允许有限的并行性。
Spliterator在这个队列中的元素
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.