Most visited

Recently visited

Added in API level 9

BlockingDeque

public interface BlockingDeque
implements BlockingQueue<E>, Deque<E>

java.util.concurrent.BlockingDeque<E>
Known Indirect Subclasses


一个 Deque还支持在检索元素时等待deque变为非空的阻塞操作,并在存储元素时等待空间在deque中可用。

BlockingDeque方法有四种形式,用不同的方式处理操作,但不能立即满足,但可能在将来某个时候满足:一个抛出异常,第二个返回特殊值( nullfalse ,取决于操作),第三个块无限期地阻塞当前线程,直到操作成功,第四个块在放弃之前只有给定的最大时间限制。 下表总结了这些方法:

Summary of BlockingDeque methods
First Element (Head)
Throws exception Special value Blocks Times out
Insert addFirst(e) offerFirst(e) putFirst(e) offerFirst(e, time, unit)
Remove removeFirst() pollFirst() takeFirst() pollFirst(time, unit)
Examine getFirst() peekFirst() not applicable not applicable
Last Element (Tail)
Throws exception Special value Blocks Times out
Insert addLast(e) offerLast(e) putLast(e) offerLast(e, time, unit)
Remove removeLast() pollLast() takeLast() pollLast(time, unit)
Examine getLast() peekLast() not applicable not applicable

像任何 BlockingQueue一样, BlockingDeque是线程安全的,不允许空元素,并且可能(或可能不)容量受限。

一个BlockingDeque实现可以直接用作FIFO BlockingQueue BlockingQueue接口继承的方法与BlockingDeque方法的精确等价,如下表所示:

Comparison of BlockingQueue and BlockingDeque methods
BlockingQueue Method Equivalent BlockingDeque Method
Insert
add(e) addLast(e)
offer(e) offerLast(e)
put(e) putLast(e)
offer(e, time, unit) offerLast(e, time, unit)
Remove
remove() removeFirst()
poll() pollFirst()
take() takeFirst()
poll(time, unit) pollFirst(time, unit)
Examine
element() getFirst()
peek() peekFirst()

存储器一致性效果:当与其他并发集合,事先将物体放置成在一个线程动作 BlockingDeque happen-before到该元素的从访问或移除后续动作 BlockingDeque在另一个线程。

该界面是 Java Collections Framework的成员。

Summary

Public methods

abstract boolean add(E e)

将指定的元素插入此双端队列表示的队列中(换句话说,在此双端队列的尾部),如果它是立即可行且不会违反容量限制,返回 true成功时和抛出 IllegalStateException ,如果当前没有空间可用的。

abstract void addFirst(E e)

如果可以立即执行而不违反容量限制,则将指定的元素插入此双端队列的前端,如果当前没有可用空间,则抛出 IllegalStateException

abstract void addLast(E e)

如果可以立即执行而不违反容量限制,则将指定的元素插入此双端队列的末尾,如果当前没有可用空间,则抛出 IllegalStateException

abstract boolean contains(Object o)

如果此双端队列包含指定的元素,则返回 true

abstract E element()

检索但不删除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素)。

abstract Iterator<E> iterator()

以适当的顺序返回此双端队列中元素的迭代器。

abstract boolean offer(E e, long timeout, TimeUnit unit)

将指定的元素插入到由此双端队列表示的队列中(换句话说,在此双端队列的尾部),如果有必要,等待达到指定的等待时间以使空间变为可用。

abstract boolean offer(E e)

将指定的元素插入此双端队列表示的队列中(换句话说,在此双端队列的尾部),如果它是立即可行且不会违反容量限制,返回 true在成功和 false ,如果当前没有空间可用。

abstract boolean offerFirst(E e)

插入此双端队列的前面,如果它是立即可行且不会违反容量限制,返回指定的元素 true在成功 false如果当前没有空间可用。

abstract boolean offerFirst(E e, long timeout, TimeUnit unit)

将指定的元素插入此双端队列的前端,如果有必要,等待指定的等待时间以使空间变为可用。

abstract boolean offerLast(E e)

插入此双端队列的末尾,如果它是立即可行且不会违反容量限制,返回指定的元素 true在成功 false如果当前没有空间可用。

abstract boolean offerLast(E e, long timeout, TimeUnit unit)

在指定的元素末尾插入指定的元素,如果需要,等待指定的等待时间以使空间变为可用。

abstract E peek()

检索但不移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素),或者如果此双端队列为空,则返回 null

abstract E poll(long timeout, TimeUnit unit)

检索并移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素),如果元素可用,则等待达到指定的等待时间(如果需要)。

abstract E poll()

检索并移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素);如果此双端队列为空,则返回 null

abstract E pollFirst(long timeout, TimeUnit unit)

检索并删除此双端队列的第一个元素,如果需要,等待达到指定的等待时间,以使元素可用。

