Friday, February 20, 2026
HomeLanguagesJavaConcurrentLinkedQueue offer() method in Java

ConcurrentLinkedQueue offer() method in Java

The offer() method of ConcurrentLinkedQueue is used to insert the element, passed as parameter, at the tail of this ConcurrentLinkedQueue. This method returns True if insertion is successful. ConcurrentLinkedQueue is unbounded, so this method offer() will never returns false.

Syntax:

public boolean offer(E e)

Parameter: This method takes a single parameter e which represents the element we want to insert into this ConcurrentLinkedQueue.

Returns: This method returns true after successful insertion of element.

Exception: This method throws NullPointerException if the specified element is null.

Below programs illustrate offer() method of ConcurrentLinkedQueue:

Example 1: To demonstrate offer() method of ConcurrentLinkedQueue to add String.




// Java Program Demonstrate offer()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<String>
            queue = new ConcurrentLinkedQueue<String>();
  
        // Add String to queue using offer() method
        queue.offer("Aman");
        queue.offer("Amar");
        queue.offer("Sanjeet");
        queue.offer("Rabi");
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
    }
}


Output:

ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]

Example 2: To demonstrate offer() method of ConcurrentLinkedQueue for adding Numbers.




// Java Program Demonstrate offer()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer>
            queue = new ConcurrentLinkedQueue<Integer>();
  
        // Add Numbers to queue using offer
        queue.offer(4353);
        queue.offer(7824);
        queue.offer(78249);
        queue.offer(8724);
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
    }
}


Output:

ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]

Example 3: To demonstrate NullPointerException thrown by offer() method for adding Null.




// Java Program Demonstrate offer()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue<Integer>
            queue = new ConcurrentLinkedQueue<Integer>();
  
        // Add null to queue
        try {
            queue.offer(null);
        }
        catch (NullPointerException e) {
            System.out.println("Exception thrown"
                               + " while adding null: " + e);
        }
    }
}


Output:

Exception thrown while adding null: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#offer-E-

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

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS