当前位置:网站首页>线程池执行定时任务
线程池执行定时任务
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是默认执行时间+间隔时间后执行线程。
边栏推荐
- 突破100万,剑指200万!
- Mixlab编辑团队招募队友啦~~
- Is it safe to open an account with flush?
- TCP congestion control details | 3 design space
- 切入点表达式
- Expression of request header in different countries and languages
- pycharm错Error updating package list: connect timed out
- One article takes you to understand machine learning
- Détails du contrôle de la congestion TCP | 3. Espace de conception
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
猜你喜欢
NSQ源码安装运行过程
First knowledge of database
Getting started with Message Oriented Middleware
Principles of several common IO models
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
Mb10m-asemi rectifier bridge mb10m
uploads-labs靶场(附源码分析)(更新中)
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
Basis of target detection (IOU)
MongoDB 的安装和基本操作
随机推荐
Detailed explanation of four modes of distributed transaction (Seata)
Record windows10 installation tensorflow-gpu2.4.0
Chinese translation of Tagore's floating birds (1~10)
首发!!lancet饿了么官方文档
pycharm错Error updating package list: connect timed out
Record a jar package conflict resolution process
Characteristic polynomial and constant coefficient homogeneous linear recurrence
手机注册股票开户安全吗 开户需要钱吗
程序猿如何快速成长
uploads-labs靶场(附源码分析)(更新中)
疫情常态化大背景下,关于远程办公的思考|社区征文
Pyinstaller is not an internal or external command, nor is it a runnable program or batch file
无心剑中译泰戈尔《漂鸟集(1~10)》
From the 18th line to the first line, the new story of the network security industry
Principles of several common IO models
App mobile terminal test [3] ADB command
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
Golang 匿名函数使用
Low level version of drawing interface (explain each step in detail)
First knowledge of database