abstract E pollLast(long timeout, TimeUnit unit)

检索并删除此双端队列的最后一个元素,如果元素变为可用,则等待达到指定的等待时间。

abstract void push(E e)

如果可以立即执行而不违反容量限制, IllegalStateException元素推入由此双端队列表示的堆栈中(如果没有空间限制,则抛出 IllegalStateException

abstract void put(E e)

将指定的元素插入到由此双端队列表示的队列中(换句话说,在此双端队列的尾部),等待必要的空间变为可用状态。

abstract void putFirst(E e)

将指定的元素插入此双端队列的前端,如果需要,等待空间变为可用。

abstract void putLast(E e)

在此双端队列的末尾插入指定的元素,如果需要,等待空间变为可用。

abstract E remove()

检索并移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素)。

abstract boolean remove(Object o)

从此双端队列中移除指定元素的第一个匹配项。

abstract boolean removeFirstOccurrence(Object o)

从此双端队列中移除指定元素的第一个匹配项。

abstract boolean removeLastOccurrence(Object o)

从此双端队列中移除指定元素的最后一次出现。

abstract int size()

返回此双端队列中的元素数量。

abstract E take()

检索并移除由此双端队列表示的队列的头部(换句话说,此双端队列的第一个元素),如果有必要,则等待一个元素变为可用。

abstract E takeFirst()

检索并删除此双端队列的第一个元素,如果需要等待,直到元素变为可用。

abstract E takeLast()

检索并移除此双端队列的最后一个元素,如果需要,等待元素变为可用。

Inherited methods

From interface java.util.concurrent.BlockingQueue
From interface java.util.Deque
From interface java.util.Queue
From interface java.util.Collection
From interface java.lang.Iterable

Public methods

add

Added in API level 9
boolean add (E e)

如果可以在不违反容量限制的情况下立即执行此操作,则将指定元素插入到由此双端队列表示的队列中(换句话说,即在此双端队列的尾部),成功时返回true ,如果当前没有空间,则抛出IllegalStateException 当使用容量限制的双端队列时,通常最好使用offer

该方法相当于 addLast

Parameters
e E: the element to add
Returns
boolean true (as specified by add(E))
Throws
IllegalStateException
ClassCastException if the class of the specified element prevents it from being added to this deque
NullPointerException if the specified element is null
IllegalArgumentException if some property of the specified element prevents it from being added to this deque

addFirst

Added in API level 9
void addFirst (E e)

如果可以立即执行而不违反容量限制,则将指定元素插入此双端队列的前端,如果当前没有可用空间,则抛出IllegalStateException 当使用容量限制的双端队列时,通常最好使用offerFirst

Parameters
e E: the element to add
Throws
IllegalStateException
ClassCastException
NullPointerException if the specified element is null
IllegalArgumentException

addLast

Added in API level 9
void addLast (E e)

如果可以立即执行而不违反容量限制,则将指定的元素插入此双端队列的末尾,如果当前没有可用空间,则抛出IllegalStateException 当使用容量限制的双端队列时,通常最好使用offerLast

Parameters
e E: the element to add
Throws
IllegalStateException
ClassCastException
NullPointerException if the specified element is null
IllegalArgumentException

contains

Added in API level 9
boolean contains (Object o)

如果此双端队列包含指定的元素,则返回true 更正式地,返回true当且仅当该双端包含至少一个元素e ,使得o.equals(e)

Parameters
o Object: object to be checked for containment in this deque
Returns
boolean true if this deque contains the specified element
Throws
ClassCastException if the class of the specified element is incompatible with this deque (optional)
NullPointerException if the specified element is null (optional)

element

Added in API level 9
E element ()

检索但不删除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素)。 此方法与peek仅在于,如果此双端队列为空,则会引发异常。

这种方法相当于 getFirst

Returns
E the head of this deque
Throws
NoSuchElementException if this deque is empty

iterator

Added in API level 9
Iterator<E> iterator ()

以适当的顺序返回此双端队列中元素的迭代器。 元素将从第一个(头部)到最后一个(尾部)按顺序返回。

Returns
Iterator<E> an iterator over the elements in this deque in proper sequence

offer

Added in API level 9
boolean offer (E e, 
                long timeout, 
                TimeUnit unit)

将指定的元素插入到由此双端队列表示的队列中(换句话说,在此双端队列的尾部),如果有必要,等待达到指定的等待时间以使空间变为可用。

该方法相当于 offerLast

Parameters
e E: the element to add
timeout long: how long to wait before giving up, in units of unit
unit TimeUnit: a TimeUnit determining how to interpret the timeout parameter
Returns
boolean true if the element was added to this deque, else false
Throws
InterruptedException
ClassCastException if the class of the specified element prevents it from being added to this deque
NullPointerException if the specified element is null
IllegalArgumentException if some property of the specified element prevents it from being added to this deque

