java take,Java DelayQueue take()用法及代码示例
DelayQueue的take()方法用于检索DelayQueue的头部,也将其删除。因此,减小了DelayQueue的大小。如果此队列上有延迟过期的元素可用,则此函数等待。
用法:
public E take ()
参数:此方法不接受任何参数。
返回值:该函数返回DelayQueue的头部。
异常:如果在等待期间被中断,则此方法将引发InterruptedException。
以下示例程序旨在说明DelayQueue.take()方法:
程序:
// Java Program Demonstrate DelayQueue take() method
import java.util.concurrent.*;
import java.util.*;
// The DelayObject for DelayQueue
// It must implement Delayed and
// its getDelay() and compareTo() method
class DelayObject implements Delayed {
private String name;
private long time;
// Contructor of DelayObject
public DelayObject(String name, long delayTime)
{
this.name = name;
this.time = System.currentTimeMillis()
+ delayTime;
}
// Implementing getDelay() method of Delayed
@Override
public long getDelay(TimeUnit unit)
{
long diff = time - System.currentTimeMillis();
return unit.convert(diff, TimeUnit.MILLISECONDS);
}
// Implementing compareTo() method of Delayed
@Override
public int compareTo(Delayed obj)
{
if (this.time < ((DelayObject)obj).time) {
return -1;
}
if (this.time > ((DelayObject)obj).time) {
return 1;
}
return 0;
}
// Implementing toString() method of Delayed
@Override
public String toString()
{
return "\n{"
+ " " + name + ", time=" + time + "}";
}
}
// Driver Class
public class GFG {
public static void main(String[] args) throws InterruptedException
{
// create object of DelayQueue
// using DelayQueue() constructor
BlockingQueue DQ
= new DelayQueue();
// Add numbers to end of DelayQueue
// using add() method
DQ.add(new DelayObject("A", 1));
DQ.add(new DelayObject("B", 2));
DQ.add(new DelayObject("C", 3));
DQ.add(new DelayObject("D", 4));
// print the size of DelayQueue
System.out.println("Size of the DelayQueue: "
+ DQ.size());
// print queue
System.out.println("DelayQueue: "
+ DQ);
// take() function will retrieve and remove the head of DelayQueue
System.out.println("Head of the DelayQueue:"
+ DQ.take());
// print the size of DelayQueue
System.out.println("Size of the DelayQueue: "
+ DQ.size());
// print queue
System.out.println("DelayQueue: "
+ DQ);
}
}
输出:
Size of the DelayQueue: 4
DelayQueue: [
{ A, time=1546842594283},
{ B, time=1546842594284},
{ C, time=1546842594285},
{ D, time=1546842594286}]
Head of the DelayQueue:
{ A, time=1546842594283}
Size of the DelayQueue: 3
DelayQueue: [
{ B, time=1546842594284},
{ D, time=1546842594286},
{ C, time=1546842594285}]
java take,Java DelayQueue take()用法及代码示例相关推荐
- java queue iterator_Java DelayQueue iterator()用法及代码示例
DelayQueue的iterator()方法用于在DelayQueue中的所有元素上返回迭代器.这些元素可以过期或未过期. 用法: public Iterator iterator () 参数:此方 ...
- java foreach delete_Java CopyOnWriteArrayList forEach()用法及代码示例
CopyOnWriteArrayList的forEach()方法为Iterable的每个元素执行给定的操作,直到已处理完所有元素或该操作引发异常. 用法: public void forEach (C ...
- java 清空scanner_Java Scanner reset()用法及代码示例
java.util.Scanner类的reset()方法将重置此扫描器.重置扫描器时,它会丢弃所有可能由useDelimiter(java.util.regex.Pattern),useLocale( ...
- java is start_PHP IntlChar::isJavaIDStart()用法及代码示例
IntlChar::isJavaIDStart()函数是PHP中的一个内置函数,用于检查输入字符代码点是否允许,因为第一个字符是Java标识符.对于具有常规类别"Sc"(货币符号) ...
- java map sortedmap_Java SortedMap put()用法及代码示例
Java中SortedMap接口的put()方法用于将指定值与该映射中的指定键相关联. 用法: V put(K key, V value) 参数:此方法有两个参数: key:这是左参数, value: ...
- java创建datetime_Java OffsetDateTime ofInstant()用法及代码示例
Java中OffsetDateTime类的ofInstant(Instant Instant,ZoneId zone)方法用于根据指定的Instant和ZoneID创建OffsetDateTime的实 ...
- java temporalfield_Java Year isSupported(TemporalField)用法及代码示例
Year类的isSupported(TemporalField)方法用于检查Year类是否支持指定的字段,这意味着使用此方法,我们可以检查是否可以为指定的字段查询Year对象. ChronoField ...
- java generate()_Java IntStream generate()用法及代码示例
IntStream generate(IntSupplier s)返回一个无限顺序无序流,其中每个元素由提供的IntSupplier(提供int值的结果)生成.这适用于生成恒定流,随机元素流等. 用法 ...
- java printwrite_Java PrintWriter write(int)用法及代码示例
Java中的PrintWriter类的write(int)方法用于在流上写入指定的字符.使用以整数值传递的字符的ASCII值指定此字符.该整数值用作参数. 用法: public void write( ...
- Java 中removelinked_Java LinkedHashSet remove()用法及代码示例
Java.util.LinkedHashSet.remove(Object O)方法用于从LinkedHashSet中删除特定元素. 用法: LinkedHashSet.remove(Object O ...
最新文章
- Facebook数字货币凸显硅谷雄心:打造全球金融操作系统
- PHP环境下配置WebGrind——让你的网站性能看得见
- python算程序员吗_我算是优秀的程序员吗?
- leetcode746. 使用最小花费爬楼梯
- TW实习日记:第16天
- java 图形应用有必要学吗_儿童英语口语怎么学?有必要报班吗?
- 在vc++里面进行图像处理的时候应该把图形放哪_图形找朋友小班教案
- 树莓派上FFMPEG/VLC播放海康网络摄像仪视频
- 软件工程理论、方法与实践(第三章)
- java 本地文件路径怎么写_java本地文件路径怎么写
- docker安装gamit_科学网—ubuntu下安装gamit 安装 - 贺小星的博文
- 关于域名抢注:过期高PR域名抢注价值高吗?
- JavaScript 根据身份证号获取年龄
- 热门表情包+头像+壁纸自动采集多分类微信小程序源码下载支持多种流量主模式
- 记录家里路由器、光猫出问题的解决方案
- 戴尔笔记本,快捷键无法调节屏幕亮度,有图标显示,就是没有调节效果
- hp台式计算机硬盘如何取出,惠普台式机不读硬盘怎么办
- 清理电脑C盘全攻略 - 是时候给你的系统盘放个假了!
- 基于布谷鸟算法优化的Elman神经网络数据预测-附代码
- html右键菜单背景图片,右键菜单背景图片添加步骤