当前位置:网站首页>Golang timer
Golang timer
2022-04-23 19:21:00 【Handsome that handsome】
timer timer
- Used in the specified Duration After the type time, the function is called or the expression is calculated .
- If you just want to execute after a specified time , Use time.Sleep()
- Use NewTimer(), Can return Timer Type before timer expires , Cancel the timer
- Until you use <-timer.C Send a value , This timer will expire
Define counters
timer := time.NewTimer(time.Second * 2)
Stop timer
timer.Stop()
Mandatory modification timer The time specified in
timer.Reset(0)
ticker Periodic ticker
Call a function or evaluate an expression according to a specified cycle .
You usually use go Open a new thread , Otherwise
Define a ticker
ticker := time.NewTicker(time.Millisecond * 500)
stop it ticker
ticker.Stop()
Ticker Trigger
For t := range ticker.C {
//do something
}
demo
package main
import (
"fmt"
"time"
"sync"
)
func main() {
ticker := time.NewTicker(5 * time.Second)
quit := make(chan int)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println("child goroutine bootstrap start")
for {
select {
case <- ticker.C:
fmt.Println("ticker .")
case <- quit:
fmt.Println("work well .")
ticker.Stop()
return
}
}
fmt.Println("child goroutine bootstrap end")
}()
time.Sleep(10 * time.Second)
quit <- 1
wg.Wait()
}
版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557450985.html
边栏推荐
- Codeworks round 783 (Div. 2) d problem solution
- On the forced conversion of C language pointer
- MySQL syntax collation (2)
- Openlayers 5.0 discrete aggregation points
- Application of DCT transform
- arcgis js api dojoConfig配置
- ArcMap connecting ArcGIS Server
- 5 minutes to achieve wechat cloud applet payment function (including source code)
- [advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
- Translation of audio signal processing and coding: Preface
猜你喜欢
Switching power supply design sharing and power supply design skills diagram
Common SQL commands
Oracle configuration st_ geometry
The fifth bullet of MySQL learning -- detailed explanation of transaction and its operation characteristics
Using Visual Studio code to develop Arduino
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Oracle配置st_geometry
Matlab 2019 installation of deep learning toolbox model for googlenet network
redis优化系列(三)解决主从配置后的常见问题
2021-2022-2 ACM training team weekly Programming Competition (8) problem solution
随机推荐
Codeforces Round #784 (Div. 4)
Openlayers 5.0 reload the map when the map container size changes
Using 8266 as serial port debugging tool
Easy mock local deployment (you need to experience three times in a crowded time. Li Zao will do the same as me. Love is like a festival mock)
[报告] Microsoft :Application of deep learning methods in speech enhancement
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
Sword finger offer II 116 Number of provinces - spatial complexity O (n), time complexity O (n)
Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
Switching power supply design sharing and power supply design skills diagram
Zero base to build profit taking away CPS platform official account
MySQL syntax collation (4)
Zero cost, zero foundation, build profitable film and television applet
SSDB基础3
Oracle configuration st_ geometry
Core concepts of rest
UML类图几种关系的总结
Android Development: the client obtains the latest value in the database in real time and displays it on the interface
Codeforces Round #784 (Div. 4)
Openlayers 5.0 loading ArcGIS Server slice service