Java ArrayBlockingQueue poll() 方法

2025年1月7日 | 阅读 2 分钟

ArrayBlockingQueue 类的 poll() 方法检索此队列的头部,如果需要,会等待指定的时长以获取元素,然后将其移除。如果此队列为空,则该方法返回 null。

语法

参数

  • 不适用
  • timeout - 这是在放弃之前等待的时间(以 unit 为单位)
  • unit - 这是决定如何表示 timeout 参数的 TimeUnit

指定者:

ArrayBlockingQueue 类的 poll() 方法是在 Queue<E> 接口中的 poll() 方法规定的。

返回值

poll() 方法返回此队列的头部,如果此队列为空,或者在元素可用之前经过了指定的等待时间,则返回 null。

抛出

poll() 方法抛出

InterruptedException - 如果在等待期间方法被中断。

示例 1

输出

Head of queue is : Reema
Elements left in queue : [Rahul, Rita, Ramesh]

示例 2

输出

Head of queue : 67
Elements in queue : [109, 76, 876]

示例 3

输出

Reema gets the highest wages of 12000
Geetanjali gets the second  highest wages of 4500