LinkedBlockingDeque的put(E e)方法將指定的元素插入到此雙端隊列表示的隊列中(換句話說,在此雙端隊列的末尾)。如果Deque受容量限製,則它將等待空間可用。
用法:
public void put(E e)
參數:此方法接受強製參數e,該參數是要在LinkedBlockingDeque末尾插入的元素。
返回值:此方法不返回任何內容。
異常:該程序將引發兩個異常,如下所示:
- NullPointerException –如果指定的元素為null
- InterruptedException–如果在等待時被打斷
以下示例程序旨在說明LinkedBlockingDeque的put()方法:
示例1:
// Java Program Demonstrate put()
// 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.put(7855642);
LBD.put(35658786);
LBD.put(5278367);
LBD.put(74381793);
// print Dequeue
System.out.println("Linked Blocking Deque: " + LBD);
}
}
輸出:
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
示例2:
// Java Program Demonstrate put()
// 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.put(7855642);
LBD.put(35658786);
LBD.put(5278367);
// throws an exception
LBD.put(null);
// print Dequeue
System.out.println("Linked Blocking Deque: " + LBD);
}
}
輸出:
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:22)
示例3:
// Java Program Demonstrate put()
// 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.put(7855642);
LBD.put(35658786);
LBD.put(5278367);
// throws an exception
LBD.put(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#put-E-
相關用法
- Java LinkedBlockingDeque take()用法及代碼示例
- Java LinkedBlockingDeque pop()用法及代碼示例
- Java LinkedBlockingDeque contains()用法及代碼示例
- Java LinkedBlockingDeque add()用法及代碼示例
- Java LinkedBlockingDeque peekFirst()用法及代碼示例
- Java LinkedBlockingDeque addFirst()用法及代碼示例
- Java LinkedBlockingDeque element()用法及代碼示例
- Java LinkedBlockingDeque putLast()用法及代碼示例
- Java LinkedBlockingDeque remainingCapacity()用法及代碼示例
- Java LinkedBlockingDeque removeLast()用法及代碼示例
- Java LinkedBlockingDeque putFirst()用法及代碼示例
- Java LinkedBlockingDeque removeFirst()用法及代碼示例
- Java LinkedBlockingDeque getFirst()用法及代碼示例
- Java LinkedBlockingDeque toString()用法及代碼示例
- Java LinkedBlockingDeque spliterator()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LinkedBlockingDeque put() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。