Java LinkedTransferQueue drainTo() 方法

2025年3月21日 | 3 分钟阅读

LinkedTransferQueue() 类的 drainTo(Collection<? super E> c) 方法会移除此队列中的所有元素,并将它们添加到提供的集合中。

语法

参数

c - 要传输元素的集合。

指定者:

LinkedTransferQueue 类的 drainTo() 方法由 BlockingQueue 接口中的 drainTo() 方法指定。

返回

此方法返回传输的元素数量。

异常

NullPointerException - 如果指定的集合为 null,则会抛出此异常。

示例 1

输出

Elements int the queue = [0, 1]
Elements left in the queue :[]
Elements drained in the list[0, 1]

示例 2

输出

Elements int the queue = [0, 1, 2]
Elements left in the queue :[]
Elements drained in the list[0, 1, 2]

Java LinkedTransferQueue drainTo(Collection<? super E> c,int maxElements) 方法

LinkedTransferQueue() 类的 drainTo(Collection<? super E> c, int maxElements) 方法会移除此队列中的所有元素,并将它们添加到提供的集合中。

语法

参数

c - 要传输元素的集合。

maxElements - 要传输的最大元素数量。

指定者:

LinkedTransferQueue 类的 drainTo() 方法由 BlockingQueue<E> 接口中的 drainTo() 方法指定。

返回

此方法返回传输的元素数量。

异常

NullPointerException ? 如果指定的集合为 null,则会抛出此异常。

示例 3

输出

Elements int the queue = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Elements left in the queue :[5, 6, 7, 8, 9, 10]
Elements drained in the list[0, 1, 2, 3, 4]

示例 4

输出

Elements int the queue = [Harry, Jack, Kristan]
Elements left in the queue :[Kristan]
Elements drained in the list[Harry, Jack]