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


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已满,它将等待直到指定的时间空间可用。指定的等待时间和时间的TimeUnit将作为offer()方法的参数给出。因此,它将等到BlockingQueue删除某些元素为止,以便此方法可以将元素添加到BlockingQueue。


用法:

public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException

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

  • e–要插入BlockingQueue的元素。
  • timeout– offer方法将等待插入新元素的时间是队列已满。
  • unit–超时时间单位参数。

返回值:如果成功插入元素,则该方法返回true。否则,如果在空间可用之前经过了指定的等待时间,则它返回false。

异常:此方法引发以下异常:

  • NullPointerException –如果指定的元素为null。
  • 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 succesful: "
                           + 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 succesful: 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将阻止要插入的元素。当添加到BlockingQueue的操作成功时,offer()方法返回true;如果此队列已满,则返回false。此方法优于add()方法,因为在队列已满时add方法会引发错误,但在这种情况下offer()方法将返回false。


用法:

public boolean offer(E e)

参数:此方法采用强制性参数e,该参数是要插入LinkedBlockingQueue中的元素。

返回值:如果成功插入元素,则此方法返回true。否则返回false。

异常:如果指定的元素为null,则该方法将引发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 succesful: "
                           + response); 
  
        response = BQ.offer(456751); 
  
        System.out.println("Adding new Integer 456751 is succesful: "
                           + response); 
    } 
}
输出:
list of numbers of queue:
[34567, 45678, 98323, 93758]
Empty spaces of queue : 0
Adding new Integer 2893476 is succesful: false
Adding new Integer 456751 is succesful: 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]

参考:



相关用法


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