El método addFirst(E e) de BlockingDeque inserta el elemento pasado en el parámetro al frente de Deque si hay espacio. Si BlockingDeque tiene una capacidad restringida y no queda espacio para la inserción, devuelve una IllegalStateException .
Sintaxis:
public void addFirst(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.
Excepción:
- IllegalStateException : si el elemento no se puede agregar en este momento debido a restricciones de capacidad
- NullPointerException : si el elemento especificado es nulo
Nota : El método addFirst() de BlockingDeque se ha heredado de la clase LinkedBlockingDeque en Java.
Los siguientes programas ilustran el método addFirst() de BlockingQueue:
Programa 1:
// Java Program Demonstrate addFirst() // 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 IllegalStateException { // create object of BlockingDeque BlockingDeque<Integer> BD = new LinkedBlockingDeque<Integer>(); // Add numbers to front of BlockingDeque BD.addFirst(7855642); BD.addFirst(35658786); BD.addFirst(5278367); BD.addFirst(74381793); // before removing print Dequq System.out.println("Blocking Deque: " + BD); } }
Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Programa 2:
// Java Program Demonstrate addFirst() // method of BlockingDeque // when it is Full import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.BlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of BlockingDeque // size of list BlockingDeque<Integer> BD = new LinkedBlockingDeque<Integer>(3); // Add numbers to front of BlockingDeque BD.addFirst(7855642); BD.addFirst(35658786); BD.addFirst(5278367); // it is full BD.addFirst(74381793); // before removing print Deque System.out.println("Blocking Deque: " + BD); } }
Producción:
Exception in thread "main" java.lang.IllegalStateException: Deque full at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:326) at GFG.main(GFG.java:24)
Programa 3:
// Java Program Demonstrate addFirst() // method of BlockingDeque // when nill is inserted import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of BlockingDeque BlockingDeque<Integer> BD = new LinkedBlockingDeque<Integer>(); // Add numbers to front of BlockingDeque BD.addFirst(7855642); BD.addFirst(35658786); BD.addFirst(5278367); // NULL BD.addFirst(null); // before removing print Deque System.out.println("Blocking Deque: " + BD); } }
Producción:
Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.offerFirst(LinkedBlockingDeque.java:342) at java.util.concurrent.LinkedBlockingDeque.addFirst(LinkedBlockingDeque.java:325) at GFG.main(GFG.java:24)
Referencia: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#addFirst(E)