Thursday, April 2, 2026
HomeLanguagesJavaLinkedBlockingDeque offer() method in Java

LinkedBlockingDeque offer() method in Java

The offer(E e) method of LinkedBlockingDeque inserts the element passed in the parameter to the end of the Deque. If the container’s capacity has exceeded, then it does not returns an exception as in case of add() and addFirst() function.

Syntax:

public boolean offer(E e)

Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the end of the LinkedBlockingDeque.

Returns: This method returns true if the element has been inserted, else it returns false.

Below programs illustrate offer() method of LinkedBlockingDeque:

Program 1:




// Java Program Demonstrate offer()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>(4);
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.offer(7855642);
        LBD.offer(35658786);
        LBD.offer(5278367);
        LBD.offer(74381793);
  
        // Cannot be inserted
        LBD.offer(10);
  
        // cannot be inserted hence returns false
        if (!LBD.offer(10))
            System.out.println("The element 10 cannot be inserted"+
                                           " as capacity is full");
  
        // before removing print queue
        System.out.println("Linked Blocking Deque: " + LBD);
    }
}


Output:

The element 10 cannot be inserted as capacity is full
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]

Program 2:




// Java Program Demonstrate offer()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<String> LBD
            = new LinkedBlockingDeque<String>(4);
  
        // Add numbers to end of LinkedBlockingDeque
        LBD.offer("abc");
        LBD.offer("gopu");
        LBD.offer("geeks");
        LBD.offer("richik");
  
        // Cannot be inserted
        LBD.offer("hii");
  
        // cannot be inserted hence returns false
        if (!LBD.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: " + LBD);
    }
}


Output:

The element 'hii' cannot be inserted as capacity is full
Linked Blocking Deque: [abc, gopu, geeks, richik]

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#offer(E)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32512 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6885 POSTS0 COMMENTS
Nicole Veronica
12006 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12100 POSTS0 COMMENTS
Shaida Kate Naidoo
7015 POSTS0 COMMENTS
Ted Musemwa
7259 POSTS0 COMMENTS
Thapelo Manthata
6971 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS