mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-25 06:16:03 +03:00
sync wait group
This commit is contained in:
parent
b30bfe7e11
commit
08f341f9af
@ -1,11 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// unbuf()
|
||||
// buf()
|
||||
nBuf()
|
||||
// nBuf()
|
||||
syncWaitGroup()
|
||||
}
|
||||
|
||||
/*
|
||||
@ -45,3 +49,17 @@ func nBuf() {
|
||||
<-done
|
||||
}
|
||||
}
|
||||
|
||||
func syncWaitGroup() {
|
||||
var wg sync.WaitGroup
|
||||
// 开N个后台打印线程
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
fmt.Println("你好, 世界", i)
|
||||
wg.Done()
|
||||
}(i)
|
||||
}
|
||||
// 等待N个后台线程完成
|
||||
wg.Wait()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user