LinkedBlockingDeque的add(E e)方法将在参数中传递的元素插入到Deque的末尾,如果有空间。如果LinkedBlockingDeque受容量限制并且没有剩余空间可插入,则它返回IllegalStateException。它的工作方式与addLast()方法完全相同。
用法:
public void add(E e)
参数:此方法接受强制参数e,该参数是要在LinkedBlockingDeque末尾插入的元素。
返回值:此方法不返回任何内容。
异常:
- IllegalStateException:如果由于容量限制此时无法添加元素
- NullPointerException :如果指定的元素为null
以下示例程序旨在说明LinkedBlockingDeque的add()方法:
示例1:
// 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<Integer> LBD
= new LinkedBlockingDeque<Integer>();
// 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]
示例2:
// 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<Integer> LBD
= new LinkedBlockingDeque<Integer>(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<Integer> LBD
= new LinkedBlockingDeque<Integer>();
// 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://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#add(E)
相关用法
- Java LinkedBlockingDeque put()用法及代码示例
- Java LinkedBlockingDeque take()用法及代码示例
- Java LinkedBlockingDeque pop()用法及代码示例
- Java LinkedBlockingDeque contains()用法及代码示例
- Java LinkedBlockingDeque peekFirst()用法及代码示例
- Java LinkedBlockingDeque addFirst()用法及代码示例
- Java LinkedBlockingDeque element()用法及代码示例
- Java LinkedBlockingDeque putLast()用法及代码示例
- Java LinkedBlockingDeque remainingCapacity()用法及代码示例
- Java LinkedBlockingDeque removeLast()用法及代码示例
- Java LinkedBlockingDeque putFirst()用法及代码示例
- Java LinkedBlockingDeque removeFirst()用法及代码示例
- Java LinkedBlockingDeque getFirst()用法及代码示例
- Java LinkedBlockingDeque toString()用法及代码示例
- Java LinkedBlockingDeque spliterator()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 LinkedBlockingDeque add() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。