新聞中心
?1.介紹
在 Go 語言標準庫 time? 包中的 Timer 類型,它是表示單一事件的計時器,也就是說它是一次性定時器。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供范縣網(wǎng)站建設、范縣做網(wǎng)站、范縣網(wǎng)站設計、范縣網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、范縣企業(yè)網(wǎng)站模板建站服務,十多年范縣做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
在 Go 語言項目開發(fā)中,定時器使用廣泛,本文我們介紹 Go 語言中怎么使用 Timer,以及它的實現(xiàn)原理。
2.使用方式
使用 Timer? 一次性定時器,需要導入 time 包,創(chuàng)建定時器的方式有兩種,分別是:
func NewTimer(d Duration) *Timer
使用 func NewTimer? 創(chuàng)建 Timer?,入?yún)⑹嵌〞r器的等待時間,時間到達時,發(fā)送當前時間到 channel中。
示例代碼:
func main() {
nowTime := time.Now().Unix()
fmt.Println(nowTime)
NewTimerDemo()
}
func NewTimerDemo() {
timer := time.NewTimer(2 * time.Second)
select {
case <-timer.C:
currentTime := time.Now().Unix()
fmt.Println(currentTime, "do something")
}
}輸出結果:
1665894136
1665894138 do something
通過閱讀上面這段代碼,我們可以發(fā)現(xiàn)我們定義了一個 2s? 后執(zhí)行的定時器 timer?,然后使用 select? 讀取 timer.C 中的數(shù)據(jù),當讀取到數(shù)據(jù)時,執(zhí)行特定業(yè)務邏輯代碼。
func AfterFunc(d Duration, f func()) *Timer
使用 func AfterFunc? 創(chuàng)建 Timer,入?yún)⑹嵌〞r器等待時間,和時間到達時執(zhí)行的函數(shù)。
示例代碼:
func main() {
nowTime := time.Now().Unix()
fmt.Println(nowTime)
AfterFuncDemo()
}
func AfterFuncDemo() {
time.AfterFunc(2 * time.Second, func() {
currentTime := time.Now().Unix()
fmt.Println(currentTime, "do something")
})
time.Sleep(3 * time.Second)
}閱讀上面這段代碼,細心的讀者朋友們可能已經(jīng)發(fā)現(xiàn),我們在代碼末尾使用 time.Sleep()?,這是因為 time.AfterFunc() 是異步執(zhí)行的,所以需要等待協(xié)成退出。
3.實現(xiàn)原理
我們在源碼中查看 Timer? 的數(shù)據(jù)結構,發(fā)現(xiàn)它包含兩個字段,其中一個是可導出字段 C?,這是一個 Time?類型的 chan?;另一個是不可導出字段 r?,這是一個 runtimeTimer 類型。
type Timer struct {
C <-chan Time
r runtimeTimer
}實際上,每個 Go 應用程序底層都會有一個特定的協(xié)程管理 Timer?,該協(xié)程(底層協(xié)程)監(jiān)控到某個 Timer? 指定的時間到達時,就會將當前時間發(fā)送到 C? 中,然后上層讀取到 C 中的數(shù)據(jù)時,執(zhí)行相關業(yè)務邏輯代碼。
底層協(xié)程監(jiān)控 Timer? 的 r? 字段中的數(shù)據(jù),我們在源碼中查看一下 runtimeTimer 的數(shù)據(jù)結構:
type runtimeTimer struct {
tb uintptr
i int
when int64
period int64
f func(interface{}, uintptr)
arg interface{}
seq uintptr
}閱讀上面這段代碼,我們可以發(fā)現(xiàn) runtimeTimer? 中包含的所有字段,我們重點了解 when、f? 和 arg。
- when:定時器執(zhí)行時間。
- f:定時器執(zhí)行的回調函數(shù)。
- arg:定時器執(zhí)行的回調函數(shù)的參數(shù)。
在簡單了解 Timer? 的數(shù)據(jù)結構之后,我們在源碼中查看一下 func NewTimer 的代碼:
// 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
}
閱讀上面這段代碼,我們可以發(fā)現(xiàn) func NewTimer? 的實現(xiàn)非常簡單,它實際上就是構造了一個 Timer?,然后把 Timer.r? 傳參給 startTimer()?,除了 startTimer()? 函數(shù)外,還有兩個函數(shù),分別是 when()?和 sendTime?,其中 when()? 是計算計時器的執(zhí)行時間,sendTime 是計時器時間到達時執(zhí)行的事件(實際上就是將當前時間寫入通道中)。
sendTime 源碼:
func sendTime(c interface{}, seq uintptr) {
// Non-blocking send of time on c.
// Used in NewTimer, it cannot block anyway (buffer).
// Used in NewTicker, dropping sends on the floor is
// the desired behavior when the reader gets behind,
// because the sends are periodic.
select {
case c.(chan Time) <- Now():
default:
}
}我們已經(jīng)了解到,func NewTimer? 將構造的 Timer.r? 傳參給 startTimer()?,它負責把 runtimeTimer?寫入底層協(xié)程的數(shù)組中(如果底層協(xié)程未運行,它將會啟動底層協(xié)程),將 Timer? 交給底層協(xié)程監(jiān)控,也就是上面講到的,當?shù)讓訁f(xié)程監(jiān)控到某個 Timer 指定時間到達時,將當前時間發(fā)送到它的通道中。
// startTimer adds t to the timer heap.
//go:linkname startTimer time.startTimer
func startTimer(t *timer) {
if raceenabled {
racerelease(unsafe.Pointer(t))
}
addtimer(t)
}
4.總結
本文我們介紹 Go 語言標準庫 time? 包提供的一次性定時器 Timer,不僅介紹了它的使用方式,還介紹了它的實現(xiàn)原理。
網(wǎng)站欄目:Go語言一次性定時器使用方式和實現(xiàn)原理
鏈接地址:http://www.dlmjj.cn/article/cdihpso.html


咨詢
建站咨詢
