El método de oferta (E e) de BlockingDeque inserta el elemento pasado en el parámetro al final de 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 offer(E e)
Parámetros: Este método acepta un parámetro obligatorio e que es el elemento a insertar al final del BlockingDeque.
Devoluciones: este método devuelve verdadero si el elemento se ha insertado, de lo contrario, devuelve falso.
Nota : El método de oferta() de BlockingDeque se ha heredado de la clase LinkedBlockingDeque en Java.
Los siguientes programas ilustran el método de oferta() de BlockingDeque:
Programa 1:
// Java Program Demonstrate offer() // 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.offer(7855642); BD.offer(35658786); BD.offer(5278367); BD.offer(74381793); // Cannot be inserted BD.offer(10); // cannot be inserted hence returns false if (!BD.offer(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: [7855642, 35658786, 5278367, 74381793]
Programa 2:
// Java Program Demonstrate offer() // 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.offer("abc"); BD.offer("gopu"); BD.offer("geeks"); BD.offer("richik"); // Cannot be inserted BD.offer("hii"); // cannot be inserted hence returns false if (!BD.offer("hii")) System.out.println("The element 'hii' cannot be inserted" + " as capacity is full"); // before removing print queue System.out.println("Linked Blocking Deque: " + BD); } }
The element 'hii' cannot be inserted as capacity is full Blocking Deque: [abc, gopu, geeks, richik]
Referencia: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#offer(E)