LinkedBlockingQueue | JAVA 中的 offer() 方法

LinkedBlockingQueue | JAVA 中的 offer() 方法

LinkedBlockingQueue 类中有两种 offer() 方法:

offer(E e, long timeout, TimeUnit unit)

LinkedBlockingQueueoffer(E e, long timeout, TimeUnit unit) 方法在队列不满的情况下,将传递给方法的元素插入到此 LinkedBlockingQueue 的尾部。如果队列已满,则它将等待一定的时间,直到有足够的空间可用为止。所以它将等待指定的时间和时间单位作为 offer() 方法的参数,直到 LinkedBlockingQueue 删除一些元素,以便此方法可以向 LinkedBlockingQueue 添加元素。

语法:

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

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

  • e - 要插入到 LinkedBlockingQueue 中的元素。
  • timeout - 如果队列已满,offer 方法将等待插入新元素的时间。
  • unit - timeout 参数的时间单位。

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

  • NullPointerException - 如果指定的元素为 null。
  • InterruptedException - 如果等待期间被中断。

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

程序 1: 使用 offer(E e, long timeout, TimeUnit unit) 方法插入学生的姓名,并以秒为单位给出 timeout 参数值,设置为 5 秒,创建 LinkedBlockingQueue。

// Java程序演示
// LinkedBlockingQueue的offer(Element e, long timeout, TimeUnit unit)方法。
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    // 主要方法
    public static void main(String[] args)
        throws InterruptedException
    {
        // 定义LinkedBlockingQueue的容量
        int capacityOfQueue = 4;
 
        // 创建LinkedBlockingQueue对象
        LinkedBlockingQueue<Integer>
            linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
 
        // 添加5个元素到ArrayBlockingQueue中,
        // 在offer(Element e, long timeout, TimeUnit unit)方法中设置5秒的超时时间。
        System.out.println("添加32673821:"
                           + linkedQueue.offer(32673821,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("添加88527183:"
                           + linkedQueue.offer(88527183,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("添加431278539:"
                           + linkedQueue.offer(431278539,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("添加351278693:"
                           + linkedQueue.offer(351278693,
                                               5,
                                               TimeUnit.SECONDS));
        System.out.println("添加647264:"
                           + linkedQueue.offer(647264,
                                               5,
                                               TimeUnit.SECONDS));
 
        // 打印队列中的元素
        System.out.println("队列中的数字列表:"
                           + linkedQueue);
 
        // 现在队列已经满了,检查队列的剩余容量
        System.out.println("队列中的空闲空间:"
                           + linkedQueue.remainingCapacity());
 
        // 尝试添加更多数字
        boolean response = linkedQueue.offer(2893476,
                                             5,
                                             TimeUnit.SECONDS);
        System.out.println("添加新的数字2893476成功:"
                           + response);
    }
}

输出:

添加32673821 true
添加88527183:true
添加431278539:true
添加351278693:true
添加647264:false
队列中的数字列表:[32673821, 88527183, 431278539, 351278693]
队列中的空闲空间:0
添加新的数字2893476成功:false

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

// Java程序演示了
// LinkedBlockingQueue的offer(Element e, long timeout, TimeUnit unit)方法。
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // 定义LinkedBlockingQueue的容量
        int capacityOfQueue = 4;
 
        // 创建LinkedBlockingQueue对象
        LinkedBlockingQueue<Integer>
            linkedQueue = new LinkedBlockingQueue<Integer>(capacityOfQueue);
 
        // 往ArrayBlockingQueue添加元素
        // 在offer(Element e, long timeout, TimeUnit unit)方法中设置5秒的超时时间
        System.out.println("将 283239 添加到队列:"
                           + linkedQueue.offer(283239,
                                               5,
                                               TimeUnit.SECONDS));
 
        // 尝试在offer方法中放入null值
        try {
 
            System.out.println("将 null 添加到队列:"
                               + linkedQueue.offer(null,
                                                   5,
                                                   TimeUnit.SECONDS));
        }
        catch (Exception e) {
 
            // 输出错误信息
            System.out.println("异常:" + e);
        }
 
        // 输出队列元素
        System.out.println("队列中的元素为:"
                           + linkedQueue);
    }
}

输出:

将 283239 添加到队列:true
异常:java.lang.NullPointerException
队列中的元素为:[283239]

offer(E e)

LinkedBlockingQueueoffer(E e) 方法在该队列的尾部插入作为参数传递的元素e,仅当队列有空间时(即未满)才插入。如果队列已满,则应用offer()方法没有效果,因为LinkedBlockingQueue会阻止元素的插入操作。当向LinkedBlockingQueue中添加元素的操作成功时,offer()方法将返回true,并在队列已满时返回false。与add()方法不同,该方法被推荐使用,因为当队列已满时,add()方法会抛出错误,而offer()方法会在这种情况下返回false。 语法:

public boolean offer(E e)

参数: 该方法需要一个强制使用的参数 e ,该参数是要插入到LinkedBlockingQueue中的元素。

返回值: 该方法在插入元素成功时返回 true ,否则返回 false

异常: 如果指定的元素为null,则该方法将抛出 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)
    {
        // create object of LinkedBlockingQueue with no limit
        LinkedBlockingQueue<String>
            linkedQueue = new LinkedBlockingQueue<String>();
 
        // Add element to LinkedBlockingQueue using offer
        linkedQueue.offer("dean");
        linkedQueue.offer("kevin");
        linkedQueue.offer("sam");
        linkedQueue.offer("jack");
 
        // try to add extra element
        boolean response = linkedQueue.offer("john");
 
        // print response of offer method
        System.out.println("Adding new name john is successful: "
                           + response);
 
        // At this point, LinkedBlockingQueue doesn't
        // have any space to add new element
 
        // try to add extra element
        response = linkedQueue.offer("jacob");
 
        // print response of offer method
        System.out.println("Adding new name jacob is successful: "
                           + response);
    }
}

输出:

Adding new name john is successful: true
Adding new name jacob is successful: false
// Java程序展示LinkedBlockingQueue的offer(E e)方法
 
import java.util.concurrent.LinkedBlockingQueue;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        //定义LinkedBlockingQueue的容量
        int capacityOfQueue = 4;
 
        //创建LinkedBlockingQueue对象
        LinkedBlockingQueue<String> linkedQueue
            = new LinkedBlockingQueue<String>(capacityOfQueue);
 
        //使用offer()方法添加元素
        linkedQueue.offer("Karan");
 
        //尝试在offer方法中放入null值
        try {
            linkedQueue.offer(null);
        }
        catch (Exception e) {
            //打印错误详情
            System.out.println("Exception: " + e);
        }
 
        //打印队列中的元素
        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/util/concurrent/LinkedBlockingQueue.html#offer-E-long-java.util.concurrent.TimeUnit-
  • https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#offer-E-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程