当前位置:网站首页>Thread pool executes scheduled tasks
Thread pool executes scheduled tasks
2022-07-03 16:29:00 【Xiao Chen - Programmer】
For scheduled tasks schedule Thread pool decision , So use Executors The scheduled tasks generated by the class are SchduledExecutorService Service implementation .
ScheduledExecutorService Is to implement the ExecutorService Interface
ScheduledExecutorService There are four ways
delay : Delay running for a few seconds TimeUnit : Time unit . 1000 millisecond = 1 second 
Overloading methods schedule The only way to use the return value callable Interface . Sure scheduleFuture.get() Get the return value 
function :
Initial initialDelay cycle period.
if ( Delay time > The elapsed time )
Initial time + Delay time
else {
Initial time + The elapsed time
}
function : Initial time + The elapsed time + Delay time 
test scheduleAtFixedRate() And 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);

summary : If the thread execution time is greater than the interval , Therefore, the interval time will become the execution time . So we know that the thread runs in serial . and scheduleWithFixedDelay Is the default execution time + Execute thread after interval .
边栏推荐
- Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
- [proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
- Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
- Remote file contains actual operation
- Mysql 将逗号隔开的属性字段数据由列转行
- Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
- Explore Netease's large-scale automated testing solutions see here see here
- PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- Uploads labs range (with source code analysis) (under update)
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
猜你喜欢

线程池执行定时任务

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)

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

arduino-esp32:LVGL项目(一)整体框架

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

Uploads labs range (with source code analysis) (under update)

斑马识别成狗,AI犯错的原因被斯坦福找到了

Getting started with Message Oriented Middleware

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

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
随机推荐
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
EditText request focus - EditText request focus
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
Acwing game 58
无心剑中译泰戈尔《漂鸟集(1~10)》
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
PHP二级域名session共享方案
Netease UI automation test exploration: airtest+poco
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
2022爱分析· 国央企数字化厂商全景报告
【LeetCode】94. Middle order traversal of binary tree
Hibernate的缓存机制/会话级缓存机制
一台服务器最大并发 tcp 连接数多少?65535?
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
Unity project optimization case 1
QT串口ui设计和解决显示中文乱码
Record a jar package conflict resolution process
Cocos Creator 2. X automatic packaging (build + compile)
Learn from me about the enterprise flutter project: simplified framework demo reference