El método putFirst(E e) de LinkedBlockingDeque 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 principio de LinkedBlockingDeque.
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
Los siguientes programas ilustran el método putFirst() de LinkedBlockingDeque:
Programa 1:
// Java Program Demonstrate putFirst() // 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.putFirst(7855642); LBD.putFirst(35658786); LBD.putFirst(5278367); LBD.putFirst(74381793); // print Dequeue System.out.println("Linked Blocking Deque: " + LBD); } }
Producción:
Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Programa 2:
// Java Program Demonstrate putFirst() // 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.putFirst(7855642); LBD.putFirst(35658786); LBD.putFirst(5278367); // throws an exception LBD.putFirst(null); // print Dequeue System.out.println("Linked Blocking Deque: " + LBD); } }
Salida :
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.putFirst(LinkedBlockingDeque.java:373) at GFG.main(GFG.java:23)
Programa 3:
// Java Program Demonstrate putFirst() // 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.putFirst(7855642); LBD.putFirst(35658786); LBD.putFirst(5278367); // throws an exception LBD.putFirst(4356789); // print Dequeue System.out.println("Linked Blocking Deque: " + LBD); } }
Salida :
Runtime Errors: Max real time limit exceeded due to either by heavy load on server or by using sleep function
Referencia: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html#putFirst-E-