WaitGroups {.en}

WaitGroups {.zh}

::: {.en} To wait for multiple goroutines to finish, we can use a wait group. :::

::: {.zh}

要等待多个 Go 协程完成,我们可以使用 wait group

:::

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. )

::: {.en} This is the function we’ll run in every goroutine. Note that a WaitGroup must be passed to functions by pointer. :::

::: {.zh}

这是我们将在每个 Go 协程中运行的函数。请注意,WaitGroup 必须通过指针传递给函数。

:::

  1. func worker(id int, wg *sync.WaitGroup) {
  2. fmt.Printf("Worker %d startingn", id)

::: {.en} Sleep to simulate an expensive task. :::

::: {.zh} 等待 1s 来模拟一个耗时的任务。

:::

  1. time.Sleep(time.Second)
  2. fmt.Printf("Worker %d donen", id)

::: {.en} Notify the WaitGroup that this worker is done. :::

::: {.zh}

通知 WaitGroup 此执行已完成。

:::

  1. wg.Done()
  2. }
  3. func main() {

::: {.en} This WaitGroup is used to wait for all the goroutines launched here to finish. :::

::: {.zh}

此 WaitGroup 用于等待此处启动的所有 Go 协程完成。

:::

  1. var wg sync.WaitGroup

::: {.en} Launch several goroutines and increment the WaitGroup counter for each. :::

::: {.zh}

启动几个 Go 协程并为每个 Go 协程增加 WaitGroup 计数器。

:::

  1. for i := 1; i <= 5; i++ {
  2. wg.Add(1)
  3. go worker(i, &wg)
  4. }

::: {.en} Block until the WaitGroup counter goes back to 0; all the workers notified they’re done. :::

::: {.zh}

在 WaitGroup 计数器返回 0 之前阻塞;所有工作人员都通知他们已完成。

:::

  1. wg.Wait()
  2. }
  1. $ go run waitgroups.go
  2. Worker 5 starting
  3. Worker 3 starting
  4. Worker 4 starting
  5. Worker 1 starting
  6. Worker 2 starting
  7. Worker 4 done
  8. Worker 1 done
  9. Worker 2 done
  10. Worker 5 done
  11. Worker 3 done

::: {.en} The order of workers starting up and finishing is likely to be different for each invocation. :::

::: {.zh}

每次调用时,worker 启动和完成的顺序可能不同。

:::