安排多个goroutine

时间:2019-06-22 09:13:26

标签: go goroutine

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上执行后,我对输出感到困惑。

有人能告诉我我是否误会了吗?

2 个答案:

答案 0 :(得分:0)

  

我认为main()例程应在cubeChan <-testNum

之后被阻止

它确实会阻止,但是在这些地方:

squareVal, cubeVal := <-squareChan, <-cubeChan
                      ^             ^

如果squareChancubeChan在其他例程中均未收到任何值,则该行将变为死锁,请按如下所示修改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)")
}

答案 1 :(得分:0)

我认为您误解了并发代码。

但是,仅当通道无输出或已满时,通道才会阻塞流量。只有指定尺寸,频道才能满载。

我也不建议对功能的输入和输出使用相同的通道

使用当前的方法,您可能会在这里遇到麻烦

cubeChan <- testNum 

fmt.Println("[main] resuming (8)")
fmt.Println("[main] reading from channels (9)")

squareVal, cubeVal := <-squareChan, <-cubeChan

如果由于任何可能的原因,多维数据集功能太慢,您可以在此处使用此行立即删除添加到cubechan的testNum

squareVal, cubeVal := <-squareChan, <-cubeChan