当前位置:网站首页>Newticker uses
Newticker uses
2022-07-27 11:57:00 【Big leaves are not small】
I don't say much nonsense , Look directly at the code
package mainimport ("fmt""sync""time")/***ticker As long as the definition is complete , From this moment on , No other operation is required , Trigger every fixed time .*timer Timer , It will be executed once after a fixed time* If timer The timer is executed at intervals , Realization ticker The effect of , Use func (t *Timer) Reset(d Duration) bool*/func main() {var wg sync.WaitGroupwg.Add(2)//NewTimer Create a Timer, It will be at least in the past d To expire after , To its own C Field sent attimer1 := time.NewTimer(2 * time.Second)//NewTicker Back to a new Ticker, The Ticker Contains a channel field , And every once in a while d Send the current time to the channel . It can tune// Whole time interval or discard tick Information to accommodate slow responders . If d <= 0 Will trigger panic. Close the Ticker can// To release resources .ticker1 := time.NewTicker(2 * time.Second)go func(t *time.Ticker) {defer wg.Done()for {<-t.Cfmt.Println("get ticker1", time.Now().Format("2006-01-02 15:04:05"))}}(ticker1)go func(t *time.Timer) {defer wg.Done()for {<-t.Cfmt.Println("get timer", time.Now().Format("2006-01-02 15:04:05"))//Reset send t Restart the clock ,( After this method returns ) Waiting period d Due in the past . If you call t// Still waiting to return to true ; If t Has expired or has been stopped will return to leave .t.Reset(2 * time.Second)}}(timer1)wg.Wait()}
Running results :
get ticker1 2018-09-07 22:44:29get timer 2018-09-07 22:44:29...
Additional instructions :
time.NewTicker Regularly trigger the execution of tasks , When the next execution comes and the current task is not finished , It will wait for the current task to be executed before executing the next task . Look up go Official website documentation and code validation .
time.NewTimer and Reset() Function implementation timing trigger ,Reset() The function may fail , After testing .
Reprinted to
https://golangcaff.com/articles/230/the-difference-between-go-timer-and-ticker
边栏推荐
- 你尚未连接代理服务器可能有问题或地址不正确(如何查看代理服务器ip)
- 孤独的年轻人,戒不了Jellycat
- 微商的差商近似
- USB network card drive data stream
- Tlc549proteus simulation &sallen key filter &ad736vrms to DC conversion &proteus view 51 register value
- 解决@OneToMany查询陷入循环引用问题
- 查看系统下各个进程打开的文件描述符数量
- compute_class_weight() takes 1 positional argument but 3 were given
- Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
- Arduino常见供电问题与解决
猜你喜欢

Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法

Shell script text three swordsman awk

Analysis of the use of JUC framework from runnable to callable to futuretask

Finding the finite zero point of transfer function under different sampling periods
![[machine learning whiteboard derivation series] learning notes --- conditional random fields](/img/cc/87d8822b659b31360546adf057ef00.png)
[machine learning whiteboard derivation series] learning notes --- conditional random fields

【机器学习-白板推导系列】学习笔记---条件随机场

Beyond compare 3 next difference segment / down search arrow not found

微博评论爬虫+可视化

Vscode removes style / syntax highlighting / code highlighting / black background when copying code

EfficientNet
随机推荐
Sword finger offer note: t45. arrange the array into the smallest number
Sword finger offer notes: t58 - ii Rotate string left
箱型图介绍
希腊字母读法
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
快抖抢救“失意人”
Can you really write binary search - variant binary search
Temporary use of solo, difficult choice of Blog
Common power supply problems and solutions of Arduino
CH340模块无法识别/烧写不进的一种可能性
源码编译安装LAMP
Docker Mysql的使用note
V-show失效问题
Interaction free shell programming
剑指 Offer 笔记: T45. 把数组排成最小的数
npm踩坑
Wilcoxon rank sum and signed rank
剑指 Offer 笔记: T53 - I. 在排序数组中查找数字
Greek alphabet reading
Unity Shader 一 激光特效Shader[通俗易懂]