offer

Added in API level 9
boolean offer (E e)

将指定的元素插入此双端队列表示的队列中(换句话说,在此双端队列的尾部),如果它是立即可行且不会违反容量限制,返回true在成功和false ,如果当前没有空间可用。 使用容量限制的双端队列时,此方法通常优于add(E)方法,该方法可能无法仅通过抛出异常来插入元素。

此方法相当于 offerLast

Parameters
e E: the element to add
Returns
boolean true if the element was added to this deque, else false
Throws
ClassCastException if the class of the specified element prevents it from being added to this deque
NullPointerException if the specified element is null
IllegalArgumentException if some property of the specified element prevents it from being added to this deque

offerFirst

Added in API level 9
boolean offerFirst (E e)

插入此双端队列的前面,如果它是立即可行且不会违反容量限制,返回指定的元素true在成功false如果当前没有空间可用。 当使用容量限制的双端队列时,此方法通常优于addFirst方法,该方法可能无法仅通过抛出异常来插入元素。

Parameters
e E: the element to add
Returns
boolean true if the element was added to this deque, else false
Throws
ClassCastException
NullPointerException if the specified element is null
IllegalArgumentException

offerFirst

Added in API level 9
boolean offerFirst (E e, 
                long timeout, 
                TimeUnit unit)

将指定的元素插入此双端队列的前端,如果有必要,等待指定的等待时间以使空间变为可用。

Parameters
e E: the element to add
timeout long: how long to wait before giving up, in units of unit
unit TimeUnit: a TimeUnit determining how to interpret the timeout parameter
Returns
boolean true if successful, or false if the specified waiting time elapses before space is available
Throws
InterruptedException if interrupted while waiting
ClassCastException if the class of the specified element prevents it from being added to this deque
NullPointerException if the specified element is null
IllegalArgumentException if some property of the specified element prevents it from being added to this deque

offerLast

Added in API level 9
boolean offerLast (E e)

插入此双端队列的末尾,如果它是立即可行且不会违反容量限制,返回指定的元素true在成功false如果当前没有空间可用。 当使用容量限制的双端队列时,此方法通常比addLast方法更可取,该方法只能通过抛出异常才能插入元素。

Parameters
e E: the element to add
Returns
boolean true if the element was added to this deque, else false
Throws
ClassCastException
NullPointerException if the specified element is null
IllegalArgumentException

offerLast

Added in API level 9
boolean offerLast (E e, 
                long timeout, 
                TimeUnit unit)

在指定的元素末尾插入指定的元素,如果需要,等待指定的等待时间以使空间变为可用。

Parameters
e E: the element to add
timeout long: how long to wait before giving up, in units of unit
unit TimeUnit: a TimeUnit determining how to interpret the timeout parameter
Returns
boolean true if successful, or false if the specified waiting time elapses before space is available
Throws
InterruptedException if interrupted while waiting
ClassCastException if the class of the specified element prevents it from being added to this deque
NullPointerException if the specified element is null
IllegalArgumentException if some property of the specified element prevents it from being added to this deque

peek

Added in API level 9
E peek ()

检索但不移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素),或者如果此双端队列为空,则返回 null

该方法相当于 peekFirst

Returns
E the head of this deque, or null if this deque is empty

poll

Added in API level 9
E poll (long timeout, 
                TimeUnit unit)

检索并移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素),如果元素可用,则等待达到指定的等待时间(如果需要)。

该方法相当于 pollFirst

Parameters
timeout long: how long to wait before giving up, in units of unit
unit TimeUnit: a TimeUnit determining how to interpret the timeout parameter
Returns
E the head of this deque, or null if the specified waiting time elapses before an element is available
Throws
InterruptedException if interrupted while waiting

poll

Added in API level 9
E poll ()

检索并移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素);如果此双端队列为空,则返回 null

该方法相当于 pollFirst()

Returns
E the head of this deque, or null if this deque is empty

pollFirst

Added in API level 9
E pollFirst (long timeout, 
                TimeUnit unit)

检索并删除此双端队列的第一个元素,如果需要,等待达到指定的等待时间,以使元素可用。

Parameters
timeout long: how long to wait before giving up, in units of unit
unit TimeUnit: a TimeUnit determining how to interpret the timeout parameter
Returns
E the head of this deque, or null if the specified waiting time elapses before an element is available
Throws
InterruptedException if interrupted while waiting

pollLast

Added in API level 9
E pollLast (long timeout, 
                TimeUnit unit)

检索并删除此双端队列的最后一个元素,如果元素变为可用,则等待达到指定的等待时间。

Parameters
timeout long: how long to wait before giving up, in units of unit
unit TimeUnit: a TimeUnit determining how to interpret the timeout parameter
Returns
E the tail of this deque, or null if the specified waiting time elapses before an element is available
Throws
InterruptedException if interrupted while waiting

