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 – 如果队列已满,提供方法将等待插入新元素的时间。
- unit – Timeout参数的时间单位。
返回值: 如果插入元素成功,该方法返回 true 。否则,如果在空间可用之前,指定的等待时间已过,则返回 false 。
异常: 该方法抛出以下异常:
- NullPointerException – 如果指定的元素为空。
- InterruptedException – 如果在等待时被打断。
下面的程序说明了BlockingQueue类的offer(E e, long timeout, TimeUnit unit)方法:
程序1:
// Java Program Demonstrate
// offer(Element e, long timeout, TimeUnit unit)
// method of BlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
// Main method
public static void main(String[] args)
throws InterruptedException
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add 5 elements to BlockingQueue having
// Timeout in seconds with value 5 secs in
// 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));
// print the elements of queue
System.out.println("list of numbers of queue:"
+ BQ);
// now queue is full check remaining capacity of queue
System.out.println("Empty spaces of queue : "
+ BQ.remainingCapacity());
// try to add more Integer
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 Program Demonstrate
// offer(Element e, long timeout, TimeUnit unit)
// method of 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
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add elements to BlockingQueue having
// Timeout in seconds with value 5 secs in
// offer(Element e, long timeout, TimeUnit unit)
System.out.println("Adding 283239 in Queue :"
+ BQ.offer(283239,
5,
TimeUnit.SECONDS));
// try to put null value in offer method
try {
System.out.println("Adding null in Queue: "
+ BQ.offer(null,
5,
TimeUnit.SECONDS));
}
catch (Exception e) {
// print error details
System.out.println("Exception: " + e);
}
// print elements of queue
System.out.println("Items in Queue are "
+ BQ);
}
}
输出
Adding 283239 in Queue :true
Exception: java.lang.NullPointerException
Items in Queue are [283239]
offer(E e)
BlockingQueue 的 offer(E e) 方法在队列有空间即队列未满的情况下,将作为参数传递的元素e插入该BlockingQueue的尾部。如果队列是满的,那么应用offer()方法就没有效果,因为BlockingQueue会阻止元素的插入。offer()方法在向BlockingQueue添加的操作成功时返回true,如果这个队列是满的则返回false。这个方法比add()方法更好,因为add方法在队列已满时抛出错误,但offer()方法在这种情况下返回false。
语法:
public boolean offer(E e)
参数: 该方法需要一个强制参数 e ,它是要插入到LinkedBlockingQueue中的元素。
返回值: 如果元素插入成功,该方法返回 true 。
异常: 如果指定的元素是空的,该方法会抛出 NullPointerException。
以下程序说明了BlockingQueue类的offer()方法
程序1:
// Java Program Demonstrate
// offer(Element e)
// method of BlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
// Main method
public static void main(String[] args)
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<String>
BQ = new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to BlockingQueue using offer
BQ.offer("dean");
BQ.offer("kevin");
BQ.offer("sam");
BQ.offer("jack");
// print the elements of queue
System.out.println("list of names of queue:");
System.out.println(BQ);
}
}
输出
list of names of queue:
[dean, kevin, sam, jack]
程序2 :
// Java Program Demonstrate
// offer(Element e)
// method of BlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
// Main method
public static void main(String[] args)
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<Integer>
BQ = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add element to BlockingQueue using offer
BQ.offer(34567);
BQ.offer(45678);
BQ.offer(98323);
BQ.offer(93758);
// print the elements of queue
System.out.println("list of numbers of queue:");
System.out.println(BQ);
// now queue is full check remaining capacity of queue
System.out.println("Empty spaces of queue : "
+ BQ.remainingCapacity());
// try to add extra Integer
boolean response = BQ.offer(2893476);
System.out.println("Adding new Integer 2893476 is successful: "
+ response);
response = BQ.offer(456751);
System.out.println("Adding new Integer 456751 is successful: "
+ response);
}
}
输出
list of numbers of queue:
[34567, 45678, 98323, 93758]
Empty spaces of queue : 0
Adding new Integer 2893476 is successful: false
Adding new Integer 456751 is successful: false
程序3: 显示由offer()方法抛出的异常
// Java Program Demonstrate offer(E e)
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element using offer() method
BQ.offer("Karan");
// try to put null value in offer method
try {
BQ.offer(null);
}
catch (Exception e) {
// print error details
System.out.println("Exception: " + e);
}
// print elements of queue
System.out.println("Items in Queue are "
+ BQ);
}
}
输出
Exception: java.lang.NullPointerException
Items in Queue are [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)