当前位置:网站首页>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
边栏推荐
- C1000k TCP connection upper limit test
- Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)
- ArcMap connecting ArcGIS Server
- arcgis js api dojoConfig配置
- Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
- SSDB foundation 2
- MySQL syntax collation (5) -- functions, stored procedures and triggers
- Audio signal processing and coding - 2.5.3 the discrete cosine transform
- 【玩转Lighthouse】腾讯云轻量服务器搭建全平台视频解析视频下载网站
- Openlayers 5.0 thermal diagram
猜你喜欢

Is meituan, a profit-making company with zero foundation, hungry? Coupon CPS applet (with source code)

White screen processing method of fulter startup page

Application of DCT transform

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

Keysight has chosen what equipment to buy for you

binlog2sql 工具安装使用及问题汇总

Redis optimization series (III) solve common problems after master-slave configuration

Class loading process of JVM

Openharmony open source developer growth plan, looking for new open source forces that change the world!
随机推荐
Wechat video extraction and receiving file path
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
Openlayers 5.0 reload the map when the map container size changes
Installation, use and problem summary of binlog2sql tool
openlayers draw矩形
Codeforces Round #784 (Div. 4)
@Analysis of conditional on Web Application
SSDB基础2
FTP, SSH Remote Access and control
开关电源设计分享及电源设计技巧图解
SSDB foundation 2
Some records used by VS2010
Translation of audio signal processing and coding: Preface
UML类图几种关系的总结
SSDB基础
C1000k TCP connection upper limit test
HTTP cache - HTTP authoritative guide Chapter VII
C1000k TCP connection upper limit test 1
openlayers 5.0 离散聚合点
坐标转换WGS-84 转 GCJ-02 和 GCJ-02转WGS-84