push

Added in API level 9
void push (E e)

如果可以立即执行而不违反容量限制, IllegalStateException元素推入由此双端队列表示的堆栈中(如果没有空间限制,则立即抛出 IllegalStateException

这种方法相当于 addFirst

Parameters
e E: the element to push
Throws
IllegalStateException
ClassCastException
NullPointerException if the specified element is null
IllegalArgumentException

put

Added in API level 9
void put (E e)

将指定的元素插入到由此双端队列表示的队列中(换句话说,在此双端队列的尾部),等待必要的空间变为可用状态。

这种方法相当于 putLast

Parameters
e E: the element to add
Throws
InterruptedException
ClassCastException if the class of the specified element prevents it from being added to this deque
NullPointerException if the specified element is null
IllegalArgumentException if some property of the specified element prevents it from being added to this deque

putFirst

Added in API level 9
void putFirst (E e)

将指定的元素插入此双端队列的前端,如果需要,等待空间变为可用。

Parameters
e E: the element to add
Throws
InterruptedException if interrupted while waiting
ClassCastException if the class of the specified element prevents it from being added to this deque
NullPointerException if the specified element is null
IllegalArgumentException if some property of the specified element prevents it from being added to this deque

putLast

Added in API level 9
void putLast (E e)

在此双端队列的末尾插入指定的元素,如果需要,等待空间变为可用。

Parameters
e E: the element to add
Throws
InterruptedException if interrupted while waiting
ClassCastException if the class of the specified element prevents it from being added to this deque
NullPointerException if the specified element is null
IllegalArgumentException if some property of the specified element prevents it from being added to this deque

remove

Added in API level 9
E remove ()

检索并移除由此双端队列表示的队列头(换句话说,此双端队列的第一个元素)。 此方法与poll仅在于,如果此双端队列为空,则会引发异常。

该方法相当于 removeFirst

Returns
E the head of the queue represented by this deque
Throws
NoSuchElementException if this deque is empty

remove

Added in API level 9
boolean remove (Object o)

从此双端队列中移除指定元素的第一个匹配项。 如果该deque不包含该元素,则该值不变。 更正式地,删除第一个元素e ,使得o.equals(e) (如果存在这样的元素)。 如果此双端队列包含指定的元素(或者等价地,如果此双端队列因呼叫而改变),则返回true

该方法相当于 removeFirstOccurrence

Parameters
o Object: element to be removed from this deque, if present
Returns
boolean true if this deque changed as a result of the call
Throws
ClassCastException if the class of the specified element is incompatible with this deque (optional)
NullPointerException if the specified element is null (optional)

removeFirstOccurrence

Added in API level 9
boolean removeFirstOccurrence (Object o)

从此双端队列中移除指定元素的第一个匹配项。 如果该deque不包含该元素,则该值不变。 更正式地说,删除第一个元素e ,使得o.equals(e) (如果存在这样的元素)。 如果此双端队列包含指定的元素(或者等价地,如果此双端队列由于调用而更改),则返回true

Parameters
o Object: element to be removed from this deque, if present
Returns
boolean true if an element was removed as a result of this call
Throws
ClassCastException if the class of the specified element is incompatible with this deque (optional)
NullPointerException if the specified element is null (optional)

removeLastOccurrence

Added in API level 9
boolean removeLastOccurrence (Object o)

从此双端队列中移除指定元素的最后一次出现。 如果该deque不包含该元素,则该值不变。 更正式地说,删除最后一个元素e ,使得o.equals(e) (如果存在这样的元素)。 如果此双端队列包含指定的元素(或者等价地,如果此双端队列因呼叫而改变),则返回true

Parameters
o Object: element to be removed from this deque, if present
Returns
boolean true if an element was removed as a result of this call
Throws
ClassCastException if the class of the specified element is incompatible with this deque (optional)
NullPointerException if the specified element is null (optional)

size

Added in API level 9
int size ()

返回此双端队列中的元素数量。

Returns
int the number of elements in this deque

take

Added in API level 9
E take ()

检索并移除由此双端队列表示的队列的头部(换句话说,此双端队列的第一个元素),如果有必要,则等待一个元素变为可用。

该方法相当于 takeFirst

Returns
E the head of this deque
Throws
InterruptedException if interrupted while waiting

takeFirst

Added in API level 9
E takeFirst ()

检索并删除此双端队列的第一个元素,如果需要等待,直到元素变为可用。

Returns
E the head of this deque
Throws
InterruptedException if interrupted while waiting

takeLast

Added in API level 9
E takeLast ()

检索并移除此双端队列的最后一个元素,如果需要,等待元素变为可用。

Returns
E the tail of this deque
Throws
InterruptedException if interrupted while waiting

Hooray!