📜  LinkedBlockingQueue | Java中的offer()方法

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

LinkedBlockingQueue | Java中的offer()方法

LinkedBlockingQueue 类有两种 offer() 方法:

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

如果队列未满,LinkedBlockingQueue 的offer(E e, long timeout, TimeUnit unit)方法会将作为参数传递给方法的元素插入到此LinkedBlockingQueue的尾部。如果 LinkedBlockingQueue 已满,它将等到指定时间才有可用空间。指定的等待时间和 TimeUnit 将作为参数提供给 offer() 方法。所以它会等到那个时候 LinkedBlockingQueue 删除一些元素,以便这个方法可以将元素添加到 LinkedBlockingQueue。

句法:

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

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

  • e – 要插入 LinkedBlockingQueue 的元素。
  • timeout - 直到提供方法将等待插入新元素的时间队列已满。
  • unit – 超时参数的时间单位。

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

异常:此方法抛出以下异常:

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

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

方案一:使用offer(E e, long timeout, TimeUnit unit)方法插入学生姓名创建LinkedBlockingQueue,timeunit参数以秒为单位,超时参数为5sec。

// Java Program Demonstrate
// offer(Element e, long timeout, TimeUnit unit)
// method of LinkedBlockingQueue.
  
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue
            linkedQueue = new LinkedBlockingQueue(capacityOfQueue);
  
        // Add 5 elements to ArrayBlockingQueue having
        // Timeout in seconds with value 5 secs in
        // offer(Element e, long timeout, TimeUnit unit)
        System.out.println("adding 32673821 "
                           + linkedQueue.offer(32673821,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("adding 88527183: "
                           + linkedQueue.offer(88527183,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("adding 431278539: "
                           + linkedQueue.offer(431278539,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("adding 351278693: "
                           + linkedQueue.offer(351278693,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("adding 647264: "
                           + linkedQueue.offer(647264,
                                               5,
                                               TimeUnit.SECONDS));
  
        // print the elements of queue
        System.out.println("list of numbers of queue:"
                           + linkedQueue);
  
        // now queue is full check remaining capacity of queue
        System.out.println("Empty spaces of queue : "
                           + linkedQueue.remainingCapacity());
  
        // try to add more Integer
        boolean response = linkedQueue.offer(2893476,
                                             5,
                                             TimeUnit.SECONDS);
        System.out.println("Adding new Integer 2893476 is successful: "
                           + 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 successful: false

程序 2:显示 offer(Element e, long timeout, TimeUnit unit) 方法抛出的异常

// Java Program Demonstrate
// offer(Element e, long timeout, TimeUnit unit)
// method of LinkedBlockingQueue.
  
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
  
public class GFG {
  
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue
            linkedQueue = new LinkedBlockingQueue(capacityOfQueue);
  
        // Add elements to ArrayBlockingQueue having
        // Timeout in seconds with value 5 secs in
        // offer(Element e, long timeout, TimeUnit unit)
        System.out.println("Adding 283239 in Queue :"
                           + linkedQueue.offer(283239,
                                               5,
                                               TimeUnit.SECONDS));
  
        // try to put null value in offer method
        try {
  
            System.out.println("Adding null in Queue: "
                               + linkedQueue.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 "
                           + linkedQueue);
    }
}
输出:
Adding 283239 in Queue :true
Exception: java.lang.NullPointerException
Items in Queue are [283239]

报价(E e)

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

句法:

public boolean offer(E e)

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

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

异常:如果指定元素为空,该方法将抛出NullPointerException

下面的程序说明了 LinkedBlockingQueue 类的 offer() 方法

程序 1:通过使用 offer() 方法插入学生姓名来创建 LinkedBlockingQueue。

// Java Program Demonstrate
// offer(Element e)
// method of LinkedBlockingQueue.
  
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue
            linkedQueue = new LinkedBlockingQueue(capacityOfQueue);
  
        // Add element to LinkedBlockingQueue using offer
        linkedQueue.offer("dean");
        linkedQueue.offer("kevin");
        linkedQueue.offer("sam");
        linkedQueue.offer("jack");
  
        // print the elements of queue
        System.out.println("list of names of queue:");
        System.out.println(linkedQueue);
    }
}
输出:
list of names of queue:
[dean, kevin, sam, jack]

程序 2:检查 LinkedBlockingQueue 是否已满,然后插入新元素。

// Java Program Demonstrate
// offer(Element e)
// method of LinkedBlockingQueue.
  
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue
            linkedQueue = new LinkedBlockingQueue(capacityOfQueue);
  
        // Add element to LinkedBlockingQueue using offer
        linkedQueue.offer(34567);
        linkedQueue.offer(45678);
        linkedQueue.offer(98323);
        linkedQueue.offer(93758);
  
        // print the elements of queue
        System.out.println("list of numbers of queue:");
        System.out.println(linkedQueue);
  
        // now queue is full check remaining capacity of queue
        System.out.println("Empty spaces of queue : "
                           + linkedQueue.remainingCapacity());
  
        // try to add extra Integer
        boolean response = linkedQueue.offer(2893476);
  
        System.out.println("Adding new Integer 2893476 is successful: "
                           + response);
  
        response = linkedQueue.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 Program Demonstrate offer(E e)
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue linkedQueue
            = new LinkedBlockingQueue(capacityOfQueue);
  
        // Add element using offer() method
        linkedQueue.offer("Karan");
  
        // try to put null value in offer method
        try {
            linkedQueue.offer(null);
        }
        catch (Exception e) {
            // print error details
            System.out.println("Exception: " + e);
        }
  
        // print elements of queue
        System.out.println("Items in Queue are "
                           + linkedQueue);
    }
}
输出:
Exception: java.lang.NullPointerException
Items in Queue are [Karan]

参考:

  • https://docs.oracle.com/javase/8/docs/api/java Java
  • https://docs.oracle.com/javase/8/docs/api/java Java