当前位置:网站首页>线程池执行定时任务
线程池执行定时任务
2022-07-03 16:19:00 【小陈-程序员】
定时任务用schedule线程池决定,故用Executors类生成的的定时任务皆是SchduledExecutorService服务实现。
ScheduledExecutorService是实现了ExecutorService接口
ScheduledExecutorService里有四种方法
delay : 延迟几秒运行 TimeUnit : 时间单位。 1000毫秒 = 1秒
重载方法schedule唯一可以利用返回值callable接口的。可以scheduleFuture.get()获取返回值
运行:
初始的initialDelay 周期period。
if (延迟时间 > 运行时间)
初始时间 + 延迟时间
else {
初始时间 + 运行时间
}
运行:初始时间 + 运行时间 + 延迟时间
测试scheduleAtFixedRate() 与scheduleWithFixedDelay()
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd : hh:mm:ss");
try {
sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(simpleDateFormat.format(new Date()));
}
}, 1000,5000, TimeUnit.MILLISECONDS);

ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd : hh:mm:ss");
try {
sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(simpleDateFormat.format(new Date()));
}
}, 1000,5000, TimeUnit.MILLISECONDS);

总结:假如线程执行时间大于间隔时间,故间隔时间会变为执行时间。故得知线程以串行运行。而scheduleWithFixedDelay是默认执行时间+间隔时间后执行线程。
边栏推荐
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- Redis high availability and persistence
- Construction practice camp - graduation summary of phase 6
- Semi supervised learning
- Expression of request header in different countries and languages
- Golang 匿名函数使用
- Jmeter线程组功能介绍
- Embedded development: seven reasons to avoid open source software
- Eleven requirements for test management post
- 8 cool visual charts to quickly write the visual analysis report that the boss likes to see
猜你喜欢

ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装

“用Android复刻Apple产品UI”(3)—优雅的数据统计图表

Mongodb installation and basic operation

Détails du contrôle de la congestion TCP | 3. Espace de conception

How to use AAB to APK and APK to AAB of Google play apps on the shelves

2022年Q2加密市场投融资报告:GameFi成为投资关键词

From the 18th line to the first line, the new story of the network security industry

NSQ源码安装运行过程

How to thicken the brush in the graphical interface
![[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display](/img/46/c7f566f8fd46d383b055582d680bb7.png)
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
随机推荐
Record a jar package conflict resolution process
高等数学(第七版)同济大学 习题2-1 个人解答
面试官:JVM如何分配和回收堆外内存
One article takes you to understand machine learning
Is it safe to open an account with flush?
突破100万,剑指200万!
“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
Custom plug-in construction and use of QT plug-in
How can technology managers quickly improve leadership?
MongoDB 的安装和基本操作
[web security] - [SQL injection] - error detection injection
利用MySQL中的乐观锁和悲观锁实现分布式锁
Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
如何在本机搭建SVN服务器
Effect of ARP package on FTP dump under vxworks-6.6 system
架构实战营 - 第 6 期 毕业总结
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
Qt插件之自定义插件构建和使用
Jmeter线程组功能介绍