A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/arrayblockingqueue-put-method-in-java/ below:

ArrayBlockingQueue put() method in Java

ArrayBlockingQueue put() method in Java

Last Updated : 11 Jul, 2025

ArrayBlockingQueue

is bounded, blocking queue that stores the elements internally backed by an array.

The

put (E e)

method inserts element passed as parameter to method at the tail of this queue(ArrayBlockingQueue) if queue is not full. If

the queue is full then it will wait for space to become available. Syntax:
public void put(E e)  throws InterruptedException
Parameter:

e - the element to add in queue.

Throws

InterruptedException - if interrupted while waiting. NullPointerException - if the specified element is null. Below programs illustrate put(E e) method of ArrayBlockingQueue.

Example 1 Java
// Java Program to demonstrate put(E e)
// method of ArrayBlockingQueue.

import java.util.concurrent.ArrayBlockingQueue;

public class GFG {

    public static void main(String[] args) throws InterruptedException
    {
        // define capacity of ArrayBlockingQueue
        int capacity = 5;

        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);

        // Add elements to ArrayBlockingQueue using put method
        queue.put(223);
        queue.put(546);
        queue.put(986);
        queue.put(357);
        queue.put(964);

        // print Queue
        System.out.println("queue contains " + queue);
    }
}
Output :
queue contains [223, 546, 986, 357, 964]
Example 2 Java
// Java Program to demonstrate put(E e)
// method of ArrayBlockingQueue

import java.util.concurrent.ArrayBlockingQueue;

public class GFG {

    public static void main(String[] args) throws InterruptedException
    {
        // define capacity of ArrayBlockingQueue
        int capacity = 5;

        // create object of ArrayBlockingQueue
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(capacity);

        // Add elements to ArrayBlockingQueue using put method
        queue.put("StarWars");
        queue.put("SuperMan");
        queue.put("Flash");
        queue.put("BatMan");
        queue.put("Avengers");

        // print Queue
        System.out.println("queue contains " + queue);

        // remove some elements
        queue.remove();
        queue.remove();

        // Add elements to ArrayBlockingQueue using put method
        queue.put("CaptainAmerica");
        queue.put("Thor");

        System.out.println("queue contains " + queue);
    }
}
Output :
queue contains [StarWars, SuperMan, Flash, BatMan, Avengers]
queue contains [Flash, BatMan, Avengers, CaptainAmerica, Thor]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#put(E)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4