当前位置:网站首页>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 .
边栏推荐
- Multithread 02 thread join
- Is it safe to open an account with flush?
- Explore Cassandra's decentralized distributed architecture
- 1287. Elements that appear more than 25% in an ordered array
- Threejs Part 2: vertex concept, geometry structure
- One article takes you to understand machine learning
- TCP congestion control details | 3 design space
- Chinese translation of Tagore's floating birds (1~10)
- Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
- PHP二级域名session共享方案
猜你喜欢
Visual SLAM algorithms: a survey from 2010 to 2016
Getting started with Message Oriented Middleware
Stm32f103c8t6 firmware library lighting
Explore Cassandra's decentralized distributed architecture
The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
ThreeJS 第二篇:顶点概念、几何体结构
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
记一次jar包冲突解决过程
Mysql 单表字段重复数据取最新一条sql语句
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
随机推荐
Construction practice camp - graduation summary of phase 6
Cocos Creator 2.x 自动打包(构建 + 编译)
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
Mixlab编辑团队招募队友啦~~
PHP CI(CodeIgniter)log级别设置
Getting started with Message Oriented Middleware
TCP congestion control details | 3 design space
EditText request focus - EditText request focus
Nine ways to define methods in scala- Nine ways to define a method in Scala?
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
QT serial port UI design and solution to display Chinese garbled code
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
什么是质押池,如何进行质押呢?
Mongodb installation and basic operation
Basis of target detection (IOU)
《天天数学》连载56:二月二十五日
QT串口ui设计和解决显示中文乱码
Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线