【发布时间】:2015-03-02 01:04:23
【问题描述】:
package main
import (
//"time"
"runtime"
"fmt"
)
func main() {
//time.Sleep(100 * time.Millisecond)//By adding this number of goroutine increases
fmt.Println(runtime.NumGoroutine())
}
我正在尝试找出程序中 goroutine 的数量。我的code is here。在编写代码时,我注意到 goroutines 的默认数量是 4。
对我来说:
- main 是一个 goroutine
- 垃圾收集器是一个 goroutine
其他的呢?
通过添加 time.Sleep(上图),goroutine 的数量增加到 5 个。这是什么原因?
【问题讨论】:
-
你可以使用 SIGQUIT(ctrl+\) 打印出所有的 goroutines。
-
@chendesheng:感谢您的建议,但它没有回答我的问题,为什么上面的程序有 4 个 goroutine,当我添加 Sleep 时,为什么数字会增加到 5?