停止计时器会结束 goroutine 吗?
Does stopping a timer end the goroutine?
像下面代码这样通过停止计时器停止的简单 goroutine 是否变得干净?还是留在内存中直到程序结束?
go func() {
<-timer1.C //Will stop before firing.
fmt.Println("Timer 1 fired")
}()
如果通道关闭,Go 例程将退出。如果通道保持打开但没有任何东西通过它发送,那么 Go 例程将永远“挂起”,直到程序退出。
如果 timer1
是 time.Timer
,那么 Stop
将 不会 关闭频道,如文档中所述:https://pkg.go.dev/time#Timer.Stop
像下面代码这样通过停止计时器停止的简单 goroutine 是否变得干净?还是留在内存中直到程序结束?
go func() {
<-timer1.C //Will stop before firing.
fmt.Println("Timer 1 fired")
}()
如果通道关闭,Go 例程将退出。如果通道保持打开但没有任何东西通过它发送,那么 Go 例程将永远“挂起”,直到程序退出。
如果 timer1
是 time.Timer
,那么 Stop
将 不会 关闭频道,如文档中所述:https://pkg.go.dev/time#Timer.Stop