LinkedBlockingQueue take() Method in Java with Examples

The take() method of LinkedBlockingQueue is used to retrieve and remove the head of this queue. If the queue is empty then it will wait until an element becomes available. This method is more efficient if working on threads and using LinkedBlockingQueue in that process. So the thread that initially calls take() goes to sleep if there is no element available, letting other threads do whatever they need to do.

Syntax:

public E take() throws InterruptedException

Return Value: This method returns value at the head of this LinkedBlockingQueue. If the queue is empty then it will wait until an element becomes available.

Exception: This method throws following exceptions:

  • InterruptedException– When the interruption occurs at time of waiting for an element to become avilable if queue is empty.

Below programs illustrates take() method of LinkedBlockingQueue class:

Program 1: Remove operation on head of LinkedBlockingQueue using take().

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate take()
// 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 to LinkedBlockingQueue
        linkedQueue.add("Ravi");
        linkedQueue.add("Suraj");
        linkedQueue.add("Harsh");
        linkedQueue.add("Sayan");
  
        // print elements of queue
        System.out.println("Items in Queue are " + linkedQueue);
  
        // remove two elements from queue from head
        // Applying take() method on queue to remove element
        String removedItem1 = linkedQueue.take();
  
        // print removedItem and queue
        System.out.println("Removed Item from head is "
                           + removedItem1);
  
        // print elements of queue after removing first item
        System.out.println("Remaining Items in Queue are "
                           + linkedQueue);
  
        // Applying take() method on queue to remove another element
        String removedItem2 = linkedQueue.take();
  
        // print removedItem and queue
        System.out.println("Removed Item from head is "
                           + removedItem2);
  
        // print elements of queue after removing first item
        System.out.println("Remaining Items in Queue are "
                           + linkedQueue);
    }
}

chevron_right


Output:

Items in Queue are [Ravi, Suraj, Harsh, Sayan]
Removed Item from head is Ravi
Remaining Items in Queue are [Suraj, Harsh, Sayan]
Removed Item from head is Suraj
Remaining Items in Queue are [Harsh, Sayan]

Program 2: Remove Employee object from LinkedBlockingQueue using take().

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate take()
// method of LinkedBlockingQueue
  
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
  
    public void takeDemo() 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("Ravi", "Tester", "39000");
        Employee emp2 = new Employee("Sanjeet", "Manager", "98000");
  
        // Add Employee Objects to linkedQueue
        linkedQueue.add(emp1);
        linkedQueue.add(emp2);
  
        // remove elements from the queue
        // and follow this process again and again
        // until the queue becomes empty
        while (linkedQueue.size() != 0) {
  
            // Remove Employee item from LinkedBlockingQueue
            // using take()
            Employee removedEmp = linkedQueue.take();
  
            // print removedItem
            System.out.println("Removed Item is :");
            System.out.println("Employee Name - "
                               + removedEmp.name);
            System.out.println("Employee Position - "
                               + removedEmp.position);
            System.out.println("Employee Salary - "
                               + removedEmp.salary);
  
            // find size of linkedQueue
            int size = linkedQueue.size();
  
            // print remaining capacity value
            System.out.println("\nSize of list :" + size + "\n");
        }
    }
  
    // create an Employee Object with name,
    // position and salary as an attribute
    public class Employee {
  
        public String name;
        public String position;
        public String salary;
  
        Employee(String name, String position, String salary)
        {
            this.name = name;
            this.position = position;
            this.salary = salary;
        }
  
        @Override
        public String toString()
        {
            return "Employee [name=" + name + ", position="
                + position + ", salary=" + salary + "]";
        }
    }
  
    // Main Method
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        try {
            gfg.takeDemo();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

chevron_right


Output:

Removed Item is :
Employee Name - Ravi
Employee Position - Tester
Employee Salary - 39000

Size of list :1

Removed Item is :
Employee Name - Sanjeet
Employee Position - Manager
Employee Salary - 98000

Size of list :0


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.