当前位置:网站首页>【多线程】 如何自己实现定时器
【多线程】 如何自己实现定时器
2022-06-29 21:43:00 【51CTO】
目录
什么是定时器
定时器是多线程编程中的一个重要的组件好比一个闹钟 定好一个时间让一个线程去执行定时器在网络编程中特别常见
比如:
在我们打开浏览器,随便打开一个网页
如果顺利,马上就能进入网站但是!
有时候由于网络不稳定,或者网站的服务器崩了。
那么,此时这里就会一直转圈圈加载。
但是浏览器不会一直都在这里等待访问的网站反馈信息。它里面其实有一个等待 的“超时时间”,过了这个时间,它就不会再等待,直接跟你说“访问失败 / 网站不见了”此时我们很快就能想到 join,有一个用法,在括号里添加指定的 “超时时间”。
sleep也可以达到这个效果,sleep(指定休眠时间) 。join 和 sleep 都是基于系统内部的定时器,来实现的
那么,我们就可以使用 代码中的定时器,来实现类似的功能。
先介绍标准库的定时器用法,然后再看看如何自己实现一个定时器
标准库的定时器用法
Timer <-----> java.util.Timer
核心方法:scheduleschedule,它的功能就跟 它 中文意思一样。
每调用一次 schedule‘,就会给定时器 安排一个任务。
通过这个方法,就可以把 任务 注册到 定时器内部。
而计数器内部是支持 注册 多个任务的。
schedule方法,有两个参数:
任务多长时间之后执行

如何自己实现一个定时器
一个问题:Timer 类 的内部需要什么东西?
从Timer 的 工作内容入手
- 管理很多的任务
- 执行时间到了的任务
管理任务又可以细分为 2个:
1、描述任务(创建一个专门的类来表示一个定时器中的任务【Timer Task】)
2、组织任务(使用一定的数据及结构进行组织数据,把一些任务放到一起。)
具体任务顺序为
1、描述任务(创建一个专门的类来表示一个定时器中的任务【Timer Task】)
2、组织任务(使用一定的数据及结构进行组织数据,把一些任务放到一起。)
3、执行时间到了的任务
1. 描述任务
2. 组织任务

3. 执行时间到了的任务
需要先执行时间最靠前的任务
比如:十分钟后,大家去休息一下。
但是,当前的我们无法判断这个时间,所以我们需要用一个线程去不断的去检查当前优先队列的首元素,看看当前最靠前的这个任务是不是时间到了
通过 自己构造的 Mytimer 计时器类 的 构造方法。创建一个线程,帮助我们来进行一个检查

上述代码中存在两个非常严重的问题!!!

代码
边栏推荐
- The logic behind the three whys encountered in technical communication
- 亚马逊关键词搜索API接口(item_search-按关键字搜索亚马逊商品接口),亚马逊API接口
- Top ten questions for senior Performance Test Engineer
- 数论-整除分块
- Flame retardant test of aluminum sheet as/nzs 1530.1 non combustible materials
- How do new shareholders open accounts online? Is it safe to open an account online?
- Automatic reply of wechat bulletin number intelligent reply with Turing robot
- 22 years of a doctor in Huawei
- LeetCode 1. Sum of two numbers
- 阿里巴巴店铺的所有商品API接口(item_search_shop-获得店铺的所有商品接口),阿里巴巴API接口
猜你喜欢

ASP dynamically creates table table

Huawei cloud AOM version 2.0 release

Cout ambiguous problem
A mysql IBD file is too large processing record

Huawei cloud AOM version 2.0 release

Divide the bonus pool of 10million + million yuan, and empower developers in the 2022 shengteng AI innovation competition

Deep learning remote sensing data set

cout 不明确问题

Matlab adds noise / disturbance to data

Layer 3 loop brought by route Summary - solution experiment
随机推荐
软件快速交付真的需要以安全为代价吗?
As a developer, you need to know about the codeless development platform IVX
American tunneling ASTM E84 surface flame retardant test
Final training simple address book c language
Information available from radar echo
Change detection and batch update
The explain function of the DALEX package of R language generates a machine learning model interpreter and predict for the specified classification prediction_ The parts function analyzes the contribu
Huawei cloud AOM version 2.0 release
Realization of graduation project topic selection system based on JSP
Reading notes on how to connect the network - Web server request and response (V)
This time, I will talk about technology and life
澳洲要求PVC 塑料片符合AS/NZS 1530.3 火焰蔓延指数为0吗?
Is it reliable to open an account on the compass with your mobile phone? Is there any hidden danger in this way
证券开户选择哪个证券另外想问,现在在线开户安全么?
什么是 SYN 洪水攻击?如何防护?
If the evaluation conclusion of waiting insurance is poor, does it mean that waiting insurance has been done in vain?
As for the domestic Kirin system running QT, it can be run on the command line but cannot be run by double clicking (no response)
细说GaussDB(DWS)复杂多样的资源负载管理手段
华为云AOM 2.0版本发布
Reflections on remote sensing image interpretation