Java中LinkedBlockingQueue poll()方法

Java中LinkedBlockingQueue poll()方法

LinkedBlockingQueue中有两种类型的poll()方法。

poll()

LinkedBlockingQueue的 poll() 方法返回队列头部的元素,并将其从队列中移除。可以说这个方法从这个LinkedBlockingQueue的头部检索和删除元素。如果队列为空,则poll方法返回null。

语法:

public E poll()

返回值: 这个方法从这个LinkedBlockingQueue的头部检索和删除元素。如果队列为空,则返回null。

以下程序说明了LinkedBlockingQueue类的poll()方法:

程序1:使用poll()方法从LinkedBlockingQueue中删除元素,其中LinkedBlockingQueue包含名称列表。

// Java程序演示LinkedBlockingQueue的poll()方法
// 
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
//   
    public static void main(String[] args)
    {
        //定义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);
//    
        //我们想从队列头中删除两个元素
        //在队列上应用poll()方法以删除元素
        String removedItem1 = linkedQueue.poll();
//    
        //打印已删除的项和队列
        System.out.println("Removed Item is " + removedItem1);
//    
        //打印删除第一个项后的队列元素
        System.out.println("Remaining Items in Queue are "
                           + linkedQueue);
//    
        //在队列上应用poll()方法以删除另一个元素
        String removedItem2 = linkedQueue.poll();
//    
        //打印已删除的项和队列
        System.out.println("Removed Item is " + removedItem2);
//    
        //打印删除第一个项后的队列元素
        System.out.println("Remaining Items in Queue are " + linkedQueue);
    }
}
Items in Queue are [Ravi, Suraj, Harsh, Sayan]

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

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

程序2:从包含员工列表的LinkedBlockingQueue中删除元素,如果队列为空,则输出null。

// Java代码演示 LinkedBlockingQueue
// 类的 poll() 方法
  
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
  
    public void pollingMethod()
    {
        // 定义 LinkedBlockingQueue 的容量
        int capacityOfQueue = 5;
  
        // 创建 LinkedBlockingQueue 对象
        LinkedBlockingQueue<Employee> linkedQueue
            = new LinkedBlockingQueue<Employee>(capacityOfQueue);
  
        // 添加元素到 LinkedBlockingQueue
        Employee emp1 = new Employee("Ravi", "测试人员", "39000");
        Employee emp2 = new Employee("Sanjeet", "经理", "98000");
  
        // 将 Employee 对象添加到 linkedQueue 中
        linkedQueue.add(emp1);
        linkedQueue.add(emp2);
  
        // 从队列中删除元素
        // 然后一遍又一遍地重复此过程,
        // 直到队列变为空为止
        while (linkedQueue.size() != 0) {
  
            // 从 LinkedBlockingQueue 中删除 Employee 项
            Employee removedEmp = linkedQueue.poll();
  
            // 打印已删除的项
            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");
        }
  
        // 此时队列大小为零
        // 接着尝试从队列中提取更多的元素
        // 从 LinkedBlockingQueue 中删除 Employee 项
        Employee removedEmp = linkedQueue.poll();
  
        // 打印已删除的项
        // 由于大小为零,因此已删除的项为空
        System.out.println("已删除的项如下:" + removedEmp);
    }
  
    // 创建一个具有名称、职位和薪资属性的 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=" + name + ", position="
                + position + ", salary=" + salary + "]";
        }
    }
  
    // 主方法
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
        gfg.pollingMethod();
    }
}
已删除的项如下:
员工姓名 - Ravi
员工职位 - 测试人员
员工薪资 - 39000

列表的大小为:1

已删除的项如下:
员工姓名 - Sanjeet
员工职位 - 经理
员工薪资 - 98000

列表的大小为:0

已删除的项如下: null

poll(long timeout, TimeUnit unit)

LinkedBlockingQueue 的 poll(long timeout, TimeUnit unit) 方法通过删除队列中的元素并返回队列中的头元素。可以说,该方法检索并删除此 LinkedBlockingQueue 的头元素。如果队列为空,则 poll() 方法将等待指定的时间,以等待元素变得可用。

语法:

public E poll(long timeout,TimeUnit unit) throws 

参数: 该方法需要两个强制参数:

  • timeout – 等待的时间长度,以 unit 为单位。
  • unit – timeout 参数的时间单位。

返回值: 该方法检索并删除此 LinkedBlockingQueue 的头元素,如果在指定的等待时间内没有元素可用,则返回 null。

异常: 如果在等待元素可用时中断方法,则此方法将抛出InterruptedException异常。

以下程序演示了LinkedBlockingQueue类的poll(long timeout,TimeUnit unit)方法:

程序1:使用poll()方法从LinkedBlockingQueue中删除元素,其中LinkedBlockingQueue包含名称列表。

//演示Java程序
//LinkedBlockingQueue的poll(long timeout, TimeUnit unit)方法
 
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
public class GFG {
 
    public static void main(String[] args)
        throws InterruptedException
    {
        //定义LinkedBlockingQueue的容量
        int capacityOfQueue = 4;
 
        //创建LinkedBlockingQueue的对象
        LinkedBlockingQueue  linkedQueue
            = new LinkedBlockingQueue(capacityOfQueue);
 
        //添加元素到LinkedBlockingQueue
        linkedQueue.add("Ravi");
        linkedQueue.add("Suraj");
        linkedQueue.add("Harsh");
 
        //打印队列中的元素
        System.out.println("Items in Queue are " + linkedQueue);
 
        //使用poll(long timeout, TimeUnit unit)方法尝试从linkedQueue中拉取元素
        System.out.println("Removing item From head: "
                           + linkedQueue.poll(5, TimeUnit.SECONDS));
 
        //打印队列详细信息
        System.out.println("Now Queue Contains" + linkedQueue);
 
        //使用poll(long timeout, TimeUnit unit)方法
        System.out.println("Removing item From head: "
                           + linkedQueue.poll(5, TimeUnit.SECONDS));
 
        //打印队列详细信息
        System.out.println("Now Queue Contains" + linkedQueue);
 
        //使用poll(long timeout, TimeUnit unit)方法
        System.out.println("Removing item From head: "
                           + linkedQueue.poll(5, TimeUnit.SECONDS));
 
        //打印队列详细信息
        System.out.println("Now Queue Contains" + linkedQueue);
 
        //使用poll(long timeout, TimeUnit unit)方法
        System.out.println("Removing item From head: "
                           + linkedQueue.poll(5, TimeUnit.SECONDS));
        //打印队列详细信息
        System.out.println("Now Queue Contains" + linkedQueue);
    }
}
Items in Queue are [Ravi, Suraj, Harsh]

Removing item From head: Ravi
Now Queue Contains[Suraj, Harsh]

Removing item From head: Suraj
Now Queue Contains[Harsh]

Removing item From head: Harsh
Now Queue Contains[]

Removing item From head: null
Now Queue Contains[]

参考:

  • https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#poll–
  • https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#poll-long-java.util.concurrent.TimeUnit-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程