当前位置:网站首页>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 .
边栏推荐
- 跟我学企业级flutter项目:简化框架demo参考
- 面试官:JVM如何分配和回收堆外内存
- A survey of state of the art on visual slam
- [proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
- 斑马识别成狗,AI犯错的原因被斯坦福找到了
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Is it safe to open a stock account by mobile registration? Does it need money to open an account
- 2022爱分析· 国央企数字化厂商全景报告
- PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- 如何在本机搭建SVN服务器
猜你喜欢
ThreeJS 第二篇:顶点概念、几何体结构
Visual SLAM algorithms: a survey from 2010 to 2016
消息队列消息丢失和消息重复发送的处理策略
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
How to use AAB to APK and APK to AAB of Google play apps on the shelves
2022爱分析· 国央企数字化厂商全景报告
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
Mysql 单表字段重复数据取最新一条sql语句
爱可可AI前沿推介(7.3)
随机推荐
Nine ways to define methods in scala- Nine ways to define a method in Scala?
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
EditText request focus - EditText request focus
Mysql 单表字段重复数据取最新一条sql语句
How can technology managers quickly improve leadership?
探索Cassandra的去中心化分布式架构
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
Svn usage specification
Data driving of appium framework for mobile terminal automated testing
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
(补)双指针专题
Aike AI frontier promotion (7.3)
Cocos Creator 2.x 自动打包(构建 + 编译)
Mongodb installation and basic operation
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
[web security] - [SQL injection] - error detection injection
Construction practice camp - graduation summary of phase 6
斑馬識別成狗,AI犯錯的原因被斯坦福找到了