当前位置:网站首页>Thread pool executes scheduled tasks
Thread pool executes scheduled tasks
2022-07-03 16:29:00 【Xiao Chen - Programmer】
For scheduled tasks schedule Thread pool decision , So use Executors The scheduled tasks generated by the class are SchduledExecutorService Service implementation .
ScheduledExecutorService Is to implement the ExecutorService Interface
ScheduledExecutorService There are four ways
delay : Delay running for a few seconds TimeUnit : Time unit . 1000 millisecond = 1 second 
Overloading methods schedule The only way to use the return value callable Interface . Sure scheduleFuture.get() Get the return value 
function :
Initial initialDelay cycle period.
if ( Delay time > The elapsed time )
Initial time + Delay time
else {
Initial time + The elapsed time
}
function : Initial time + The elapsed time + Delay time 
test scheduleAtFixedRate() And 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);

summary : If the thread execution time is greater than the interval , Therefore, the interval time will become the execution time . So we know that the thread runs in serial . and scheduleWithFixedDelay Is the default execution time + Execute thread after interval .
边栏推荐
- Netease UI automation test exploration: airtest+poco
- Unreal_DataTable 实现Id自增与设置RowName
- PHP CI (CodeIgniter) log level setting
- Extraction of the same pointcut
- One article takes you to understand machine learning
- 记一次jar包冲突解决过程
- Custom plug-in construction and use of QT plug-in
- Pointcut expression
- Deep understanding of grouping sets statements in SQL
- 特征多项式与常系数齐次线性递推
猜你喜欢

斑马识别成狗,AI犯错的原因被斯坦福找到了

爱可可AI前沿推介(7.3)

Getting started with Message Oriented Middleware

From the 18th line to the first line, the new story of the network security industry

关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM

近视:摘镜or配镜?这些问题必须先了解清楚

Interviewer: how does the JVM allocate and recycle off heap memory

0214-27100 a day with little fluctuation
![[solved] access denied for user 'root' @ 'localhost' (using password: yes)](/img/71/1ff8ed1d773da99054310f96dca3f8.jpg)
[solved] access denied for user 'root' @ 'localhost' (using password: yes)

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
随机推荐
Expression of request header in different countries and languages
One article takes you to understand machine learning
14 topics for performance interviews between superiors and subordinates (4)
(补)双指针专题
利用MySQL中的乐观锁和悲观锁实现分布式锁
TCP congestion control details | 3 design space
Is it safe to open an account with tongdaxin?
Construction practice camp - graduation summary of phase 6
用同花顺炒股开户安全吗?
ThreeJS 第二篇:顶点概念、几何体结构
MongoDB 的安装和基本操作
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
Deep understanding of grouping sets statements in SQL
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Mysql 单表字段重复数据取最新一条sql语句
Golang 匿名函数使用
Top k questions of interview
Détails du contrôle de la congestion TCP | 3. Espace de conception
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
QT serial port UI design and solution to display Chinese garbled code