1. offer(E e) Method
PriorityBlockingQueue的offer(E e)方法將作為參數傳遞的元素e插入此PriorityBlockingQueue中。此方法將元素插入此PriorityBlockingQueue中。由於PriorityBlockingQueue是不受限製的,因此永遠不會阻止此方法。
用法:
public boolean offer(E e)
參數:
此方法接受一個參數e,該參數代表我們要在此PriorityBlockingQueue中插入的元素e。
返回:
如果插入成功,則此方法返回布爾響應true。
異常:此方法引發以下異常。
- ClassCastException-如果不能根據PriorityBlockingQueue的順序將指定的元素與優先級隊列中的當前元素進行比較。
-
NullPointerException-如果元素為null
下麵的程序說明PriorityBlockingQueue的offer()方法:
範例1:該程序演示PriorityBlockingQueue上的offer()方法以添加數字列表。
// Java Program Demonstrate offer()
// method of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
// Add numbers to PriorityBlockingQueue using offer()
PrioQueue.offer(35658786);
PrioQueue.offer(5278367);
PrioQueue.offer(74381793);
PrioQueue.offer(87625142);
// print elements of PriorityBlockingQueue
System.out.println("Queue Contains:");
System.out.println(PrioQueue.toString());
}
}
Queue Contains: [5278367, 35658786, 74381793, 87625142]
範例2:程序,當我們嘗試向PriorityBlockingQueue添加null時,演示offer()方法引發的異常。
// Java Program Demonstrate offer()
// method of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue which contains
// name of students
PriorityBlockingQueue<String> names
= new PriorityBlockingQueue<String>(capacityOfQueue);
// Add names of students of girls college
names.offer("Joyita");
names.offer("Priyanka");
// try to insert null value in offer method
try {
names.offer(null);
}
catch (Exception e) {
// print error details
System.out.println("Exception Thrown: " + e);
}
}
}
Exception Thrown: java.lang.NullPointerException
2. offer(E e, long timeout, TimeUnit unit) Method
PriorityBlockingQueue的offer(E e,long timeout,TimeUnit unit)方法將作為參數傳遞的元素e插入此PriorityBlockingQueue中。此方法將元素插入此PriorityBlockingQueue中。由於PriorityBlockingQueue是不受限製的,因此此方法將永遠不會被阻塞,並且不會忽略timeout和timeUnit參數。此方法繼承自priorityBlockingQueue的supercals,後者是BlockingQueue類,但是由於priorityBlockingQueue從未阻止要插入的新元素,因此該方法繼承自super類在邏輯上與offer(E e)方法相同。
用法:
offer(E e, long timeout, TimeUnit unit)
參數:該方法接受三個參數
- e:要插入PriorityBlockingQueue中的元素。
- timeout:忽略此參數,因為隊列從不阻止新元素的插入。
- unit:也忽略此參數,因為隊列從不阻止新元素的插入。
返回值:如果插入成功,則此方法返回布爾響應true。
異常:此方法引發以下異常。
- ClassCastException-如果不能根據PriorityBlockingQueue的順序將指定的元素與優先級隊列中的當前元素進行比較。
-
NullPointerException-如果元素為null
下麵的程序說明PriorityBlockingQueue的offer(E e,長超時,TimeUnit單位)方法:
範例1:程序演示在PriorityBlockingQueue上的offer(E e,長超時,TimeUnit單位)方法,以添加數字列表。
// Java Program Demonstrate offer()
// method of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
// Add 3 elements to PriorityBlockingQueue using
// offer(Element e, long timeout, TimeUnit unit)
System.out.println("adding 1234 "
+ PrioQueue.offer(1234,
5, TimeUnit.SECONDS));
System.out.println("adding 2345 "
+ PrioQueue.offer(2345,
5, TimeUnit.SECONDS));
System.out.println("adding 3456 "
+ PrioQueue.offer(3456,
5, TimeUnit.SECONDS));
// print elements of PriorityBlockingQueue
System.out.println("Queue Contains:");
System.out.println(PrioQueue.toString());
}
}
adding 1234 true adding 2345 true adding 3456 true Queue Contains: [1234, 2345, 3456]
參考:
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#offer-E-
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#offer-E-long-java.util.concurrent.TimeUnit-
相關用法
- Java PriorityBlockingQueue add()用法及代碼示例
- Java PriorityBlockingQueue put()用法及代碼示例
- Java PriorityBlockingQueue contains()用法及代碼示例
- Java PriorityBlockingQueue take()用法及代碼示例
- Java ArrayDeque offer()用法及代碼示例
- Java LinkedBlockingDeque offer()用法及代碼示例
- Java PriorityQueue offer()用法及代碼示例
- Java Deque offer()用法及代碼示例
- Java LinkedTransferQueue offer()用法及代碼示例
- Java ArrayBlockingQueue offer()用法及代碼示例
- Java ConcurrentLinkedQueue offer()用法及代碼示例
- Java Queue offer()用法及代碼示例
- Java PriorityBlockingQueue toArray()用法及代碼示例
- Java PriorityBlockingQueue comparator()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 PriorityBlockingQueue offer() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。