当前位置:网站首页>任务调度线程池基本介绍
任务调度线程池基本介绍
2022-08-01 20:22:00 【Q z1997】
在『任务调度线程池』功能加入之前,可以使用 java.util.Timer 来实现定时功能,Timer 的优点在于简单易用,但由于所有任务都是由同一个线程来调度,因此所有任务都是串行执行的,同一时间只能有一个任务在执行,前一个任务的延迟或异常都将会影响到之后的任务。
Timer timer = new Timer();
TimerTask task1 = new TimerTask() {
@Override
public void run() {
log.debug("task 1");
sleep(2);
}
};
TimerTask task2 = new TimerTask() {
@Override
public void run() {
log.debug("task 2");
}
};
// 使用 timer 添加两个任务,希望它们都在 1s 后执行
// 但由于 timer 内只有一个线程来顺序执行队列中的任务,因此『任务1』的延时,影响了『任务2』的执行
timer.schedule(task1, 1000);
timer.schedule(task2, 1000);
输出
20:46:09.444 c.TestTimer [main] - start...
20:46:10.447 c.TestTimer [Timer-0] - task 1
20:46:12.448 c.TestTimer [Timer-0] - task 2
使用 ScheduledExecutorService
改写:
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
// 添加两个任务,希望它们都在 1s 后执行
executor.schedule(() -> {
System.out.println("任务1,执行时间:" + new Date());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}, 1000, TimeUnit.MILLISECONDS);
executor.schedule(() -> {
System.out.println("任务2,执行时间:" + new Date());
}, 1000, TimeUnit.MILLISECONDS);
输出
任务1,执行时间:Thu Jan 03 12:45:17 CST 2019
任务2,执行时间:Thu Jan 03 12:45:17 CST 2019
scheduleAtFixedRate
例子:
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
log.debug("start...");
// param1: 任务 param2: 等待多长时间 param3: 间隔多长时间 param4: 时间单位
pool.scheduleAtFixedRate(() -> {
log.debug("running...");
}, 1, 1, TimeUnit.SECONDS);
scheduleAtFixedRate
例子:
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
log.debug("start...");
pool.scheduleAtFixedRate(() -> {
log.debug("running...");
// 任务执行时间超过了间隔时间
sleep(2);
}, 1, 1, TimeUnit.SECONDS);
输出分析:一开始,延时 1s,接下来,由于任务执行时间 > 间隔时间,间隔被『撑』到了 2s
21:44:30.311 c.TestTimer [main] - start...
21:44:31.360 c.TestTimer [pool-1-thread-1] - running...
21:44:33.361 c.TestTimer [pool-1-thread-1] - running...
21:44:35.362 c.TestTimer [pool-1-thread-1] - running...
21:44:37.362 c.TestTimer [pool-1-thread-1] - running...
scheduleWithFixedDelay
例子:
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
log.debug("start...");
pool.scheduleWithFixedDelay(() -> {
log.debug("running...");
sleep(2);
}, 1, 1, TimeUnit.SECONDS);
输出分析:一开始,延时 1s,scheduleWithFixedDelay 的间隔是 上一个任务结束 <-> 延时 <-> 下一个任务开始 所以间隔都是 3s
21:40:55.078 c.TestTimer [main] - start...
21:40:56.140 c.TestTimer [pool-1-thread-1] - running...
21:40:59.143 c.TestTimer [pool-1-thread-1] - running...
21:41:02.145 c.TestTimer [pool-1-thread-1] - running...
21:41:05.147 c.TestTimer [pool-1-thread-1] - running...
评价 整个线程池表现为:线程数固定,任务数多于线程数时,会放入无界队列排队。任务执行完毕,这些线程也不会被释放。用来执行延迟或反复执行的任务
边栏推荐
- 通配符 SSL/TLS 证书
- Ruijie switch basic configuration
- 给定中序遍历和另外一种遍历方法确定一棵二叉树
- 大神经验:软件测试的自我发展规划
- 【多任务模型】Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized(RecSys‘20)
- [Multi-task learning] Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts KDD18
- Digital twin Beijing the imperial palace, yuan universe is the process of tourism
- MongoDB快速上手
- Buttons with good user experience should not have hover state on mobile phones
- KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System
猜你喜欢
外骨骼机器人(七):标准步态数据库
】 【 nn. The Parameter () to generate and why do you want to initialize
我的驾照考试笔记(3)
LabVIEW 使用VISA Close真的关闭COM口了吗
[Multi-task optimization] DWA, DTP, Gradnorm (CVPR 2019, ECCV 2018, ICML 2018)
使用Huggingface在矩池云快速加载预训练模型和数据集
nacos installation and configuration
17. Load balancing
[Energy Conservation Institute] Comparative analysis of smart small busbar and column head cabinet solutions in data room
【节能学院】智能操控装置在高压开关柜的应用
随机推荐
Where should I prepare for the PMP exam in September?
【torch】张量乘法:matmul,einsum
实用新型专利和发明专利的区别?秒懂!
锐捷交换机基础配置
我的驾照考试笔记(1)
外骨骼机器人(七):标准步态数据库
因斯布鲁克大学团队量子计算硬件突破了二进制
作为程序员你应该会的软件
C语言实现-直接插入排序(带图详解)
解除360对默认浏览器的检测与修改
【多任务模型】Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized(RecSys‘20)
【个人作品】记之-串口日志记录工具
】 【 nn. The Parameter () to generate and why do you want to initialize
[Energy Conservation Institute] Ankerui Food and Beverage Fume Monitoring Cloud Platform Helps Fight Air Pollution
模板特例化和常用用法
如何写一个vim插件?
nacos安装与配置
Does LabVIEW really close the COM port using VISA Close?
用户身份标识与账号体系实践
专利检索常用的网站有哪些?