Go学习笔记—定时器、打点器
1、定时器
timer
// The Timer type represents a single event.
// When the Timer expires, the current time will be sent on C,
// unless the Timer was created by AfterFunc.
// A Timer must be created with NewTimer or AfterFunc.
type Timer struct {
C <-chan Time
r runtimeTimer
}
AfterFuncNewTimerAfterFunc
NewTimer
使用方法如下:
timer := time.NewTimer(time.Second * 4)
//用变量接收一个传入时间值的方法产生的对象
源码如下:
// NewTimer creates a new Timer that will send
// the current time on its channel after at least duration d.
func NewTimer(d Duration) *Timer {
c := make(chan Time, 1)
t := &Timer{
C: c,
r: runtimeTimer{
when: when(d),
f: sendTime,
arg: c,
},
}
startTimer(&t.r)
return t
}
AfterFuncgoroutineffStop()
// AfterFunc waits for the duration to elapse and then calls f
// in its own goroutine. It returns a Timer that can
// be used to cancel the call using its Stop method.
func AfterFunc(d Duration, f func()) *Timer {
t := &Timer{
r: runtimeTimer{
when: when(d),
f: goFunc,
arg: f,
},
}
startTimer(&t.r)
return t
}
//f 是一个函数类型,调用时传入goFunc
func goFunc(arg interface{}, seq uintptr) {
go arg.(func())()
}
一个定时器,指在持续一段时间后某一时刻的独立事件。会通过一个通道进行感知。在通道接收到失效信息之前,会一直处于阻塞状态。
func main(){
timer1 := time.NewTimer(time.Second * 4) //创建一个4秒后失效的定时器
<- timer1.C //用于感知的通道
fmt.Println("Timer 1 expired") //输出提示信息
}
//控制台四秒后输出信息
//Timer 1 expired
time.Sleep
func main(){
timer2 := time.NewTimer(time.Second * 4)
go func(){
<-timer2.C
fmt.Println("Timer 2 expired")
}()
stop2 := time2.Stop()
if stop2 {
fmt.Println("Timer 2 stopped")
}
}
//控制台立即输出
//Timer 2 stopped
timer2Timer 2 expired
2、打点器
ticker
// A Ticker holds a channel that delivers ``ticks'' of a clock
// at intervals.
type Ticker struct {
C <-chan Time // The channel on which the ticks are delivered.
r runtimeTimer
}
NewTickerNewTicker
ticker := time.NewTicker(time.Second * 1)
//用变量接收一个传入时间值的方法产生的对象
源码如下:
// NewTicker returns a new Ticker containing a channel that will send
// the time on the channel after each tick. The period of the ticks is
// specified by the duration argument. The ticker will adjust the time
// interval or drop ticks to make up for slow receivers.
// The duration d must be greater than zero; if not, NewTicker will
// panic. Stop the ticker to release associated resources.
func NewTicker(d Duration) *Ticker {
if d <= 0 {
panic(errors.New("non-positive interval for NewTicker"))
}
// Give the channel a 1-element time buffer.
// If the client falls behind while reading, we drop ticks
// on the floor until the client catches up.
c := make(chan Time, 1)
t := &Ticker{
C: c,
r: runtimeTimer{
when: when(d),
period: int64(d),
f: sendTime,
arg: c,
},
}
startTimer(&t.r)
return t
}
NewTickertickerticker
创建一个对象在,间隔1秒进行一次打点操作,返回打点时间。
func main(){
ticker := time.NewTicker(time.Second * 1)
go func(){
for t := range ticker.C{ //从通道中获取时间值
fmt.Println("Tick at",t)
}
}()
time.Sleep(time.second * 3) //模拟函数程序时间
ticker.Stop()
fmt.Println("Ticker stopped")
}
//每间隔一秒,输出一条结果
//Tick at 2021-08-16 15:47:48.9317032 +0800 CST m=+1.002859901
//Tick at 2021-08-16 15:47:49.9427927 +0800 CST m=+2.013949401
//Ticker stopped