BlockingDeque的put(E e)方法将指定元素插入到此双端队列表示的队列中(换句话说,在此双端队列的末尾)。如果Deque受容量限制,则它将等待空间可用。
用法:
public void put(E e)
参数:此方法接受强制参数e,该参数是要在BlockingDeque末尾插入的元素。
返回值:此方法不返回任何内容。
异常:该程序将引发两个异常,如下所示:
- NullPointerException –如果指定的元素为null
- InterruptedException–如果在等待时被打断
注意:BlockingDeque的put()方法已从Java中的LinkedBlockingDeque类继承。
以下示例程序旨在说明BlockingDeque的put()方法:
示例1:
// Java Program Demonstrate put()
// 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 InterruptedException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.put(7855642);
BD.put(35658786);
BD.put(5278367);
BD.put(74381793);
// print Deque
System.out.println("Blocking Deque: " + BD);
}
}
输出:
Blocking Deque: [7855642, 35658786, 5278367, 74381793]
示例2:
// Java Program Demonstrate put()
// method of BlockingDeque
// throwing NullPointerException
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>();
// Add numbers to end of BlockingDeque
BD.put(7855642);
BD.put(35658786);
BD.put(5278367);
// throws an exception
BD.put(null);
// print Dequeue
System.out.println("Blocking Deque: " + BD);
}
}
输出:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.putLast(LinkedBlockingDeque.java:390) at java.util.concurrent.LinkedBlockingDeque.put(LinkedBlockingDeque.java:649) at GFG.main(GFG.java:24)
示例3:
// Java Program Demonstrate put()
// method of BlockingDeque
// when capacity exceeded
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.BlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// create object of BlockingDeque
BlockingDeque<Integer> BD
= new LinkedBlockingDeque<Integer>(3);
// Add numbers to end of BlockingDeque
BD.put(7855642);
BD.put(35658786);
BD.put(5278367);
// throws an exception
BD.put(4356789);
// print Dequeue
System.out.println("Blocking Deque: " + BD);
}
}
输出:
Max real time limit exceeded due to either by heavy load on server or by using sleep function
参考:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#put(E)
相关用法
- Java BlockingDeque add()用法及代码示例
- Java BlockingDeque take()用法及代码示例
- Java BlockingDeque contains()用法及代码示例
- Java BlockingDeque size()用法及代码示例
- Java BlockingDeque peek()用法及代码示例
- Java BlockingDeque remove()用法及代码示例
- Java BlockingDeque removeLastOccurrence()用法及代码示例
- Java BlockingDeque iterator()用法及代码示例
- Java BlockingDeque takeFirst()用法及代码示例
- Java BlockingDeque element()用法及代码示例
- Java BlockingDeque offerFirst()用法及代码示例
- Java BlockingDeque offerLast()用法及代码示例
- Java BlockingDeque addLast()用法及代码示例
- Java BlockingDeque addFirst()用法及代码示例
- Java BlockingDeque pollFirst()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 BlockingDeque put() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。