📌  相关文章
📜  Java中的 LinkedBlockingQueue take() 方法及示例

📅  最后修改于: 2022-05-13 01:54:55.453000             🧑  作者: Mango

Java中的 LinkedBlockingQueue take() 方法及示例

LinkedBlockingQueuetake()方法用于检索和移除此队列的头部。如果队列为空,则它将等待直到元素可用。如果在线程上工作并在该进程中使用 LinkedBlockingQueue,则此方法效率更高。因此,如果没有可用的元素,最初调用 take() 的线程将进入睡眠状态,让其他线程做他们需要做的任何事情。
句法:

public E take() throws InterruptedException

返回值:此方法返回此 LinkedBlockingQueue 头部的值。如果队列为空,则它将等待直到元素可用。
异常:此方法抛出以下异常:

  • InterruptedException – 如果队列为空,则在等待元素变为可用时发生中断。

下面的程序说明了 LinkedBlockingQueue 类的 take() 方法:
程序 1:使用 take() 删除 LinkedBlockingQueue 头部的操作。

Java
// 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 linkedQueue
            = new LinkedBlockingQueue(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);
    }
}


Java
// 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 linkedQueue
            = new LinkedBlockingQueue(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();
        }
    }
}


输出:
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]

程序 2:使用 take() 从 LinkedBlockingQueue 中删除 Employee 对象。

Java

// 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 linkedQueue
            = new LinkedBlockingQueue(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();
        }
    }
}
输出:
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