當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BlockingDeque putLast()用法及代碼示例


BlockingDeque的putLast(E e)方法將指定元素插入到此雙端隊列表示的隊列中(換句話說,在此雙端隊列的末尾)。如果Deque受容量限製,則它將等待空間可用。

用法:

public void putLast(E e)

參數:此方法接受強製參數e,該參數是要在BlockingDeque末尾插入的元素。


返回:此方法不返回任何內容。

異常:該程序將引發兩個異常,如下所示:

  • NullPointerException -如果指定的元素為null
  • InterruptedException-如果在等待時被打斷

注意:: BlockingDeque的putLast()方法已從Java中的LinkedBlockingDeque類繼承。

以下示例程序旨在說明BlockingDeque的putLast()方法:

程序1:

// Java Program Demonstrate putLast() 
// 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.putLast(7855642); 
        BD.putLast(35658786); 
        BD.putLast(5278367); 
        BD.putLast(74381793); 
  
        // print Dequeue 
        System.out.println("Blocking Deque:" + BD); 
    } 
}
輸出:
Blocking Deque:[7855642, 35658786, 5278367, 74381793]

程序2:

// Java Program Demonstrate putLast() 
// 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.putLast(7855642); 
        BD.putLast(35658786); 
        BD.putLast(5278367); 
  
        // throws an exception 
        BD.putLast(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 GFG.main(GFG.java:24)

程序3:

// Java Program Demonstrate putLast() 
// 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.putLast(7855642); 
        BD.putLast(35658786); 
        BD.putLast(5278367); 
  
        // throws an exception 
        BD.putLast(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#putLast(E)



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 BlockingDeque putLast() function in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。