why this code about golang goruntine running order is “2” first

两盒软妹~` 提交于 2019-12-04 19:32:35

You're not synchronizing your 2 launched goroutines to each other, so there is no guarantee in what order they run. The only thing you synchronize is the main goroutine to wait for the other 2 to complete, but their order will be undefined.

Here's an example that also synchronizes order using another sync.WaitGroup:

w := &sync.WaitGroup{}
w.Add(2)

w2 := &sync.WaitGroup{}
w2.Add(1)
go func() {
    fmt.Println("1")
    w.Done()
    w2.Done()
}()
go func() {
    w2.Wait()
    fmt.Println("2")
    w.Done()
}()
w.Wait()

Output will be (try it on the Go Playground):

1
2

Basically the 2nd goroutine waits for w2, which is called in the 1st goroutine once it's done.

Note that since the 2nd goroutine waits for the first, it is enough for the main goroutine to only wait for the 2nd goroutine (which transitively means to wait for both). So the above example can be written like this:

w2 := &sync.WaitGroup{}
w2.Add(1)
go func() {
    fmt.Println("1")
    w2.Done()
}()

w := &sync.WaitGroup{}
w.Add(1)
go func() {
    w2.Wait()
    fmt.Println("2")
    w.Done()
}()
w.Wait()

Output is the same. Try this one on the Go Playground.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!