当前位置:网站首页>线程池执行定时任务
线程池执行定时任务
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是默认执行时间+间隔时间后执行线程。
边栏推荐
- 用同花顺炒股开户安全吗?
- Pychart error updating package list: connect timed out
- 特征多项式与常系数齐次线性递推
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- How to use AAB to APK and APK to AAB of Google play apps on the shelves
- EditText request focus - EditText request focus
- 拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
- Page dynamics [2]keyframes
- A Fei's expectation
- 切入点表达式
猜你喜欢
记一次jar包冲突解决过程
2022年Q2加密市场投融资报告:GameFi成为投资关键词
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Project -- high concurrency memory pool
Embedded development: seven reasons to avoid open source software
QT串口ui设计和解决显示中文乱码
【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
探索Cassandra的去中心化分布式架构
【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
随机推荐
pyinstaller不是内部或外部命令,也不是可运行的程序 或批处理文件
Function introduction of JMeter thread group
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
TCP拥塞控制详解 | 3. 设计空间
用通达信炒股开户安全吗?
Embedded development: seven reasons to avoid open source software
MongoDB 的安装和基本操作
远程文件包含实操
“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
Basis of target detection (IOU)
Mixlab编辑团队招募队友啦~~
App mobile terminal test [3] ADB command
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
Go language self-study series | if else if statement in golang
App mobile terminal test [4] APK operation
Hibernate的缓存机制/会话级缓存机制
Salary 3000, monthly income 40000 by "video editing": people who can make money never rely on hard work!
Extraction of the same pointcut
Principles of several common IO models
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14