El método putFirst(E e) de BlockingDeque inserta el elemento especificado al principio de la cola representada por este deque. Si el Deque tiene una capacidad restringida, entonces esperará a que el espacio esté disponible.
Sintaxis:
public void putFirst(E e)
Parámetros: este método acepta un parámetro obligatorio e que es el elemento que se insertará al frente del BlockingDeque.
Devoluciones: este método no devuelve nada.
Excepciones: el programa lanza dos excepciones como se muestra a continuación:
- NullPointerException : si el elemento especificado es nulo
- InterruptedException : si se interrumpe mientras espera
Nota : El método putFirst() de BlockingDeque se ha heredado de la clase LinkedBlockingDeque en Java.
Los siguientes programas ilustran el método putFirst() de BlockingDeque:
Programa 1:
// Java Program Demonstrate putFirst() // 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.putFirst(7855642); BD.putFirst(35658786); BD.putFirst(5278367); BD.putFirst(74381793); // print Deque System.out.println("Blocking Deque: " + BD); } }
Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Programa 2:
// Java Program Demonstrate putFirst() // 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.putFirst(7855642); BD.putFirst(35658786); BD.putFirst(5278367); // throws an exception BD.putFirst(null); // print Dequeue System.out.println("Blocking Deque: " + BD); } }
Salida :
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.putFirst(LinkedBlockingDeque.java:373) at GFG.main(GFG.java:24)
Programa 3:
// Java Program Demonstrate putFirst() // 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.putFirst(7855642); BD.putFirst(35658786); BD.putFirst(5278367); // throws an exception BD.putFirst(4356789); // print Dequeue System.out.println("Blocking Deque: " + BD); } }
Salida :
Max real time limit exceeded due to either by heavy load on server or by using sleep function
Referencia: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#putFirst(E)