如果有空間,則BlockingDeque的addFirst(E e)方法會將在參數中傳遞的元素插入到Deque的前麵。如果BlockingDeque受容量限製並且沒有剩餘空間可插入,則它將返回IllegalStateException。
用法:
public void addFirst(E e)
參數:此方法接受強製參數e,該參數是要插入到BlockingDeque前麵的元素。
返回值:此方法不返回任何內容。
異常:
- IllegalStateException:如果由於容量限製此時無法添加元素
- NullPointerException :如果指定的元素為null
注意:BlockingDeque的addFirst()方法已從Java中的LinkedBlockingDeque類繼承。
下麵的程序說明BlockingQueue的addFirst()方法:
示例1:
// Java Program Demonstrate addFirst()
// 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 front of BlockingDeque
BD.addFirst(7855642);
BD.addFirst(35658786);
BD.addFirst(5278367);
BD.addFirst(74381793);
// before removing print Dequq
System.out.println("Blocking Deque: " + BD);
}
}
輸出:
Blocking Deque: [74381793, 5278367, 35658786, 7855642]
示例2:
// Java Program Demonstrate addFirst()
// 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> BD
= new LinkedBlockingDeque<Integer>(3);
// Add numbers to front of BlockingDeque
BD.addFirst(7855642);
BD.addFirst(35658786);
BD.addFirst(5278367);
// it is full
BD.addFirst(74381793);
// before removing print Deque
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 GFG.main(GFG.java:24)
示例3:
// Java Program Demonstrate addFirst()
// method of BlockingDeque
// when nill is inserted
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
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 front of BlockingDeque
BD.addFirst(7855642);
BD.addFirst(35658786);
BD.addFirst(5278367);
// NULL
BD.addFirst(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 GFG.main(GFG.java:24)
參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#addFirst(E)
相關用法
- Java Deque addFirst()用法及代碼示例
- Java BlockingDeque add()用法及代碼示例
- Java BlockingDeque contains()用法及代碼示例
- Java BlockingDeque put()用法及代碼示例
- Java BlockingDeque take()用法及代碼示例
- Java BlockingDeque pollLast()用法及代碼示例
- Java BlockingDeque takeLast()用法及代碼示例
- Java BlockingDeque push()用法及代碼示例
- Java BlockingDeque iterator()用法及代碼示例
- Java BlockingDeque pollFirst()用法及代碼示例
- Java BlockingDeque element()用法及代碼示例
- Java BlockingDeque peek()用法及代碼示例
- Java BlockingDeque offerFirst()用法及代碼示例
- Java BlockingDeque poll()用法及代碼示例
- Java BlockingDeque offerLast()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 BlockingDeque addFirst() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。