当前位置:网站首页>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
边栏推荐
- 快抖抢救“失意人”
- 一些MathType常用快捷键
- Adobe audit prompts that the sampling rate of audio input does not match the output device - problem solving
- 剑指 Offer 笔记: T45. 把数组排成最小的数
- Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
- MySQL数据库主从复制集群原理概念以及搭建流程
- SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
- omitempty在go中的使用
- JS-寄生组合式继承
- 剑指 Offer 笔记: T53 - II. 0~n-1 中缺失的数字
猜你喜欢

【无标题】多模态模型 CLIP

Wilcoxon rank sum and signed rank

Iptables firewall

Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution

Detailed explanation of hash table

Principle of PWM and generation of PWM wave

广东:剧本杀等新行业新业态场所,消防安全监管不再“缺位”

Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan

Introduction to box diagram

Regular expression of shell programming (grep of shell script text three swordsmen)
随机推荐
Newton-Raphson迭代法
Summary of leetcode SQL exercises (MySQL Implementation)
w.r.t. ; i.e.;etc.;e.g.是什么意思
[untitled] multimodal model clip
微商的差商近似
torch‘ has no attribute ‘inference_ mode‘
LeetCode 03: T58. 最后一个单词的长度(简单); 剑指 Offer 05. 替换空格(简单); 剑指 Offer 58 - II. 左旋转字符串(简单)
53 亿 BI 市场 TOP 10:帆软、微软、永洪、SAP、百度、IBM、SAS、思迈特、Salesforce、浪潮通软
N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
Arduino常见供电问题与解决
Leetcode 01: t1. sum of two numbers; T1108. IP address invalidation; T344. Reverse string
Makefile template
【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法
Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
STM32 compilation error: l6235e: more than one section matches selector - cannot all be first/l
Database cli tool docker image
解决方案:idea project没有显示树状图
w.r.t. ; i.e.; etc.; e. G. what does it mean
EfficientNet
Source code compilation and installation lamp