當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。