当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java PriorityBlockingQueue offer()用法及代码示例


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)

参数:该方法接受三个参数


  1. e:要插入PriorityBlockingQueue中的元素。
  2. timeout:忽略此参数,因为队列从不阻止新元素的插入。
  3. 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]

参考:



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 PriorityBlockingQueue offer() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。