引言

一般来说,很多时候我们面临这样一种情况,即我们需要运行时间记录器,它不断向我们显示当前时间或在给定的时间间隔内保持执行一定的代码和平,在这种情况下,我们应该使用 ​​Ticker​​​,使用这个我们需要使用 go 语言的 ​​time​​​ 包,我们有一个名为 ​​NewTicker()​​​ 的方法,它允许我们停止和启动时间代码,我们需要通过传递 ​​chan​​​ 和 ​​bool​​ 作为将使用的参数来创建一个代码通道检查它是否打开,如果通道打开意味着计时器将继续。

Go 的计时器

Go 提供了非常简单的语法来实现一个计时器,定时器的结构体定义:

type Ticker struct {  C <-chan Time // 抛出来的channel,给上层系统使用,实现计时  r runtimeTimer // 给系统管理使用的定时器,系统通过该字段确定定时器是否到时,如果到时,调用对应的函数向C中推送当前时间。  }

Ticker对外仅暴露一个 channel,指定的时间到来时就往该 channel 中写入系统时间,也即一个事件。

Ticker 的使用方式也很简单,代码如下:

import time  TimeTicker := time.NewTicker(1 * time.Second)  TimeTicker.Stop()
  • ​time​​​ : 如果想要使用​​time​​ 中的计时器,那么需要将其导入代码中
  • ​NewTicker()​​​ 函数:这个很好理解,新建一个计时器,然后该计时器以时间表达式作为参数,比如一秒​​1*time.Second​​​ ,而不是直接传入​​1​​​ ,也可以传入其他时间方式,比如​​2*time.Millisecond​
  • 最后,我们可以使用时间表达式调用由​​NewTicker​​​ 函数创建的变量上的任何活动。例如,在上面的语法中,我们使用了​​TimeTicker.Stop()​​​ 来停止时间计时器。我们可以在特定条件下使用它,比如定义倒数 10 秒的计时器,就可以检查时间是否满足 10 秒,一旦 10 秒发生,我们可以调用​​TimeTicker.stop()​​。

我们可以每隔一秒输出一个结果,比如实现一个倒数 10 个数的功能:

package main  import (  "fmt"  "time"  )  func main() {  TimeTicker := time.NewTicker(1 * time.Second)  i := 10  for {  <-TimeTicker.C  fmt.Println("i = ", i)  i--  if i == 0 {  TimeTicker.Stop()  break  }  }  }

然后执行该程序:

$ go run main.go
i = 10
i = 9
i = 8
i = 7
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1

Ticker 计时器是如何工作的?

​NewTicker​​​创建的计时器与​​NewTimer​​​创建的计时器持有的时间​​channel​​​一样都是带一个缓存的​​channel​​​,每次触发后执行的函数也是​​sendTime​​​,这样即保证了无论有误接收方​​Ticker​​触发时间事件时都不会阻塞:

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  }

NewTicker()只是构造了一个Ticker,然后把Ticker.r通过startTimer()交给系统协程维护。

其中period为事件触发的周期。

其中​​sendTime()​​方法便是定时器触发时的动作:

func sendTime(c interface{}, seq uintptr) {  select {  case c.(chan Time) <- Now():  default:  }  }

​sendTime​​ 接收一个管道作为参数,其主要任务是向管道中写入当前时间。

停止Ticker:

停止Ticker,只是简单的把Ticker从系统协程中移除。

函数主要实现如下:

func (t *Ticker) Stop() {  stopTimer(&t.r)  }

​stopTicker()​​ 即通知系统协程把该 Ticker 移除,即不再监控。系统协程只是移除 Ticker 并不会关闭管道,以避免用户协程读取错误。

Ticker 使用方式

例子一:

package main  import (  "fmt"  "time"  )  func main() {  TimeTicker := time.NewTicker(3 * time.Second)  tickerChannel := make(chan bool)  go func() {  for {  select {  case timeticker := <-TimeTicker.C:  fmt.Println("The time for current is : ", timeticker)  case <-tickerChannel:  return  }  }  }()  time.Sleep(6 * time.Second)  TimeTicker.Stop()  tickerChannel <- true  fmt.Println("Time for running ticker is completed")  }

运行该代码:

$ go run main.go
The time for current is : 2022-04-29 22:37:13.93862 +0800 CST m=+3.000267823
The time for current is : 2022-04-29 22:37:16.939081 +0800 CST m=+6.000707515
Time for running ticker is completed

例子二:

package main  import (  "fmt"  "time"  )  func main() {  tm := time.Millisecond  tickerTicker := time.NewTicker(400 * tm)  tickerChaneel := make(chan bool)  go func() {  for {  select {  case <-tickerChaneel:  return  case tmtr := <-tickerTicker.C:  fmt.Println("Ticker time at current is", tmtr)  }  }  }()  time.Sleep(1400 * time.Millisecond)  tickerTicker.Stop()  tickerChaneel <- true  fmt.Println("Ticker has stopped now")  }

运行该代码:

$ go run main.go
Ticker time at current is 2022-04-29 22:39:51.13057 +0800 CST m=+0.400159916
Ticker time at current is 2022-04-29 22:39:51.531516 +0800 CST m=+0.801102997
Ticker time at current is 2022-04-29 22:39:51.931238 +0800 CST m=+1.200822301
Ticker has stopped now

总结

本文简单了解了 go 计时器 ​​Ticker​​​ 的基本概念,介绍了它的工作原理,并且我们专注于 go 语言中 ​​ticker​​ 的语法和使用。最后展现了 go 语言中的一些重要示例。