栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无限循环数据库检查

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无限循环数据库检查

另一种可能性是使用

ScheduledThreadPoolExecutor

您可以实现一个

Runnable
包含您的逻辑并将其注册到
ScheduledExecutorService
,如下所示:

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);executor.scheduleAtFixedRate(myRunnable, 0, 5, TimeUnit.SECONDS);

上面的代码

ScheduledThreadPoolExecutor
在其池中创建一个具有10个线程的线程,并对其进行了
Runnable
注册,该注册将在5秒内立即开始运行。


要安排可运行时间,可以使用:

scheduleAtFixedRate

创建并执行一个周期性操作,该操作将在给定的初始延迟后首先启用,然后在给定的时间段内启用;也就是说执行将在initialDelay,initialDelay
+ period,initialDelay + 2 * period等之后开始。

scheduleWithFixedDelay

创建并执行一个周期性动作,该动作在给定的初始延迟后首先启用,然后在一个执行的终止与下一个执行的开始之间具有给定的延迟。


And
here
you can see the advantages of

ThreadPoolExecutor
, in order to see if it fits
to your requirements. I advise this question: Java Timer vs
ExecutorService? too in order to
make a good decision.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/419561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号