当前位置:网站首页>【多线程】 如何自己实现定时器
【多线程】 如何自己实现定时器
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 计时器类 的 构造方法。创建一个线程,帮助我们来进行一个检查

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

代码
边栏推荐
- 尚硅谷实时数据仓库项目(阿里云实时数仓)
- 细说GaussDB(DWS)复杂多样的资源负载管理手段
- 亚马逊关键词搜索API接口(item_search-按关键字搜索亚马逊商品接口),亚马逊API接口
- A. Beat The Odds
- 研发测试时间比,BUG数据分析
- static关键字续、继承、重写、多态
- A. Marathon
- Alibaba keyword search commodity API interface (item_search- commodity search interface by keyword), Alibaba Search API interface
- Water polo chart - using dynamic ripples to show percentages
- Don't worry about form deformation anymore
猜你喜欢

铝板AS/NZS 1530.1 不燃性材料的阻燃测试

Structure the fifth operation of the actual camp module

This time, I will talk about technology and life

小型圖書館項目總結

华为7年经验的软件测试总监,给所有想转行学软件测试的同学的几个建议

The soft youth under the blessing of devcloud makes education "smart" in the cloud

为什么要同时重写hashcode和equals方法之简单理解

Golang operation etcd

ASP using panel to realize simple registration page

Water polo chart - using dynamic ripples to show percentages
随机推荐
jfinal中如何使用过滤器监控Druid监听SQL执行?
Is it safe to open a securities account in Caixue school?
Reflections on remote sensing image interpretation
Reading notes on how to connect the network - LAN on the server side (4)
数论-整除分块
How to use SMS to deliver service information to customers? The guide is here!
Information available from radar echo
Flame retardant test of aluminum sheet as/nzs 1530.1 non combustible materials
为什么要同时重写hashcode和equals方法之简单理解
Win10添加ssh公钥
R language plot visualization: plot to visualize the normalized histograms of multiple data sets, set different histograms to use different bin sizes, and add edge axis whisker graph rugs at the botto
Detailed explanation of MySQL and mvcc and the difference between RC and RR for snapshot reading
C. Where‘s the Bishop?
Report delivery engineer
ASP动态创建表格 Table
Introduction to gaofen-3 satellite (GF-3)
Analysis of typical remote sensing tasks
Writing a makefile for a golang project
Vipshop Keyword Search API interface (item_search- search vipshop commodity API interface by keyword), vipshop API interface
Taro2.* applet configuration sharing wechat circle of friends