当前位置:网站首页>任务调度线程池基本介绍
任务调度线程池基本介绍
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...
评价 整个线程池表现为:线程数固定,任务数多于线程数时,会放入无界队列排队。任务执行完毕,这些线程也不会被释放。用来执行延迟或反复执行的任务
边栏推荐
- 第57章 业务逻辑之业务实体与数据库表的映射规则定义
- 【节能学院】推进农业水价综合改革的意见解读
- regular expression
- 【torch】张量乘法:matmul,einsum
- Redis 做签到统计
- Redis does web page UV statistics
- 57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom
- 【社媒营销】如何知道自己的WhatsApp是否被屏蔽了?
- Wildcard SSL/TLS certificate
- 大神经验:软件测试的自我发展规划
猜你喜欢

启明云端分享|盘点ESP8684开发板有哪些功能

Little data on how to learn?Jida latest small learning data review, 26 PDF page covers the 269 - page document small data learning theory, method and application are expounded

How PROE/Croe edits a completed sketch and brings it back to sketching state

Greenplum数据库源码分析——Standby Master操作工具分析

【kali-信息收集】(1.2)SNMP枚举:Snmpwalk、Snmpcheck;SMTP枚举:smtp-user-enum

Convolutional Neural Network (CNN) mnist Digit Recognition - Tensorflow
![57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom](/img/e1/2fa8dcc9c246bbbc2494326a83cda1.png)
57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom

Acrel-5010重点用能单位能耗在线监测系统在湖南三立集团的应用

【torch】张量乘法:matmul,einsum

部署zabbix
随机推荐
Application of Acrel-5010 online monitoring system for key energy consumption unit energy consumption in Hunan Sanli Group
The graphic details Eureka's caching mechanism/level 3 cache
研究生新同学,牛人看英文文献的经验,值得你收藏
有用的网站
Wildcard SSL/TLS certificate
Does LabVIEW really close the COM port using VISA Close?
我的驾照考试笔记(2)
Creo5.0 rough hexagon is how to draw
[Personal Work] Remember - Serial Logging Tool
Win10, the middle mouse button cannot zoom in and out in proe/creo
17. Load balancing
myid file is missing
图文详述Eureka的缓存机制/三级缓存
Hangao data import
【kali-信息收集】(1.6)服务的指纹识别:Nmap、Amap
Multithreaded producers and consumers
实用新型专利和发明专利的区别?秒懂!
[Multi-task model] Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized (RecSys'20)
18、分布式配置中心nacos
SIPp 安装及使用