javaqueue清空_JavaDelayQueueclear()⽤法及代码⽰例Java中的DelayQueue的clear()⽅法⽤于删除当前DelayQueue对象中的所有元素。返回此调⽤后,队列将为空。延迟中没有过期的队列中的元素会⾃动从队列中丢弃。
句法:我的幸福作文
public void clear()七校联合办学
参数:此⽅法不带任何参数。
返回值:此⽅法不返回任何值。
以下⽰例程序旨在说明上述⽅法:
// Java program to illustrate the clear() method
// of DelayQueue class
import urrent.DelayQueue;
import urrent.Delayed;
import urrent.TimeUnit;
public class GFG {
public static void main(String args[])
{
// Create a DelayQueue instance
DelayQueue queue = new DelayQueue();
// Create an Object of type Delayed
Delayed obj = new Delayed() {
public long getDelay(TimeUnit unit)
广西习俗{
有气质带静的网名
return 24; // some value is returned
}
public int compareTo(Delayed o)
{
if (o.getDelay(TimeUnit.DAYS) >结肠功能紊乱
return 1;
el if (o.getDelay(TimeUnit.DAYS) ==
return 0;
return -1;
}两个月宝宝吐奶
};
// Add the obj to the queue
queue.add(obj);
// Check if queue is empty
谭晶
System.out.println("Is queue empty() : "
+ queue.isEmpty());
// Remove all elements from queue
// uing clear() method
queue.clear();
// Check if queue is empty
System.out.println("Is queue empty after calling clear() : " + queue.isEmpty());
}
}
输出:
Is queue empty() : fal
幼儿园教具Is queue empty after calling clear() : true