package main
import "fmt"
func square(c chan int) {
fmt.Println("[square] reading (4)")
num := <-c
fmt.Println("[square] calc (5)")
c <- num * num
fmt.Println("back from [square] (10)")
}
func cube(c chan int) {
fmt.Println("[cube] reading (3)")
num := <-c
fmt.Println("[cube] calc (11)")
c <- num * num * num
fmt.Println("back from [cube] (12)")
}
func main() {
fmt.Println("[main] main() started (1)")
squareChan := make(chan int)
cubeChan := make(chan int)
go square(squareChan)
go cube(cubeChan)
testNum := 3
fmt.Println("[main] sent testNum to squareChan (2)")
squareChan <- testNum
fmt.Println("[main] resuming (6)")
fmt.Println("[main] sent testNum to cubeChan (7)")
cubeChan <- testNum // why doesn't block here?
fmt.Println("[main] resuming (8)")
fmt.Println("[main] reading from channels (9)")
squareVal, cubeVal := <-squareChan, <-cubeChan
fmt.Println("[main] waiting calculating (13)")
fmt.Println("[main] results: ", squareVal, cubeVal)
fmt.Println("[main] main() stopped")
}
输出:
[main] main() started (1)
[main] sent testNum to squareChan (2)
[cube] reading (3)
[square] reading (4)
[square] calc (5)
[main] resuming (6)
[main] sent testNum to cubeChan (7)
[main] resuming (8)
[main] reading from channels (9)
back from [square] (10)
[cube] calc (11)
back from [cube] (12)
[main] waiting calculating (13)
[main] results: 9 27
[main] main() stopped
在上面给出的代码中,我认为
main()
例程应在cubeChan <- testNum
之后被阻塞,然后应安排cube
例程,这意味着输出[cube] calc (11)
应该在[main] resuming (8)
之前。但是在Playground上执行后,我对输出感到困惑。谁能告诉我我是否误会了什么?
最佳答案
我认为main()例程应在cubeChan
它确实会阻止,但是在这些方面:
squareVal, cubeVal := <-squareChan, <-cubeChan
^ ^
如果
squareChan
或cubeChan
在其他例程中未接收到任何值,则该行将变为死锁,请按如下所示修改cube()
以查看效果:func cube(c chan int) {
fmt.Println("[cube] reading (3)")
_ = <-c
fmt.Println("[cube] calc (11)")
// c <- num * num * num
fmt.Println("back from [cube] (12)")
}
关于go - 安排多个goroutine,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56714188/