当前位置:网站首页>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
边栏推荐
- [网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
- Principle, concept and construction process of MySQL database master-slave replication cluster
- 剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
- 检定和校准的区别
- Principle of control system based on feedback rate
- MySQL数据库主从复制集群原理概念以及搭建流程
- origin如何作一张图中多张子图是柱状图(或其他图)
- Iptables firewall
- Sword finger offer note: t45. arrange the array into the smallest number
- Introduction to box diagram
猜你喜欢
随机推荐
我在英国TikTok做直播电商
Why is ack=seq+1 when TCP shakes hands three times
pytorch和tensorflow一样展示summary
JS字符串方法总结
哈希表 详细讲解
npm踩坑
TLC549Proteus仿真&Sallen-Key滤波器&AD736Vrms到DC转换&Proteus查看51寄存器值
Sword finger offer notes: t58 - I. flip word order
shell编程之免交互
Unity Shader 一 激光特效Shader[通俗易懂]
Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
剑指 Offer 笔记: T53 - I. 在排序数组中查找数字
STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
B 站 713 事故后的多活容灾建设|TakinTalks 大咖分享
go 用本地代码replace
Firewalld防火墙
[untitled] multimodal model clip
Beyond compare 3 next difference segment / down search arrow not found
Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
Summary of leetcode SQL exercises (MySQL Implementation)








