怎么用Go实现计时器

发布时间:2022-05-05 09:12:37 作者:iii
来源:亿速云 阅读:113

这篇文章主要介绍“怎么用Go实现计时器”,在日常操作中,相信很多人在怎么用Go实现计时器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Go实现计时器”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

引言

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

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()

我们可以每隔一秒输出一个结果,比如实现一个倒数 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实现计时器”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. JS实现简易计时器
  2. js如何实现秒表计时器

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

go

上一篇:Go语言的net和url包怎么用

下一篇:Go中的字符串应用实例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》