定时器
在我们项目中,常常会有这样的场景,比如到了未来某一时刻,需要某个逻辑或者某个任务执行一次,或者是周期性的的执行多次,有点类似定时任务。这种场景就需要用到定时器,Go语言中也内置了定时器的实现,timer和ticker。
Timer
Timer是一种一次性时间定时器,即在未来某个时刻,触发的事件只会执行一次。
Timer的结构定义
| Go |
|---|
| type Timer struct {
C <-chan Time
r runtimeTimer
}
|
Timer结构里有一个Time类型的管道C,主要用于事件通知。在未到达设定时间的时候,管道内没有数据写入,一直处于阻塞状态,到达设定时间后,会向管道内写入一个系统时间,触发事件。
创建Timer
| Go |
|---|
| func NewTimer(d Duration) *Timer
|
使用示例:
| Go |
|---|
| package main
import (
"fmt"
"time"
)
func main() {
timer := time.NewTimer(2 * time.Second) //设置超时时间2s
<-timer.C
fmt.Println("after 2s Time out!")
}
|
运行结果:
程序在2s后打印"after 2s Time out!",因为创建了一个定时器timer,设置了超时时间为2s,执行<-timer.C会一直阻塞,直到2s后,程序继续执行。
停止Timer
| Go |
|---|
| func (t *Timer) Stop() bool
|
返回值:
true:执行stop()时timer还没有到达超时时间,即超时时间内停止了timer
false:执行stop()时timer到达了超时时间,过了超时时间才停止timer
使用示例:
| Go |
|---|
| package main
import (
"fmt"
"time"
)
func main() {
timer := time.NewTimer(2 * time.Second) //设置超时时间2s
res := timer.Stop()
fmt.Println(res)
}
|
运行结果:
重置Timer
| Go |
|---|
| func (t *Timer) Reset(d Duration) bool
|
对于已经过期或者是已经停止的timer,可以通过重置方法激活使其继续生效。
使用示例:
| Go |
|---|
| package main
import (
"fmt"
"time"
)
func main() {
timer := time.NewTimer(time.Second * 2)
<-timer.C
fmt.Println("time out1")
res1 := timer.Stop()
fmt.Printf("res1 is %t\n", res1)
timer.Reset(time.Second * 3)
res2 := timer.Stop()
fmt.Printf("res2 is %t\n", res2)
}
|
运行结果:
| Text Only |
|---|
| time out1
res1 is false
res2 is true
|
程序2s之后打印"time out1",此时timer已经过期了,所以res1的值为false,接下来执行timer.Reset(time.Second * 3)又使timer生效了,并且重设超时时间为3s,但是紧接着执行了timer.Stop(),还未到超时时间,所以res2的值为true。
time.AfterFunc
方法定义:
| Go |
|---|
| func AfterFunc(d Duration, f func()) *Timer
|
time.AfterFunc参数为超时时间d和一个具体的函数f,返回一个Timer的指针,作用在创建出timer之后,在当前goroutine,等待一段时间d之后,将执行f。
使用示例:
| Go |
|---|
| package main
import (
"fmt"
"time"
)
func main() {
duration := time.Duration(1) * time.Second
f := func() {
fmt.Println("f has been called after 1s by time.AfterFunc")
}
timer := time.AfterFunc(duration, f)
defer timer.Stop()
time.Sleep(2 * time.Second)
}
|
运行结果:
| Text Only |
|---|
| f has been called after 1s by time.AfterFunc
|
1s之后打印语句。
time.After
方法定义:
| Go |
|---|
| func After(d Duration) <-chan Time {
return NewTimer(d).C
}
|
根据函数定义可以看到,after函数会返回timer里的管道,并且这个管道会在经过时段d之后写入数据,调用这个函数,就相当于实现了定时器。一般time.After会配合select一起使用。
使用示例如下:
| Go |
|---|
| package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan string)
go func() {
time.Sleep(time.Second * 3)
ch <- "test"
}()
select {
case val := <-ch:
fmt.Printf("val is %s\n", val)
case <-time.After(time.Second * 2):
fmt.Println("timeout!!!")
}
}
|
运行结果:
程序创建了一个管道ch,并且在主goroutine用select监听两个管道,一个是刚刚创建的ch,一个是time.After函数返回的管道c,ch管道3s之后才会有数据写入,而time.After函数是2s超时,所以2s后就会有数据写入,这样select会先收到管道c里的数据,执行timeout退出。
Ticker
Ticker创建
方法定义如下:
| Go |
|---|
| func NewTicker(d Duration) *Ticker
|
NewTicker用于返回一个Ticker对象。
Ticker对象定义
| Go |
|---|
| type Ticker struct {
C <-chan Time // The channel on which the ticks are delivered.
r runtimeTimer
}
|
Ticker对象的字段和Timer是一样的,也包含一个通道字段,并会每隔时间段d就向该通道发送当时的时间,根据这个管道消息来触发事件,但是ticker只要定义完成,就从当前时间开始计时,每隔固定时间都会触发,只有关闭Ticker对象才不会继续发送时间消息。
使用示例:
| Go |
|---|
| package main
import (
"fmt"
"time"
)
func Watch() chan struct{} {
ticker := time.NewTicker(1 * time.Second)
ch := make(chan struct{})
go func(ticker *time.Ticker) {
defer ticker.Stop()
for {
select {
case <-ticker.C:
fmt.Println("watch!!!")
case <-ch:
fmt.Println("Ticker Stop!!!")
return
}
}
}(ticker)
return ch
}
func main() {
ch := Watch()
time.Sleep(5 * time.Second)
ch <- struct{}{}
close(ch)
}
|
运行结果:
| Text Only |
|---|
| watch!!!
watch!!!
watch!!!
watch!!!
watch!!!
Ticker Stop!!!
|
Watch函数里创建一个ticker,将它传递到子goroutine函数,每隔1s打印"watch!!!",主函数创建一个管道ch,通过ch来控制go func()函数的退出,在5s之后主函数发送一个信号到ch,watch函数select收到ch信号,将return,在return之前将执行defer ticker.Stop()语句关闭ticker。在这5s之间,select将每个1s收到ticker.C管道里的消息,打印"watch!!!"。
注意:调用ticker.Stop()只会停止ticker,不会再继续向ticker.C中发送新的时间值,但这并不会关闭ticker.C这个管道,所以我们需要用这个channel来控制watch函数中的goroutine能够退出,否则会一直阻塞。