BlockingDeque的push(E e)方法将元素压入此双端队列表示的堆栈上。如果有空间,它将在参数中传递的元素插入到双端队列的前面。如果BlockingDeque受容量限制并且没有剩余空间可插入,则它将返回IllegalStateException。此函数类似于addFirst()。
用法:
public void push(E e)
参数:此方法接受强制参数e,该参数是要在LinkedBlockingDeque的前面插入的元素。
注意:BlockingDeque的push()方法已从Java中的LinkedBlockingDeque类继承。
返回值:此方法不返回任何内容。
异常:
- IllegalStateException:如果由于容量限制此时无法添加元素
- NullPointerException :如果指定的元素为null
以下示例程序旨在说明BlockingDeque的push()方法:
示例1:
// Java Program Demonstrate push()
// method of BlockingDeque
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of dBlockingDeque
BD.push(7855642);
BD.push(35658786);
BD.push(5278367);
BD.push(74381793);
// before removing print queue
System.out.println("Blocking Deque: " + BD);
}
}
输出:
Blocking Deque: [74381793, 5278367, 35658786, 7855642]
示例2:
// Java Program Demonstrate push()
// method of BlockingDeque
// when it is Full
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
// size of list
BlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>(3);
// Add numbers to end of BlockingDeque
BD.push(7855642);
BD.push(35658786);
BD.push(5278367);
// it is full
BD.push(74381793);
// before removing print queue
System.out.println("Blocking Deque: " + BD);
}
}
输出:
Exception in thread "main" java.lang.IllegalStateException: Deque full at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:326) at java.util.concurrent.LinkedBlockingDeque.push(LinkedBlockingDeque.java:770) at GFG.main(GFG.java:24)
示例3:
// Java Program Demonstrate push()
// method of BlockingDeque
// when null is inserted
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.push(7855642);
BD.push(35658786);
BD.push(5278367);
// NULL
BD.push(null);
// before removing print Deque
System.out.println("Blocking Deque: " + BD);
}
}
输出:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.offerFirst(LinkedBlockingDeque.java:342) at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:325) at java.util.concurrent.LinkedBlockingDeque.push(LinkedBlockingDeque.java:770) at GFG.main(GFG.java:24)
参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#push(E)
相关用法
- Java BlockingDeque add()用法及代码示例
- Java BlockingDeque put()用法及代码示例
- Java BlockingDeque take()用法及代码示例
- Java BlockingDeque contains()用法及代码示例
- Java BlockingDeque iterator()用法及代码示例
- Java BlockingDeque offerFirst()用法及代码示例
- Java BlockingDeque putFirst()用法及代码示例
- Java BlockingDeque addLast()用法及代码示例
- Java BlockingDeque takeLast()用法及代码示例
- Java BlockingDeque pollFirst()用法及代码示例
- Java BlockingDeque removeFirstOccurrence()用法及代码示例
- Java BlockingDeque addFirst()用法及代码示例
- Java BlockingDeque element()用法及代码示例
- Java BlockingDeque offerLast()用法及代码示例
- Java BlockingDeque peek()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 BlockingDeque push() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。