Java中的BlockingQueue offer()方法及其示例
BlockingQueue接口有两种类型的offer()方法:
注意: BlockingQueue 的 offer() 方法是从Java的 Queue 类继承而来的。
offer(E e, long timeout, TimeUnit unit)
如果队列不满,BlockingQueue的 offer(E e, long timeout, TimeUnit unit) 方法会将传递给方法的元素插入到该BlockingQueue的尾部。如果BlockingQueue已满,则它将等待一段指定的时间,直到有空间变得可用。将等待时间和时间单位作为参数传递给offer()方法。因此,它将等到该时间为止,以便BlockingQueue删除一些元素,从而使此方法可以向BlockingQueue添加元素。
语法:
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
参数: 此方法接受三个参数:
- e - 要插入到BlockingQueue中的元素。
- timeout - 如果队列已满,则offer方法等待插入新元素的时间。
- unit - timeout参数的时间单位。
返回值: 如果元素插入成功,此方法将返回 true 。否则,如果在空间可用之前经过了指定的等待时间,则返回 false 。
异常: 此方法会抛出以下异常:
- NullPointerException - 如果指定元素为null。
- InterruptedException - 如果在等待时被中断。
以下程序说明了BlockingQueue类的offer(E e, long timeout, TimeUnit unit)方法:
程序 1:
// Java程序演示
// BlockingQueue.offer(Element e, long timeout, TimeUnit unit)
// 方法。
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
// 主方法
public static void main(String[] args)
throws InterruptedException
{
// 定义阻塞队列的容量
int capacityOfQueue = 4;
// 创建一个阻塞队列对象
BlockingQueue
BQ = new LinkedBlockingQueue(capacityOfQueue);
// 向阻塞队列添加5个元素,
// 超时为5秒,单位为秒,在
// offer(Element e, long timeout, TimeUnit unit)中
System.out.println("adding 32673821 "
+ BQ.offer(32673821,
5,
TimeUnit.SECONDS));
System.out.println("adding 88527183: "
+ BQ.offer(88527183,
5,
TimeUnit.SECONDS));
System.out.println("adding 431278539: "
+ BQ.offer(431278539,
5,
TimeUnit.SECONDS));
System.out.println("adding 351278693: "
+ BQ.offer(351278693,
5,
TimeUnit.SECONDS));
System.out.println("adding 647264: "
+ BQ.offer(647264,
5,
TimeUnit.SECONDS));
// 打印队列的元素
System.out.println("list of numbers of queue:"
+ BQ);
// 现在队列已满,检查队列剩余容量
System.out.println("Empty spaces of queue : "
+ BQ.remainingCapacity());
// 尝试添加更多整数
boolean response = BQ.offer(2893476,
5,
TimeUnit.SECONDS);
System.out.println("Adding new Integer 2893476 is successful: "
+ response);
}
}
输出
adding 32673821 true
adding 88527183: true
adding 431278539: true
adding 351278693: true
adding 647264: false
list of numbers of queue:[32673821, 88527183, 431278539, 351278693]
Empty spaces of queue : 0
Adding new Integer 2893476 is successful: false
程序2:
// Java程序演示
// offer(Element e, long timeout, TimeUnit unit)
//方法的BlockingQueue。
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// 定义BlockingQueue的容量
int capacityOfQueue = 4;
// 创建BlockingQueue对象
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// 添加元素到BlockingQueue中
// Timeout为5秒
System.out.println("将283239添加到队列中 :"
+ BQ.offer(283239,
5,
TimeUnit.SECONDS));
// 对offer方法传递null值进行尝试
try {
System.out.println("在队列中添加null : "
+ BQ.offer(null,
5,
TimeUnit.SECONDS));
}
catch (Exception e) {
// 打印错误信息
System.out.println("异常: " + e);
}
// 打印队列中的元素
System.out.println("队列中的元素为: "
+ BQ);
}
}
输出:
将283239添加到队列中 :true
异常: java.lang.NullPointerException
队列中的元素为: [283239]
offer(E e)
BlockingQueue 的 offer(E e) 方法将作为参数传递的元素e插入到此BlockingQueue的末尾,如果队列还有空间,否则应用offer()方法不生效,因为BlockingQueue会阻止元素被插入。 当成功添加元素到BlockingQueue时,offer()方法返回true,如果该队列已满,则返回false。 在队列已满的情况下,这个方法比add()方法更受欢迎,因为add()方法在队列已满时抛出错误,而offer()方法在这种情况下返回false。
语法:
public boolean offer(E e)
参数: 此方法需要一个强制性参数 e ,它是要插入到LinkedBlockingQueue中的元素。
返回值: 如果成功添加元素,则此方法返回 true 。否则返回值为 false 。
异常: 如果指定的元素为null,则该方法将引发 NullPointerException 。
下面的程序说明了BlockingQueue类的offer()方法
程序1:
// Java程序演示
// BlockingQueue的offer(Element e)方法
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
// 主方法
public static void main(String[] args)
{
// 定义BlockingQueue的容量
int capacityOfQueue = 4;
// 创建BlockingQueue对象
BlockingQueue<String>
BQ = new LinkedBlockingQueue<String>(capacityOfQueue);
// 使用offer()方法将元素添加到BlockingQueue
BQ.offer("dean");
BQ.offer("kevin");
BQ.offer("sam");
BQ.offer("jack");
// 打印队列中的元素
System.out.println("队列中的名称列表:");
System.out.println(BQ);
}
}
输出:
队列名称列表:
[Dean,Kevin,Sam,Jack]
程序2:
//Java程序演示了
//offer(Element e)
//BlockingQueue方法。
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG{
// 主要方法
public static void main(String[] args)
{
//定义BlockingQueue的容量
int capacityOfQueue = 4;
//创建BlockingQueue对象
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
//使用offer向阻止队列添加元素
BQ.offer(34567);
BQ.offer(45678);
BQ.offer(98323);
BQ.offer(93758);
//打印队列的元素
System.out.println("queue的数字列表:");
System.out.println(BQ);
//现在队列已满,检查队列的剩余容量
System.out.println("队列的空闲空间 : "
+ BQ.remainingCapacity());
//试着添加额外的Integer
boolean response = BQ.offer(2893476);
System.out.println("添加新的Integer 2893476是否成功:" + response);
response = BQ.offer(456751);
System.out.println("添加新的Integer 456751是否成功:" + response);
}
}
输出
queue的数字列表:
[34567, 45678, 98323, 93758]
队列的空闲空间 : 0
添加新的Integer 2893476是否成功:false
添加新的Integer 456751是否成功:false
程序3: 展示offer()方法引发的异常
//Java程序演示offer(E e)
//LinkedBlockingQueue方法
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG{
public static void main(String[] args)
throws InterruptedException
{
// 定义BlockingQueue的容量
int capacityOfQueue = 4;
// 创建BlockingQueue对象
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
// 使用offer()方法添加元素
BQ.offer("Karan");
//尝试在offer方法中放置空值
try {
BQ.offer(null);
}
catch (Exception e) {
//打印错误详细信息
System.out.println("Exception: " + e);
}
//打印队列的元素
System.out.println("队列中的项目是"
+ BQ);
}
}
输出:
Exception:java.lang.NullPointerException
队列中的项目是[Karan]
参考:
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#offer(E)
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#offer(E,%20long,%20java.util.concurrent.TimeUnit)
极客教程