当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LinkedBlockingQueue poll()用法及代码示例


LinkedBlockingQueue中有两种poll()方法。

poll()

LinkedBlockingQueue的poll()方法通过从队列中删除该元素来返回LinkedBlockingQueue的头部。可以说此方法从此LinkedBlockingQueue的头部检索并删除了元素。如果队列为空,则轮询方法返回null。

用法:


public E poll()

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

以下示例程序旨在说明LinkedBlockingQueue类的poll()方法:

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

// Java Program Demonstrate poll() 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // 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); 
  
        // we want to remove two elements from queue from head 
        // Applying poll() method on queue to remove element 
        String removedItem1 = linkedQueue.poll(); 
  
        // print removedItem and queue 
        System.out.println("Removed Item is " + removedItem1); 
  
        // print elements of queue after removing first item 
        System.out.println("Remaining Items in Queue are "
                           + linkedQueue); 
  
        // Applying poll() method on queue to remove another element 
        String removedItem2 = linkedQueue.poll(); 
  
        // print removedItem and queue 
        System.out.println("Removed Item is " + removedItem2); 
  
        // print elements of queue after removing first item 
        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 Program Demonstrate poll() 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
public class GFG { 
  
    public void pollingMethod() 
    { 
        // 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 
            Employee removedEmp = linkedQueue.poll(); 
  
            // 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"); 
        } 
  
        // Now size of Queue is Zero 
        // Now try to Poll more items 
        // Remove Employee item from LinkedBlockingQueue 
        Employee removedEmp = linkedQueue.poll(); 
  
        // print removedItem 
        // size is zero so removedItem is null 
        System.out.println("Removed Item is : " + removedEmp); 
    } 
  
    // 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(); 
        gfg.pollingMethod(); 
    } 
}
输出:
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

Removed Item is : null

poll(long timeout, TimeUnit unit)

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

用法:

public E poll(long timeout, TimeUnit unit) throws 

参数:此方法采用两个强制性参数:

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

返回值:此方法从此LinkedBlockingQueue的头部检索并删除元素,如果在元素可用之前经过了指定的等待时间,则为null。


异常如果在等待元素可用时方法被中断,则此方法将引发InterruptedException。

以下示例程序旨在说明LinkedBlockingQueue类的poll(长时间超时,TimeUnit单位)方法:

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

// Java program to demonstrate 
// poll(long timeout, TimeUnit unit) 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
import java.util.concurrent.TimeUnit; 
  
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"); 
  
        // print elements of queue 
        System.out.println("Items in Queue are " + linkedQueue); 
  
        // Try to poll elements from linkedQueue 
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + linkedQueue.poll(5, TimeUnit.SECONDS)); 
  
        // print queue details 
        System.out.println("Now Queue Contains" + linkedQueue); 
  
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + linkedQueue.poll(5, TimeUnit.SECONDS)); 
  
        // print queue details 
        System.out.println("Now Queue Contains" + linkedQueue); 
  
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + linkedQueue.poll(5, TimeUnit.SECONDS)); 
  
        // print queue details 
        System.out.println("Now Queue Contains" + linkedQueue); 
  
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + linkedQueue.poll(5, TimeUnit.SECONDS)); 
        // print queue details 
        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[]

参考:



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LinkedBlockingQueue poll() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。