日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Go学习笔记—定时器、打点器

發布時間:2023/12/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Go学习笔记—定时器、打点器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Go學習筆記—定時器、打點器

1、定時器

定時器timer的功能是讓一段Go代碼在指定的時間段后運行一次。

// 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 Timer runtimeTimer }
  • 定時器類型表示單個事件。
  • 定時器過期時,會通過通道C返回當前時間,除非定時器由AfterFunc創建。
  • 必須使用NewTimer或者AfterFunc創建定時器

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 }

AfterFunc等待持續時間結束,然后在自己的goroutine中調用f,f返回一個定時器,可使用Stop()方法取消調用。

// 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.Cfmt.Println("Timer 2 expired")}()stop2 := time2.Stop()if stop2 {fmt.Println("Timer 2 stopped")} }//控制臺立即輸出 //Timer 2 stopped

此例中,如果沒有使用協程來控制timer2接收失效信息,控制臺會等待4秒輸出Timer 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 }

NewTicker創建一個打點器,它將定時執行。NewTicker使用方法如下:

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 }
  • NewTicker返回一個打點器,包含一個發送每次打點的時間的通道。
  • 打點時間由生成ticker傳入的參數決定。
  • 打點器會調整時間或下降打點次數,來彌補接受速度慢的問題。
  • 接收的時間參數必須大于0,否則會返回錯誤信息。
  • 使用完畢,要關閉打點器ticker以節約資源。

創建一個對象在,間隔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

總結

以上是生活随笔為你收集整理的Go学习笔记—定时器、打点器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。