📜  Java中的 BlockingQueue offer() 方法及示例

📅  最后修改于: 2022-05-13 01:54:45.704000             🧑  作者: Mango

Java中的 BlockingQueue offer() 方法及示例

BlockingQueue 接口有两种类型的 offer() 方法:
注意BlockingQueueoffer()方法继承自Java中的Queue类。

offer(E e, long timeout, TimeUnit 单位)

如果队列未满,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 - 直到提供方法将等待插入新元素的时间队列已满。
  • unit – 超时参数的时间单位。

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

  • NullPointerException – 如果指定元素为空。
  • InterruptedException – 如果在等待时被中断。

下面的程序说明了 BlockingQueue 类的 offer(E e, long timeout, TimeUnit unit) 方法:
方案一:

Java
// 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
            BQ = new LinkedBlockingQueue(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 successful: "
                           + response);
    }
}


Java
// 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
            BQ = new LinkedBlockingQueue(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);
    }
}


Java
// 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
            BQ = new LinkedBlockingQueue(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);
    }
}


Java
// 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
            BQ = new LinkedBlockingQueue(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 successful: "
                           + response);
 
        response = BQ.offer(456751);
 
        System.out.println("Adding new Integer 456751 is successful: "
                           + response);
    }
}


Java
// 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 BQ
            = new LinkedBlockingQueue(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);
    }
}


输出
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

方案二:

Java

// 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
            BQ = new LinkedBlockingQueue(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]

报价(E e)

BlockingQueue 的offer(E e)方法在此BlockingQueue的尾部插入作为参数传递的元素 e,如果队列有空间,即队列未满。如果队列已满,则应用 offer() 方法无效,因为 BlockingQueue 将阻止要插入的元素。如果添加到 BlockingQueue 的操作成功,则 offer() 方法返回 true,如果队列已满,则返回 false。此方法优于 add() 方法,因为 add() 方法在队列已满时抛出错误,但 offer() 方法在这种情况下返回 false。
句法:

public boolean offer(E e)

参数:此方法采用强制参数e ,即要插入 LinkedBlockingQueue 的元素。
返回值:如果元素插入成功,此方法返回true 。否则返回false
异常:如果指定元素为空,该方法将抛出NullPointerException
下面的程序说明了 BlockingQueue 类的 offer() 方法
方案一:

Java

// 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
            BQ = new LinkedBlockingQueue(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]

方案二:

Java

// 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
            BQ = new LinkedBlockingQueue(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 successful: "
                           + response);
 
        response = BQ.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

// 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 BQ
            = new LinkedBlockingQueue(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]

参考:

  • https://docs.oracle.com/javase/7/docs/api/java Java)
  • https://docs.oracle.com/javase/7/docs/api/java Java, %20long, %20java.util.concurrent.TimeUnit)