📌  相关文章
📜  Java中的 LinkedBlockingDeque add() 方法

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

Java中的 LinkedBlockingDeque add() 方法

LinkedBlockingDequeadd(E e)方法将参数中传入的元素插入到 Deque 末尾是否有空格。如果 LinkedBlockingDeque 容量受限且没有空间可插入,则返回IllegalStateException 。它的工作方式与 addLast() 方法完全相同。

句法:

public void add(E e)

参数:此方法接受一个强制参数e ,它是要插入到 LinkedBlockingDeque 末尾的元素。

返回:此方法不返回任何内容。

例外:

  • IllegalStateException : 如果此时由于容量限制无法添加元素
  • NullPointerException : 如果指定元素为 null

下面的程序说明了 LinkedBlockingDeque 的 add() 方法:

方案一:

// Java Program Demonstrate add()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(7855642);
        LBD.add(35658786);
        LBD.add(5278367);
        LBD.add(74381793);
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}
输出:
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]

方案二:

// Java Program Demonstrate add()
// method of LinkedBlockingDeque
// when it is Full
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        // size of list
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque(3);
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(7855642);
        LBD.add(35658786);
        LBD.add(5278367);
  
        // it is full
        LBD.add(74381793);
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}

输出:

Exception in thread "main" java.lang.IllegalStateException: Deque full
    at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:335)
    at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633)
    at GFG.main(GFG.java:23)

方案 3:

// Java Program Demonstrate add()
// method of LinkedBlockingDeque
// when null is inserted
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque LBD
            = new LinkedBlockingDeque();
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.add(7855642);
        LBD.add(35658786);
        LBD.add(5278367);
  
        // NULL
        LBD.add(null);
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}

输出:

Exception in thread "main" java.lang.NullPointerException
    at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357)
    at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:334)
    at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633)
    at GFG.main(GFG.java:23)

参考: https: Java/util/concurrent/LinkedBlockingDeque.html#add(E)