当前位置:网站首页>Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
2022-06-21 12:40:00 【51CTO】
欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos
起因
- 一个基于Spring Cloud框架的应用,如果注册到了Eureka server,那么它就会定时更新服务列表,这个定时任务启动的代码在com.netflix.discovery.DiscoveryClient类的initScheduledTasks方法中,如下(来自工程eureka-client,版本1.7.0):
- 上述代码中,scheduler是ScheduledExecutorService接口的实现,其schedule方法的 官方文档如下所示:

上图红框显示:该方法创建的是一次性任务,但是在实际测试中,如果在CacheRefreshThread类的run方法中打个断点,就会发现该方法会被周期性调用;
因此问题就来了:方法schedule(Callable<V> callable,long delay,TimeUnit unit)创建的明明是个一次性任务,但CacheRefreshThread被周期性执行了;
寻找答案
- 打开的run方法源码,请注意下面的中文注释:
真相就在上面的最后一行代码中:scheduler.schedule(this, delay.get(), TimeUnit.MILLISECONDS):执行完任务后,会再次调用schedule方法,在指定的时间之后执行一次相同的任务,这个间隔时间和最近一次任务是否超时有关,如果超时了就间隔时间就会变大;
小结:从整体上看,TimedSupervisorTask是固定间隔的周期性任务,一旦遇到超时就会将下一个周期的间隔时间调大,如果连续超时,那么每次间隔时间都会增大一倍,一直到达外部参数设定的上限为止,一旦新任务不再超时,间隔时间又会自动恢复为初始值,另外还有CAS来控制多线程同步,简洁的代码,巧妙的设计,值得我们学习;
欢迎关注51CTO博客:程序员欣宸
边栏推荐
- Workbench常见网格划分方法讲解
- [安洵杯 2019]easy_web-1
- centos7 部署mysql 环境
- Data types in redis
- 2022年中国手机银行年度专题分析
- redis中的数据类型
- Educoder Web练习题---表单的结构
- [upgraded student information management system] + file operation + more details
- Pre training weekly (50): no decoding converter, neural prompt search, gradient space dimensionality reduction
- Educoder Web练习题---创建下拉列表
猜你喜欢

Educator table labels - settings for Advanced Table Styles

centos7 部署mysql 环境

Distributed transactions, simple in principle, are all pits in writing

Nouveau partage de l'expérience de travail à domicile

About SDRAM memory

居家辦公初體驗之新得分享| 社區征文

计算距离的几种方式汇总

Add a description to the form component

分布式事务,原理简单,写起来全是坑

最新远程部署运维工具汇总
随机推荐
AGCO AI frontier promotion (6.21)
Educoder Web练习题---创建文本区域
Educoder web exercises - interactive elements
Tami dog sharing: the way of property right transaction and the significance of data-based property right transaction market
最新远程部署运维工具汇总
Mvcc in PostgreSQL
Educoder Web练习题---交互元素
塔米狗 热门资讯:厦门产权交易中心再创第一次!
计算距离的几种方式汇总
安科瑞BM100系列信号隔离器的实际应用
Pretraining Weekly No. 50: No Decode converter, neural prompt search, gradient Space reduction
Data types in redis
机器学习探索共价键碳的亚稳态相图,未来可用于发现或设计亚稳态材料
Educoder web exercises - form structure
~~~~Configuration
CVPR2022 | 上科大x小红书首次提出动作序列验证任务,可应用于体育赛事打分等多场景
Machine learning explores the metastable phase diagram of covalent carbon, which can be used to discover or design metastable materials in the future
Three structures of program - system learning I
About SDRAM memory
【毕业季·进击的技术er】大学生涯的成长之路