El método offerFirst(E e) de BlockingDeque inserta el elemento pasado en el parámetro al frente del contenedor Deque. Si la capacidad del contenedor ha excedido, entonces no devuelve una excepción como en el caso de la función add() y addFirst().
Sintaxis:
public boolean offerFirst(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 devuelve verdadero si el elemento se ha insertado, de lo contrario, devuelve falso.
Nota : El método offerFirst() de BlockingDeque se ha heredado de la clase LinkedBlockingDeque en Java.
Los siguientes programas ilustran el método offerFirst() de LinkedBlockingDeque:
Programa 1:
Java
// Java Program Demonstrate offerFirst() // 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>(4); // Add numbers to end of BlockingDeque BD.offerFirst(7855642); BD.offerFirst(35658786); BD.offerFirst(5278367); BD.offerFirst(74381793); // Cannot be inserted BD.offerFirst(10); // cannot be inserted hence returns false if (!BD.offerFirst(10)) System.out.println("The element 10 cannot be inserted" + " as capacity is full"); // before removing print queue System.out.println("Blocking Deque: " + BD); } }
The element 10 cannot be inserted as capacity is full Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Programa 2:
Java
// Java Program Demonstrate offerFirst() // 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<String> BD = new LinkedBlockingDeque<String>(4); // Add numbers to end of BlockingDeque BD.offerFirst("abc"); BD.offerFirst("gopu"); BD.offerFirst("geeks"); BD.offerFirst("richik"); // Cannot be inserted BD.offerFirst("hii"); // cannot be inserted hence returns false if (!BD.offerFirst("hii")) System.out.println("The element 'hii' cannot be" + " inserted as capacity is full"); // before removing print queue System.out.println("Blocking Deque: " + BD); } }
The element 'hii' cannot be inserted as capacity is full Blocking Deque: [richik, geeks, gopu, abc]
Referencia: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#offerFirst(E)