1 回答
TA贡献1827条经验 获得超8个赞
在写问题时,我记得 Golang 源代码非常容易阅读……并且认为我应该在看起来很傻之前先看一眼。我对我的发现很满意 :)
如果报价阅读器落后,它将开始丢弃报价,而不是阻塞写入通道(缓冲区只有 1)。这样做的效果是在我们错过一个或多个滴答时立即让事情回到“正轨”。
示例证明:
package main
import (
"fmt"
"time"
)
func main() {
t := time.NewTicker(time.Millisecond * 50)
for i := 0; i < 10; i++ {
fmt.Printf("New invocation starting at %dms\n", time.Now().Round(time.Millisecond).Nanosecond()/int(time.Millisecond))
if i%3 == 0 {
fmt.Println("Executing for 25ms")
time.Sleep(time.Millisecond * 25)
} else {
fmt.Println("Executing for 75ms")
time.Sleep(time.Millisecond * 75)
}
fmt.Println("Waiting for ticker...")
<-t.C
}
t.Stop()
}
输出:
New invocation starting at 0ms
Executing for 25ms
Waiting for ticker...
New invocation starting at 50ms
Executing for 75ms
Waiting for ticker...
New invocation starting at 125ms
Executing for 75ms
Waiting for ticker...
New invocation starting at 200ms
Executing for 25ms
Waiting for ticker...
New invocation starting at 250ms
Executing for 75ms
Waiting for ticker...
New invocation starting at 325ms
Executing for 75ms
Waiting for ticker...
New invocation starting at 400ms
Executing for 25ms
Waiting for ticker...
New invocation starting at 450ms
Executing for 75ms
Waiting for ticker...
New invocation starting at 525ms
Executing for 75ms
Waiting for ticker...
New invocation starting at 600ms
Executing for 25ms
Waiting for ticker...
- 1 回答
- 0 关注
- 150 浏览
添加回答
举报