美文网首页
Golang sync.WaitGroup基础用法

Golang sync.WaitGroup基础用法

作者: FredricZhu | 来源:发表于2019-06-08 08:02 被阅读0次
package main

import (
    "fmt"
    "sync"
    "time"
)

func main() {
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        defer wg.Done()
        fmt.Println("First goroutine sleep")
        time.Sleep(1 * time.Second)
    }()

    wg.Add(1)
    go func() {
        defer wg.Done()
        fmt.Println("Second goroutine sleep")
        time.Sleep(2 * time.Second)
    }()
    wg.Wait()
    fmt.Println("All goroutines complete")
}

程序输出如下,


image.png

相关文章

网友评论

      本文标题:Golang sync.WaitGroup基础用法

      本文链接:https://www.haomeiwen.com/subject/fcibxctx.html