当前位置:网站首页>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
边栏推荐
猜你喜欢

Getting started with vcpkg

开关电源设计分享及电源设计技巧图解

MySQL syntax collation (2)

Oracle配置st_geometry

White screen processing method of fulter startup page

Reflection on the performance of some OpenGL operations in the past

Openharmony open source developer growth plan, looking for new open source forces that change the world!

MySQL syntax collation

Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy

First experience of using fluent canvas
随机推荐
[advanced level 11 of C language -- character and string functions and their simulation implementation (2)]
JS to get the local IP address
Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
Why is PostgreSQL about to surpass SQL Server?
JVM的类加载过程
该买什么设备,Keysight 给你挑好了
Openlayers draw rectangle
Common processing of point cloud dataset
Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
c1000k TCP 连接上限测试
Codeforces Round #784 (Div. 4)
SSDB Foundation
Solve the problem of invalid listview Click
Problems caused by flutter initialroute and home
网络协议之:sctp流控制传输协议
ArcMap connecting ArcGIS Server
MySQL practical skills
Translation of audio signal processing and coding: Preface
开关电源设计分享及电源设计技巧图解
Summary of several relationships of UML class diagram