LinkedBlockingQueue put() method in Java with Examples

The put(E e) method of LinkedBlockingQueue inserts element passed as parameter to method at the tail of this LinkedBlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to LinkedBlockingQueue.

Syntax:

public void put(E e) throws InterruptedException

Parameter: This method takes a mandatory parameter e which is the element to be inserted in LinkedBlockingQueue.

Return Value: The method does not return anything.

Exception: This method throws following exceptions:

  • InterruptedException– when interruption occurred at time of waiting for queue to become available
  • NullPointerException– if the element passed to method is null

Below programs illustrates put(E e) method of LinkedBlockingQueue class:

Program 1: Prints Element of LinkedBlockingQueue after adding new names to queue using put() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate put(E e)
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<String> linkedQueue
            = new LinkedBlockingQueue<String>(capacityOfQueue);
  
        // Add element usin put() method
        linkedQueue.put("Karan");
        linkedQueue.put("Suraj");
        linkedQueue.put("Harsh");
        linkedQueue.put("Rahul");
  
        // print elements of queue
        System.out.println("Items in Queue are " + linkedQueue);
    }
}

chevron_right


Output:

Items in Queue are [Karan, Suraj, Harsh, Rahul]

Program 2: Add Employee object using put method in LinkedBlockingQueue

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate put()
// method of LinkedBlockingQueue
  
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
  
    public void PutDemo() throws InterruptedException
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 5;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<Employee> linkedQueue
            = new LinkedBlockingQueue<Employee>(capacityOfQueue);
  
        // Add element to LinkedBlockingQueue
        Employee emp1 = new Employee("Ranjeet", "Tester", "29000", 27);
        Employee emp2 = new Employee("Sanjeet", "Manager", "98000", 34);
        Employee emp3 = new Employee("Karan", "Analyst", "44000", 30);
  
        // Add Employee Objects to linkedQueue
        // Using put(E e)
        linkedQueue.put(emp1);
        linkedQueue.put(emp2);
        linkedQueue.put(emp3);
  
        System.out.println("Details of Employees:");
        // print details of linkedQueue
        Iterator itr = linkedQueue.iterator();
        while (itr.hasNext())
            System.out.println(itr.next());
    }
  
    // create an Employee Object with name,
    // position, salary and age as attributes
    public class Employee {
  
        public String name;
        public String position;
        public String salary;
        public int Age;
        Employee(String name, String position,
                 String salary, int age)
        {
            this.name = name;
            this.position = position;
            this.salary = salary;
            this.Age = age;
        }
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", position="
                + position + ", salary=" + salary
                + ", Age=" + Age + "]";
        }
    }
  
    // Main Method
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        try {
            gfg.PutDemo();
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

chevron_right


Output:

Details of Employees:
Employee [name=Ranjeet, position=Tester, salary=29000, Age=27]
Employee [name=Sanjeet, position=Manager, salary=98000, Age=34]
Employee [name=Karan, position=Analyst, salary=44000, Age=30]

Program 3: To show NullPointerException thrown by put() method

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate put(E e)
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
  
public class GFG {
  
    public static void main(String[] args)
        throws InterruptedException
    {
        // define capacity of LinkedBlockingQueue
        int capacityOfQueue = 4;
  
        // create object of LinkedBlockingQueue
        LinkedBlockingQueue<String> linkedQueue
            = new LinkedBlockingQueue<String>(capacityOfQueue);
  
        // try to put null value in put method
        try {
            linkedQueue.put(null);
        }
        catch (Exception e) {
            // print error details
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

Exception: java.lang.NullPointerException

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.