定时器设计

定时器实现方案

1、最小堆 golang的定时器

https://github.com/polaris1119/The-Golang-Standard-Library-by-Example/blob/master/chapter04/04.4.md

用法

fmt.Println(time.Now())

f := func(){
    fmt.Println(time.Now(),"hello world")
}
time.AfterFunc(time.Second,f)

time.Sleep(2*time.Second)
fmt.Println(time.Now())
/*
2021-01-31 12:10:31.158899599 +0800 CST m=+0.000195036
2021-01-31 12:10:32.159610127 +0800 CST m=+1.000905972 hello world
2021-01-31 12:10:33.159661235 +0800 CST m=+2.000957358
*/

time.AfterFunc定义在src/time/sleep.go

// 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
}

startTimer定义在src/runtime/time.go

// startTimer adds t to the timer heap.
//go:linkname startTimer time.startTimer
func startTimer(t *timer) {
	//...
	addtimer(t)
    //...
}

// addtimer adds a timer to the current P.
// This should only be called with a newly created timer.
// That avoids the risk of changing the when field of a timer in some P's heap,
// which could cause the heap to become unsorted.
func addtimer(t *timer) {
	//...
	doaddtimer(pp, t)
	//...
}

// doaddtimer adds t to the current P's heap.
// The caller must have locked the timers for pp.
func doaddtimer(pp *p, t *timer) {
	//...
	siftupTimer(pp.timers, i)
	//...
}

最小堆,不同于平常堆排序用的二叉堆,go用的是4叉堆。定义在src/runtime/time.go

// Heap maintenance algorithms.
// These algorithms check for slice index errors manually.
// Slice index error can happen if the program is using racy
// access to timers. We don't want to panic here, because
// it will c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值