【问题标题】:Does stopping a timer end the goroutine?停止计时器会结束 goroutine 吗?
【发布时间】:2021-11-05 06:27:05
【问题描述】:

像下面这样通过停止计时器停止的简单 goroutine 是否变得干净?还是留在内存中直到程序结束?

go func() {
    <-timer1.C //Will stop before firing.
    fmt.Println("Timer 1 fired")
}()

【问题讨论】:

  • 将任何事情视为“扼杀”围棋程序是不正确的。它们在执行流程到达方法末尾时自然退出,不会被任何外部进程“杀死”。
  • 感谢@meagar 已更正。

标签: go memory concurrency timer goroutine


【解决方案1】:

如果通道关闭,Go 例程将退出。如果通道保持打开但没有发送任何内容,那么 Go 例程将永远“挂起”,直到程序退出。

如果timer1time.Timer,那么Stop不会关闭通道,如文档中所述:https://pkg.go.dev/time#Timer.Stop

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多