Java ArrayBlockingQueue offer() 方法

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

Java ArrayBlockingQueue 类的 offer() 方法将定义的元素添加到此队列的尾部,仅当队列未满时,或者如果队列已满,则等到指定的时间来等待空间可用。

语法

参数

e - 这是要添加的元素。

timeout - 这是在放弃之前等待的时间(以 unit 的单位表示)。

unit - 这是决定 timeout 参数如何表示的 TimeUnit。

指定者:

ArrayBlockingQueue 类的 offer() 方法由以下接口指定:

  1. Queue<E> 接口中的 offer() 方法。
  2. BlockingQueue<E> 接口中的 offer() 方法。

抛出

offer() 方法抛出

  1. NullPointerException - 如果定义的元素包含 null 值。
  2. InterruptedException - 如果在等待过程中方法被中断。

返回值

offer() 方法在成功插入时返回 true,否则返回 false。

offer() 方法在成功插入时返回 true,或者在可用空间的时间超过指定的等待时间时返回 false。

示例 1

输出

567
56
5671
5167
5670

示例 2

输出

Reema
Rahul
Rita
Ramesh

示例 3

输出

Reema       Panda     [email protected]
Geetanjali  Sharma    [email protected]
Vineet      Tanjea    [email protected]
Himanshu    Bhardwaj  [email protected]