当前位置:网站首页>线程池执行定时任务
线程池执行定时任务
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是默认执行时间+间隔时间后执行线程。
边栏推荐
- The difference between calling by value and simulating calling by reference
- MB10M-ASEMI整流桥MB10M
- Explore Netease's large-scale automated testing solutions see here see here
- Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
- PHP二级域名session共享方案
- Nine ways to define methods in scala- Nine ways to define a method in Scala?
- Extraction of the same pointcut
- 14 topics for performance interviews between superiors and subordinates (4)
- 8 cool visual charts to quickly write the visual analysis report that the boss likes to see
- 架构实战营 - 第 6 期 毕业总结
猜你喜欢

“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画

Record a jar package conflict resolution process

Mixlab编辑团队招募队友啦~~

The difference between calling by value and simulating calling by reference

Remote file contains actual operation

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

Embedded development: seven reasons to avoid open source software

Cocos Creator 2.x 自动打包(构建 + 编译)

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

The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
随机推荐
Principles of several common IO models
How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
Uploads labs range (with source code analysis) (under update)
"Remake Apple product UI with Android" (3) - elegant statistical chart
Cocos Creator 2.x 自动打包(构建 + 编译)
Project -- high concurrency memory pool
Function introduction of JMeter thread group
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
Semi supervised learning
Detailed explanation of four modes of distributed transaction (Seata)
Unity项目优化案例一
June to - -------
First knowledge of database
QT串口ui设计和解决显示中文乱码
Custom plug-in construction and use of QT plug-in
A Fei's expectation
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
Caching mechanism of Hibernate / session level caching mechanism
Develop team OKR in the way of "crowdfunding"
“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画