Java中的 BlockingDeque push() 方法及示例
BlockingDeque的push(E e)方法将元素推送到此双端队列表示的堆栈上。如果有空格,它将参数中传递的元素插入到 Deque 的前面。如果 BlockingDeque 容量受限且没有空间可插入,则返回IllegalStateException 。该函数类似于 addFirst() 的函数。
句法:
public void push(E e)
参数:此方法接受一个强制参数e ,它是要插入到 LinkedBlockingDeque 前面的元素。
注意: BlockingDeque的 push() 方法继承自Java中的 LinkedBlockingDeque 类。
返回:此方法不返回任何内容。
例外:
- IllegalStateException : 如果此时由于容量限制无法添加元素
- NullPointerException : 如果指定元素为 null
下面的程序说明了 BlockingDeque 的 push() 方法:
方案一:
Java
// 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 BD
= new LinkedBlockingDeque();
// Add numbers to end of BlockingDeque
BD.push(7855642);
BD.push(35658786);
BD.push(5278367);
BD.push(74381793);
// before removing print queue
System.out.println("Blocking Deque: " + BD);
}
}
Java
// 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 LBD
= new LinkedBlockingDeque(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);
}
}
Java
// 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 BD
= new LinkedBlockingDeque();
// 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);
}
}
输出:
Blocking Deque: [74381793, 5278367, 35658786, 7855642]
方案二:
Java
// 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 LBD
= new LinkedBlockingDeque(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
// 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 BD
= new LinkedBlockingDeque();
// 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: Java/util/concurrent/BlockingDeque.html#push(E)