当前位置:网站首页>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
边栏推荐
- Common SQL commands
- Solve the problem of invalid listview Click
- js 计算时间差
- One stop service platform for high-level talents and development of comprehensive service platform system for talents
- Matlab 2019 installation of deep learning toolbox model for googlenet network
- JS calculation time difference
- [记录]TypeError: this.getOptions is not a function
- [play with lighthouse] Tencent cloud lightweight server builds a full platform video analysis video download website
- binlog2sql 工具安装使用及问题汇总
- ArcMap publishing slicing service
猜你喜欢
Client interns of a large factory share their experience face to face
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
Android Development: the client obtains the latest value in the database in real time and displays it on the interface
Use of fluent custom fonts and pictures
ArcMap connecting ArcGIS Server
Zero base to build profit taking away CPS platform official account
Wechat video extraction and receiving file path
8266 obtain 18b20 temperature
2022.04.23 (lc_763_divided into letter interval)
I just want to leave a note for myself
随机推荐
SSDB基础1
The difference between ordinary inner class and static inner class
[报告] Microsoft :Application of deep learning methods in speech enhancement
MySQL syntax collation (5) -- functions, stored procedures and triggers
JS to get the local IP address
Raspberry pie 18b20 temperature
[transfer] summary of new features of js-es6 (one picture)
Customize the non slidable viewpage and how to use it
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
開關電源設計分享及電源設計技巧圖解
Convert string to JSON
Encyclopedia of professional terms and abbreviations in communication engineering
redis优化系列(三)解决主从配置后的常见问题
The platinum library cannot search the debug process records of some projection devices
SSDB基础
Gossip: on greed
SSDB基础3
Summary of several relationships of UML class diagram
I just want to leave a note for myself
Thoughts on the optimization of examination papers in the examination system