为编程爱好者分享易语言教程源码的资源网
好用的代理IP,游戏必备 ____广告位招租____ 服务器99/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器
好用的代理IP,游戏必备 ____广告位招租____ 服务器低至38/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器

网站首页 > 网络编程 > 其它综合 正文

golang定时器介绍(golang定时器实现)

三叶资源网 2022-10-17 19:18:50 其它综合 157 ℃ 0 评论

1、time.After()

调用time.After(duration)之后,会返回一个time.Time类型的chan,不会阻塞程序的继续执行。等到指定duration时间后,会自动发送一个当前时间到chan,其底层是使用的NewTimer

// 函数原型
// After waits for the duration to elapse and then sends the current time
// on the returned channel.
// It is equivalent to NewTimer(d).C.
// The underlying Timer is not recovered by the garbage collector
// until the timer fires. If efficiency is a concern, use NewTimer
// instead and call Timer.Stop if the timer is no longer needed.
func After(d Duration) <-chan Time {
	return NewTimer(d).C
}
// 示例
func main() {
	duration := time.Second * 5
	t := time.After(duration)
	// 不阻塞,打印当前时间
	fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
	// 过了设定时间duration(5s)之后,往t中发送当前时间
	fmt.Println((<-t).Format("2006-01-02 15:04:05"))
}

// 输出
2021-03-30 09:11:55
2021-03-30 09:12:00

#扩展(结合select实现超时处理)

select语句用来选择哪个case中的发送或接收操作可以被立即执行。它类似于switch语句,但是它的case涉及到channel相关的I/O操作

select语句不是循环,它只会选择一个case来处理,如果想一直处理channel,可以加一个无限的for循环

如果没有case需要处理,select语句就会一直阻塞。这时候就需要一个超时控制,用来处理超时的情况。

// 示例
func main() {
	c := make(chan string)
	go func() {
		// 3s后向channel c 中发送一条数据
		time.Sleep(time.Second * 3)
		c <- "send"
	}()
	select {
	case res := <-c:
		fmt.Println(res)
	case <-time.After(time.Second): // select设置超时时间为1s,所以会先打印出timeout
		fmt.Println("timeout")
	}
}

// 输出
timeout

2、time.Tick()

Tick函数是使用channel阻塞当前的协程,用于完成定时任务的执行

// 函数原型
// Tick is a convenience wrapper for NewTicker providing access to the ticking
// channel only. While Tick is useful for clients that have no need to shut down
// the Ticker, be aware that without a way to shut it down the underlying
// Ticker cannot be recovered by the garbage collector; it "leaks".
// Unlike NewTicker, Tick will return nil if d <= 0.
func Tick(d Duration) <-chan Time {
	if d <= 0 {
		return nil
	}
	return NewTicker(d).C
}
// 示例
func main() {
    // 每隔1s向channel c 中发送当前时间
    c := time.Tick(time.Second)
		for t := range c {
			fmt.Println(t.Format("2006-01-02 15:04:05"))
	}
}

3、time.Sleep()

睡眠指定时间后继续执行下面的任务,Sleep会阻塞当前协程

// 函数原型
// Sleep pauses the current goroutine for at least the duration d.
// A negative or zero duration causes Sleep to return immediately.
func Sleep(d Duration)
// 示例
func {
    for {
			fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
			time.Sleep(time.Second)
	}
}

// 输出
2021-03-30 11:08:53
2021-03-30 11:08:54
2021-03-30 11:08:55

4、time.Ticker

ticker是一个定时触发的计时器,它会以一个间隔往channel发送一条数据(当前时间),channel接收者可以固定的时间间隔从channel中读取数据

可以通过Stop方法来停止。一旦停止,接收者不会再从channel中接收数据了

// 函数原型
// 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
}
// 示例
func {
    ticker := time.NewTicker(time.Second)
    for t := range ticker.C {
	      fmt.Println(t.Format("2006-01-02 15:04:05"))
    }
}

// 输出
2021-03-30 11:46:48
2021-03-30 11:46:49
2021-03-30 11:46:50
2021-03-30 11:46:51

来源:三叶资源网,欢迎分享,公众号:iisanye,(三叶资源网⑤群:21414575

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

百度站内搜索
关注微信公众号
三叶资源网⑤群:三叶资源网⑤群

网站分类
随机tag
皮肤制作文本转义minidump易语言滑动验证码notepd++插件模板吃鸡泰服矩阵文件夹加密LocalStorage蓝牙类库后台跳绳微信加人卡密生成系统进程通讯APP加密内存国密算法百度自动回复云外归鸟Web浏览器11111
最新评论