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