猿问

Golang time.Ticker 阻塞一段时间后触发两次

我正在使用time.Ticker一些阻塞代码,发现一个奇怪的行为:


func main() {

    ticker := time.NewTicker(1 * time.Second)


    i := 0

    for {

        select {

        case <-ticker.C:

            log.Println(i)

            if i == 0 {

                time.Sleep(5 * time.Second) // simulate the blocking

            }

            i++

        }

    }

}

示例输出:


2022/02/20 10:49:45 0

2022/02/20 10:49:50 1 <- shows at same time

2022/02/20 10:49:50 2 <- shows at same time

2022/02/20 10:49:51 3

2022/02/20 10:49:52 4

2022/02/20 10:49:53 5

...

“1”和“2”总是同时显示,这是为什么呢?


宝慕林4294392
浏览 255回答 2
2回答

动漫人物

代码创建一个容量为 1的缓冲通道。当通道已满时,自动收报机会丢弃时间值。这是问题中跟踪的时间表。同一时间列出的事件按列出的顺序依次发生一点点。1s - ticker sends value1s - main goroutine receives first value1s - main goroutine starts sleep for 5s2s - ticker sends value (channel has capacity 1).3s - ticker fails to send value because channel is full4s - ticker fails to send value because channel is full5s - ticker fails to send value because channel is full6s - main goroutine exits sleep and receives buffered value6s - ticker sends value6s - main goroutine receives buffered valueticker 和 main goroutine 跑到 6s 标记,因为 sleep 是 ticker 周期的倍数。在您的跟踪中,主 goroutine 赢得了比赛。在我的机器上,自动收报机赢得了比赛,我得到了你期望的时间。这是我机器上的样子。...5s - ticker fails to send value because channel is full6s - ticker fails to send value because channel is full6s - main goroutine receives buffered value7s - ticker sends value7s - main goroutine receives value自动收报机在操场上以 6 秒的成绩赢得比赛。 在这里运行它。当睡眠时间减少 1µs 时,主 goroutine 赢得了比赛。在这里运行它。当睡眠持续时间不是代码周期的倍数时,这很容易看出。以下是睡眠持续时间为 2.5 秒时发生的情况:1s - ticker sends value1s - main goroutine receives first value1s - main goroutine starts sleep for 2.5s2s - ticker sends value3s - ticker fails to send value because channel is full3.5s - main goroutine exits sleep and receives buffered value4s - ticker sends value4s - main goroutine receives value

慕的地8271018

在time.Ticker中,有一个chan Time.&nbsp;在NewTicker(d Duration) *Ticker函数中,它创建tickers时间通道作为容量为1的缓冲通道。在函数中,如下所述。// 给通道一个 1 元素的时间缓冲区。// 如果客户在阅读时落后了,我们将滴答声// 放在地板上,直到客户赶上。在您的情况下,时间每秒都会滴答并写入频道。睡眠时,ticker 向通道写入一次,循环继续后立即被 select case 接收并打印。在另外 4 秒内,通道被阻塞并且滴答声被丢弃。立即循环继续它被畅通并接受更多的通道滴答声。通过通道打印时间,您可以轻松了解行为。更新代码如下。func main() {&nbsp; &nbsp; ticker := time.NewTicker(1 * time.Second)&nbsp; &nbsp; i := 0&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case t := <-ticker.C:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println(i, t)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(5 * time.Second) // simulate the blocking&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:2022/02/20 08:47:58 0 2022-02-20 08:47:58.773375 +0530 +0530 m=+1.0042410042022/02/20 08:48:03 1 2022-02-20 08:47:59.772787 +0530 +0530 m=+2.0036669932022/02/20 08:48:03 2 2022-02-20 08:48:03.774272 +0530 +0530 m=+6.005207433 // printing time is same, but channel's received time has 4 sec difference.2022/02/20 08:48:04 3 2022-02-20 08:48:04.774203 +0530 +0530 m=+7.005151715
随时随地看视频慕课网APP

相关分类

Go
我要回答