当前位置:网站首页>线程池执行定时任务
线程池执行定时任务
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是默认执行时间+间隔时间后执行线程。
边栏推荐
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- Remote file contains actual operation
- 【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事
- Function introduction of JMeter thread group
- 架构实战营 - 第 6 期 毕业总结
- The mixlab editing team is recruiting teammates~~
- Construction practice camp - graduation summary of phase 6
- Hibernate的缓存机制/会话级缓存机制
- Detailed explanation of four modes of distributed transaction (Seata)
- [system safety] 43 PowerShell malicious code detection series (5) automatic extraction of ten thousand words from abstract syntax tree
猜你喜欢
![App mobile terminal test [4] APK operation](/img/f1/4bff6e66b77d0f867bf7237019e982.png)
App mobile terminal test [4] APK operation

Low level version of drawing interface (explain each step in detail)

Multithread 02 thread join

【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事

Please be prepared to lose your job at any time within 3 years?

工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!

Embedded development: seven reasons to avoid open source software

Jmeter线程组功能介绍

Initial test of scikit learn Library

8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
随机推荐
Go language self-study series | golang switch statement
Construction practice camp - graduation summary of phase 6
Leetcode binary search tree
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
架构实战营 - 第 6 期 毕业总结
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Embedded development: seven reasons to avoid open source software
Go language self-study series | if else if statement in golang
Is it safe to open an account with tongdaxin?
App mobile terminal test [4] APK operation
Client does not support authentication protocol requested by server; consider upgrading MySQL client
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
Record windows10 installation tensorflow-gpu2.4.0
Multithread 02 thread join
Chinese translation of Tagore's floating birds (1~10)
Pointcut expression
Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths