当前位置:网站首页>线程池执行定时任务
线程池执行定时任务
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是默认执行时间+间隔时间后执行线程。
边栏推荐
- Remote file contains actual operation
- 0214-27100 a day with little fluctuation
- Go language self-study series | golang switch statement
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
- Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
- Caching mechanism of Hibernate / session level caching mechanism
- How to use AAB to APK and APK to AAB of Google play apps on the shelves
- "Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
- 【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
- How can technology managers quickly improve leadership?
猜你喜欢

Break through 1million, sword finger 2million!

Low level version of drawing interface (explain each step in detail)

嵌入式开发:避免开源软件的7个理由

MongoDB 的安装和基本操作

Record windows10 installation tensorflow-gpu2.4.0

Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
![[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)](/img/04/460734209ec315c5c02cb3fae4bf0e.png)
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)

NSQ源码安装运行过程

【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示

Nifi from introduction to practice (nanny level tutorial) - flow
随机推荐
初试scikit-learn库
QT串口ui设计和解决显示中文乱码
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
Record windows10 installation tensorflow-gpu2.4.0
Cocos Creator 2.x 自动打包(构建 + 编译)
How to thicken the brush in the graphical interface
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
相同切入点的抽取
Nine ways to define methods in scala- Nine ways to define a method in Scala?
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
Interviewer: how does the JVM allocate and recycle off heap memory
Explore Netease's large-scale automated testing solutions see here see here
Detailed explanation of four modes of distributed transaction (Seata)
Nifi from introduction to practice (nanny level tutorial) - flow
Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
Multithread 02 thread join
【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
Extraction of the same pointcut
14 topics for performance interviews between superiors and subordinates (4)