E
-元素举行此集合中的类型
public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
可选的容量绑定构造函数的参数作为一种方法,以防止过度的队列扩展。的能力,如果没有指定,等于Integer.MAX_VALUE
。链接的节点是动态创建的,除非这会带来容量的队列,否则每个插入都会被动态创建的链接。
这个类和它的迭代器实现所有的可选方法的Collection
和Iterator
接口。
这个班的一员 Java Collections Framework。
Constructor and Description |
---|
LinkedBlockingQueue()
创建一个容量
Integer.MAX_VALUE
LinkedBlockingQueue 。
|
LinkedBlockingQueue(Collection<? extends E> c)
创建一个容量
Integer.MAX_VALUE
LinkedBlockingQueue ,最初包含元素的集合,加入集合的迭代器遍历顺序。
|
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, remove
containsAll, isEmpty, removeAll, retainAll
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
add
addAll, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, stream
public LinkedBlockingQueue()
Integer.MAX_VALUE
LinkedBlockingQueue
。
public LinkedBlockingQueue(int capacity)
LinkedBlockingQueue
(固定的)能力。
capacity
-这个队列容量
IllegalArgumentException
-如果
capacity
不大于零
public LinkedBlockingQueue(Collection<? extends E> c)
Integer.MAX_VALUE
LinkedBlockingQueue
,最初包含元素的集合,加入集合的迭代器遍历顺序。
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
-如果指定元素为null
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
offer
接口
BlockingQueue<E>
e
-元素添加
timeout
-多久才放弃等待,在单位
unit
unit
-
TimeUnit
确定如何解释
timeout
参数
true
如果成功,或
false
如果指定的等待时间过去之前可用空间
InterruptedException
如果中断等待
NullPointerException
-如果指定元素为null
public boolean offer(E e)
offer
接口
BlockingQueue<E>
offer
接口
Queue<E>
e
-元素添加
true
如果元素被添加到这个队列,否则
false
NullPointerException
-如果指定元素为null
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
-
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()
返回的数组将是“安全”的,在这个队列中没有引用它的引用。(换句话说,这种方法必须分配一个新的数组)。因此,调用方可以自由修改返回的数组。
此方法作为基于数组和基于集合的原料药之间的桥梁。
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
添加元素可能会导致元素在没有遭遇过失败,或集合时相关的异常被抛出。试图排队列本身造成
IllegalArgumentException
。此外,当操作正在进行中时,此操作的行为是不确定的,如果指定的集合被修改了。
drainTo
接口
BlockingQueue<E>
c
-收集传递元素
UnsupportedOperationException
如果添加元素不按指定集合的支持
ClassCastException
-如果该队列的元素类型阻止其加入指定集合
NullPointerException
-如果指定集合为空
IllegalArgumentException
-如果指定集合的队列,该队列或一个元素的一些特性可以防止它被添加到指定的集合
public int drainTo(Collection<? super E> c, int maxElements)
BlockingQueue
c
添加元素可能会导致元素在没有遭遇过失败,或集合时相关的异常被抛出。试图排队列本身造成
IllegalArgumentException
。此外,如果指定的收集是在操作进行修改此操作的行为是未定义的。
drainTo
接口
BlockingQueue<E>
c
-收集传递元素
maxElements
-元素的最大数量的转移
UnsupportedOperationException
如果添加元素不按指定集合的支持
ClassCastException
-如果该队列的元素类型阻止其加入指定集合
NullPointerException
-如果指定集合为空
IllegalArgumentException
-如果指定集合的队列,该队列或一个元素的一些特性可以防止它被添加到指定的集合
public Iterator<E> iterator()
返回的迭代器是weakly consistent。
iterator
接口
Iterable<E>
iterator
接口
Collection<E>
iterator
方法重写,继承类
AbstractCollection<E>
public Spliterator<E> spliterator()
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.