转到文档说:Go运行时如何检查goroutine是否被阻止?
当协程块,如通过调用拦截系统调用,所以他们赢得了运行时自动移动在同一操作系统线程其他协同程序的不同,运行的线程不会被阻止
但是,运行时如何检测到goroutine被阻塞?
例如,如果我将在go-routine之一中运行计算,它会被评估为阻塞操作吗?
package main
import (
"fmt"
"runtime"
)
func f(from string, score int) {
for i := 0; i < score; i++ {
for z := 0; z < score; z++ {
}
}
fmt.Println(from, " Done")
}
func main() {
runtime.GOMAXPROCS(1)
f("direct", 300000)
go f("foo", 200000)
go f("bar", 20000)
go f("baz", 2000)
go func(msg string) {
fmt.Println(msg)
}("going without loop")
var input string
fmt.Scanln(&input)
fmt.Println("done")
}
我得到的结果:巴兹,嘘酒吧。但为什么? Go了解foo
是否阻止?
http://blog.alexnesterov.com/post/coroutines-part-1/也是一个有趣的阅读(对于Kotlin,但提及Go) – VonC