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

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

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

public E take() throws InterruptedException

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

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

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

// Java 程序演示 LinkedBlockingQueue take()
// 方法
 
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        // 定义 LinkedBlockingQueue 的容量
        int capacityOfQueue = 4;
 
        // 创建 LinkedBlockingQueue 的对象
        LinkedBlockingQueue<String> linkedQueue
            = new LinkedBlockingQueue<String>(capacityOfQueue);
 
        // 将元素添加到 LinkedBlockingQueue
        linkedQueue.add("Ravi");
        linkedQueue.add("Suraj");
        linkedQueue.add("Harsh");
        linkedQueue.add("Sayan");
 
        // 打印队列中的元素
        System.out.println("Items in Queue are " + linkedQueue);
 
        // 从队列的头部移除两个元素
        // 在队列上应用 take() 方法以移除元素
        String removedItem1 = linkedQueue.take();
 
        // 打印已移除的项和队列
        System.out.println("Removed Item from head is "
                           + removedItem1);
 
        // 打印删除第一个项后的队列中的元素
        System.out.println("Remaining Items in Queue are "
                           + linkedQueue);
 
        // 在队列上应用 take() 方法以移除另一个元素
        String removedItem2 = linkedQueue.take();
 
        // 打印已移除的项和队列
        System.out.println("Removed Item from head is "
                           + removedItem2);
 
        // 打印删除第一个项后的队列中的元素
        System.out.println("Remaining Items in Queue are "
                           + linkedQueue);
    }
}

输出:

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程序演示LinkedBlockingQueue的take()方法
 
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
 
    public void takeDemo() throws InterruptedException
    {
        // 定义LinkedBlockingQueue的容量
        int capacityOfQueue = 5;
 
        // 创建LinkedBlockingQueue对象
        LinkedBlockingQueue<Employee> linkedQueue
            = new LinkedBlockingQueue<Employee>(capacityOfQueue);
 
        // 向LinkedListBlockingQueue添加元素
        Employee emp1 = new Employee("Ravi", "测试员", "39000");
        Employee emp2 = new Employee("Sanjeet", "经理", "98000");
 
        // 向linkedQueue添加Employee对象
        linkedQueue.add(emp1);
        linkedQueue.add(emp2);
 
        // 从队列中移除元素
        // 再次重复此过程,直到队列变为空为止
        while (linkedQueue.size() != 0) {
 
            // 使用take()方法从LinkedBlockingQueue队列中移除Employee项
            Employee removedEmp = linkedQueue.take();
 
            // 打印移除项
            System.out.println("移除的项为:");
            System.out.println("雇员姓名 - " + removedEmp.name);
            System.out.println("雇员职位 - " + removedEmp.position);
            System.out.println("雇员薪资 - " + removedEmp.salary);
 
            // 找出linkedQueue的大小
            int size = linkedQueue.size();
 
            // 打印剩余容量值
            System.out.println("\n列表大小:" + size + "\n");
        }
    }
 
    // 创建一个Employee对象,姓名、职位和工资作为属性
    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 + ", 职位="
                + position + ", 薪资=" + salary + "]";
        }
    }
 
    // 主方法
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        try {
            gfg.takeDemo();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出:

移除的项为:
雇员姓名 - Ravi
雇员职位 - 测试员
雇员薪资 - 39000

列表大小:1

移除的项为:
雇员姓名 - Sanjeet
雇员职位 - 经理
雇员薪资 - 98000

列表大小:0

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程