当前位置:网站首页>[multithreading] how to implement timer by yourself
[multithreading] how to implement timer by yourself
2022-06-29 22:12:00 【51CTO】
Catalog
What is a timer
Timer is an important component in multithreaded programmingLike an alarm clock Set a time for a thread to executeTimers are particularly common in network programming
such as :
Before we open the browser , Open any web page
If all goes well , You can access the website right awayhowever !
Sometimes because the network is unstable , Or the server of the website crashed.
that , At this point, it will be loaded in circles .
But the browser will not always be here waiting for feedback from the visited website .There is actually a waiting in it Of “ Timeout time ”, After this time , It won't wait , Just tell you “ The visit to fail / The website is missing ”At this point, we can soon think of join, There is a usage , Add the specified... In parentheses “ Timeout time ”.
sleep It can also achieve this effect ,sleep( Specify sleep time ) .join and sleep Are based on the timer inside the system , To achieve
that , We can use Timer in code , To implement similar functions .
First introduce the timer usage of the standard library , Then we'll see how to implement a timer by ourselves
Timer usage of standard library
Timer <-----> java.util.Timer
The core approach:scheduleschedule, Its function is just like it The Chinese meaning is the same .
Every call schedule‘, Will give the timer Arrange a task .
Through this method , You can put the Mission Sign up to Timer internal .
The internal counter supports register More than one task .
schedule Method , Yes Two parameters :
MissionHow long will it take to execute

How to implement a timer by yourself
A problem :Timer class What do you need inside ?
from Timer Of Start with the work content
- Manage many tasks
- Perform the task when the time is up
Management tasks And then you can subdivide it into 2 individual :
1、 Describe task ( Create a special class to represent the task in a timer 【Timer Task】)
2、 Organizational tasks ( Use certain data and structure to organize data , Put some tasks together .)
The specific task sequence is
1、 Describe task ( Create a special class to represent the task in a timer 【Timer Task】)
2、 Organizational tasks ( Use certain data and structure to organize data , Put some tasks together .)
3、 Perform the task when the time is up
1. Describe task
2. Organizational tasks

3. Perform the task when the time is up
You need to perform the task with the highest time first
such as : Ten minutes later , Let's go and have a rest .
however , At present, we can't judge the time , So we need to use a thread to constantly check the first element of the current priority queue , Let's see if it's time for the current top task
adopt Self constructed Mytimer Timer class Of Construction method .Create a thread , Help us with a check

There are two very serious problems in the above code !!!

Code
边栏推荐
- Matlab adds noise / disturbance to data
- Introduction to gaofen-3 satellite (GF-3)
- [crossbeam series] 5 crossbeam util and crossbeam queue: some practical gadgets
- 唯品会关键词搜索API接口(item_search-按关键字搜索唯品会商品API接口),唯品会API接口
- Shangsilicon Valley real-time data warehouse project (Alibaba cloud real-time data warehouse)
- 生产环境AIX小机报错B6267342问题处理
- BUAA OO unit 4 HW16 unit 4 Summary and course review
- 【多线程】 如何自己实现定时器
- How do new shareholders open accounts online? Is it safe to open an account online?
- The logic behind the three whys encountered in technical communication
猜你喜欢

Reading notes on how to connect the network - LAN on the server side (4)

Desai wisdom number - other charts (basic sunrise chart): high frequency words in graduation speech

DevCloud加持下的青软,让教育“智”上云端
A mysql IBD file is too large processing record
![The inadvertently discovered [tidb cache table] can solve the read / write hotspot problem](/img/96/b1595b9d2b008b353765caa68fdd3c.png)
The inadvertently discovered [tidb cache table] can solve the read / write hotspot problem

Visual analysis and display effect of summer data

Bs-gx-018 student examination system based on SSM

ASP. Net cross page submission (button control page redirection)

5分钟快速上手 pytest 测试框架

Layer 3 loop brought by route Summary - solution experiment
随机推荐
唯品会关键词搜索API接口(item_search-按关键字搜索唯品会商品API接口),唯品会API接口
Taro applet enables wxml code compression
CSDN failed to replicate problem
26岁,0基础转行软件测试,从月薪3k到16k,我整理的超全学习指南
Flame retardant test of aluminum sheet as/nzs 1530.1 non combustible materials
Does rapid software delivery really need to be at the cost of security?
Data mining review
Water polo chart - using dynamic ripples to show percentages
static关键字续、继承、重写、多态
Go standard library context package: data, cancellation signal, deadline and other related operations between a single request and multiple goroutines and the request domain
Spark集群安装
ASP.NET 跨页面提交(Button控件页面重定向)
从第三次技术革命看企业应用三大开发趋势
Report delivery engineer
美国隧道法ASTM E84 表面阻燃测试
分析安装包LNMP中的apache.sh脚本
Bs-gx-018 student examination system based on SSM
便携式4K音视频会议终端一体机带8倍数字变焦
Numpy's research imitation 1
jfinal中如何使用过滤器监控Druid监听SQL执行?