Java 中 BlockingDeque putLast()函数示例
原文:https://www . geeksforgeeks . org/blockingrequest-putlast-function-in-Java-with-examples/
阻塞请求的 putLast(E e) 方法将指定的元素插入到这个队列所代表的队列中(换句话说,在这个队列的尾部/末端)。如果德奎是容量受限的,那么它将等待空间变得可用。
语法:
public void putLast(E e)
参数:该方法接受一个强制参数 e ,它是要插入到阻塞请求末尾的元素。
返回:此方法不返回任何内容。
异常:程序抛出如下所示的两个异常:
- null pointerexception -If the specified element is empty
- Interrupt exception –If you interrupt while waiting
注:blocking eque的 putLast()方法继承自 Java 中的link edblocking eque类。
下面的程序说明了 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
版权属于:月萌API www.moonapi.com,转载请注明出处