BlockingQueue接口的poll(long timeout,TimeUnit unit)方法通过从队列中删除该元素来返回BlockingQueue的头部。可以说此方法从此LinkedBlockingQueue的头部检索并删除了元素。如果队列为空,则poll()方法将等待直到指定时间元素可用。
用法:
public E poll(long timeout, TimeUnit unit) throws
参数:此方法采用两个强制性参数:
- timeout–等待的时间,以单位为单位。
- unit–超时参数的TimeUnit。
返回值:此方法从此LinkedBlockingQueue的头部检索并删除元素,如果在元素可用之前经过了指定的等待时间,则为null。
异常如果在等待元素可用时方法被中断,则此方法将引发InterruptedException。
注意:BlockingQueue的poll()方法已从Java中的Queue类继承。
以下示例程序旨在说明BlockingQueue的poll(长时间超时,TimeUnit单位)方法:
程序1:
// Java program to demonstrate
// poll(long timeout, TimeUnit unit)
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of BlockingQueue
int capacityOfQueue = 4;
// create object of BlockingQueue
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to BlockingQueue
BQ.add("Ravi");
BQ.add("Suraj");
BQ.add("Harsh");
// print elements of queue
System.out.println("Items in Queue are " + BQ);
// Try to poll elements from BQ
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ BQ.poll(5, TimeUnit.SECONDS));
// print queue details
System.out.println("Now Queue Contains" + BQ);
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ BQ.poll(5, TimeUnit.SECONDS));
// print queue details
System.out.println("Now Queue Contains" + BQ);
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ BQ.poll(5, TimeUnit.SECONDS));
// print queue details
System.out.println("Now Queue Contains" + BQ);
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ BQ.poll(5, TimeUnit.SECONDS));
// print queue details
System.out.println("Now Queue Contains" + BQ);
}
}
输出:
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[]
程序2:
// Java program to demonstrate
// poll(long timeout, TimeUnit unit)
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of BlockingQueue
int capacityOfQueue = 2;
// create object of BlockingQueue
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to BlockingQueue
BQ.add("Gopal");
BQ.add("GFG");
// print elements of queue
System.out.println("Items in Queue are " + BQ);
// Try to poll elements from BQ
// using poll(long timeout, TimeUnit unit) method
System.out.println("Removing item From head: "
+ BQ.poll(2, TimeUnit.SECONDS));
// print queue details
System.out.println("Now Queue Contains" + BQ);
}
}
输出:
Items in Queue are [Gopal, GFG] Removing item From head: Gopal Now Queue Contains[GFG]
相关用法
- Java BlockingQueue take()用法及代码示例
- Java BlockingQueue contains()用法及代码示例
- Java BlockingQueue put()用法及代码示例
- Java BlockingQueue offer()用法及代码示例
- Java BlockingQueue remove()用法及代码示例
- Java BlockingQueue remainingCapacity()用法及代码示例
- Java BlockingQueue drainTo()用法及代码示例
- Java DelayQueue poll()用法及代码示例
- Java BlockingDeque poll()用法及代码示例
- Java BlockingQueue add()用法及代码示例
- Java ConcurrentLinkedDeque poll()用法及代码示例
- Java PriorityQueue poll()用法及代码示例
- Java Queue poll()用法及代码示例
- Java ConcurrentLinkedQueue poll()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 BlockingQueue poll() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。