当前位置:网站首页>线程池执行定时任务
线程池执行定时任务
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是默认执行时间+间隔时间后执行线程。
边栏推荐
- 8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
- Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
- Multithread 02 thread join
- 用通达信炒股开户安全吗?
- 架构实战营 - 第 6 期 毕业总结
- Function introduction of JMeter thread group
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- 深入理解 SQL 中的 Grouping Sets 语句
- [redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
- Mongodb installation and basic operation
猜你喜欢
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
Record windows10 installation tensorflow-gpu2.4.0
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
【LeetCode】94. Middle order traversal of binary tree
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
Nifi from introduction to practice (nanny level tutorial) - flow
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
近视:摘镜or配镜?这些问题必须先了解清楚
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
随机推荐
From the 18th line to the first line, the new story of the network security industry
无心剑中译泰戈尔《漂鸟集(1~10)》
[list to map] collectors Tomap syntax sharing (case practice)
EditText request focus - EditText request focus
Eleven requirements for test management post
Chinese translation of Tagore's floating birds (1~10)
Break through 1million, sword finger 2million!
探索Cassandra的去中心化分布式架构
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
跟我学企业级flutter项目:简化框架demo参考
Go language self-study series | if else statement in golang
Is it safe to open an account with flush?
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
切入点表达式
pyinstaller不是内部或外部命令,也不是可运行的程序 或批处理文件
Pyinstaller is not an internal or external command, nor is it a runnable program or batch file
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
First knowledge of database
用通达信炒股开户安全吗?