Java LinkedBlockingQueue offer()方法
对于LinkedBlockingQueue类,有两种类型的offer()方法。
offer(E e, long timeout, TimeUnit unit)
LinkedBlockingQueue的 offer(E e, long timeout, TimeUnit unit) 方法在队列未满的情况下,将作为参数传递给方法的元素插入该LinkedBlockingQueue的尾部。如果LinkedBlockingQueue满了,它将会等待到指定的时间,让空间变得可用。指定的等待时间和时间单位将被作为参数给到offer()方法。因此,它将等待LinkedBlockingQueue删除一些元素,以便该方法能够将元素添加到LinkedBlockingQueue中。
语法
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
参数: 该方法接受三个参数。
- e – 要插入到LinkedBlockingQueue中的元素。
- timeout – 如果队列已满,提供方法将等待插入新元素的时间。
- unit – 超时参数的时间单位。
返回值: 如果插入元素成功,该方法返回 true 。否则,如果在空间可用之前,指定的等待时间已过,它返回 false 。
异常: 该方法会抛出以下异常。
- NullPointerException – 如果指定的元素为空。
- InterruptedException – 如果在等待时被打断。
下面的程序说明了LinkedBlockingQueue类的offer(E e, long timeout, TimeUnit unit) 方法。
程序1:使用offer(E e, long timeout, TimeUnit unit)方法插入学生的名字,创建LinkedBlockingQueue,其中timeunit参数为秒,timeout参数为5秒。
// Java Program Demonstrate
// offer(Element e, long timeout, TimeUnit unit)
// method of LinkedBlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
// Main method
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer>
linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add 5 elements to ArrayBlockingQueue having
// Timeout in seconds with value 5 secs in
// offer(Element e, long timeout, TimeUnit unit)
System.out.println("adding 32673821 "
+ linkedQueue.offer(32673821,
5,
TimeUnit.SECONDS));
System.out.println("adding 88527183: "
+ linkedQueue.offer(88527183,
5,
TimeUnit.SECONDS));
System.out.println("adding 431278539: "
+ linkedQueue.offer(431278539,
5,
TimeUnit.SECONDS));
System.out.println("adding 351278693: "
+ linkedQueue.offer(351278693,
5,
TimeUnit.SECONDS));
System.out.println("adding 647264: "
+ linkedQueue.offer(647264,
5,
TimeUnit.SECONDS));
// print the elements of queue
System.out.println("list of numbers of queue:"
+ linkedQueue);
// now queue is full check remaining capacity of queue
System.out.println("Empty spaces of queue : "
+ linkedQueue.remainingCapacity());
// try to add more Integer
boolean response = linkedQueue.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:显示由offer(Element e, long timeout, TimeUnit unit)方法抛出的异常。
// Java Program Demonstrate
// offer(Element e, long timeout, TimeUnit unit)
// method of LinkedBlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer>
linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add elements to ArrayBlockingQueue having
// Timeout in seconds with value 5 secs in
// offer(Element e, long timeout, TimeUnit unit)
System.out.println("Adding 283239 in Queue :"
+ linkedQueue.offer(283239,
5,
TimeUnit.SECONDS));
// try to put null value in offer method
try {
System.out.println("Adding null in Queue: "
+ linkedQueue.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 "
+ linkedQueue);
}
}
输出:
Adding 283239 in Queue :true
Exception: java.lang.NullPointerException
Items in Queue are [283239]
offer(E e)
LinkedBlockingQueue的offer(E e)方法将作为参数传递的元素e插入这个LinkedBlockingQueue的尾部,如果队列有空间,即队列未满。如果队列是满的,那么应用offer()方法就没有效果,因为LinkedBlockingQueue会阻止元素的插入。offer()方法在向LinkedBlockingQueue添加操作成功时返回true,如果队列是满的,则返回false。这个方法比add()方法更受欢迎,因为add方法在队列已满时抛出错误,但offer()方法在这种情况下返回false。
语法
public boolean offer(E e)
参数: 该方法需要一个强制参数e,即要插入到LinkedBlockingQueue中的元素。
返回值: 如果插入的元素成功,该方法返回 true 。否则,返回 false。
异常: 如果指定的元素为空,该方法会抛出NullPointerException。
下面的程序说明了LinkedBlockingQueue类的offer()方法
程序1:使用offer()方法插入学生的名字,创建LinkedBlockingQueue。
// Java Program Demonstrate
// offer(Element e)
// method of LinkedBlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
// Main method
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String>
linkedQueue = new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to LinkedBlockingQueue using offer
linkedQueue.offer("dean");
linkedQueue.offer("kevin");
linkedQueue.offer("sam");
linkedQueue.offer("jack");
// print the elements of queue
System.out.println("list of names of queue:");
System.out.println(linkedQueue);
}
}
输出:
list of names of queue:
[dean, kevin, sam, jack]
程序2:检查LinkedBlockingQueue是否已满,然后插入新元素或不插入。
// Java Program Demonstrate
// offer(Element e)
// method of LinkedBlockingQueue.
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
// Main method
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer>
linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add element to LinkedBlockingQueue using offer
linkedQueue.offer(34567);
linkedQueue.offer(45678);
linkedQueue.offer(98323);
linkedQueue.offer(93758);
// print the elements of queue
System.out.println("list of numbers of queue:");
System.out.println(linkedQueue);
// now queue is full check remaining capacity of queue
System.out.println("Empty spaces of queue : "
+ linkedQueue.remainingCapacity());
// try to add extra Integer
boolean response = linkedQueue.offer(2893476);
System.out.println("Adding new Integer 2893476 is successful: "
+ response);
response = linkedQueue.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;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 4;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element using offer() method
linkedQueue.offer("Karan");
// try to put null value in offer method
try {
linkedQueue.offer(null);
}
catch (Exception e) {
// print error details
System.out.println("Exception: " + e);
}
// print elements of queue
System.out.println("Items in Queue are "
+ linkedQueue);
}
}
输出:
Exception: java.lang.NullPointerException
Items in Queue are [Karan]
参考资料
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#offer-E-long-java.util.concurrent.TimeUnit-
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#offer-E-
极客教程