go 协程之WaitGroup超时控制
作者:互联网
package main import ( "fmt" "sync" "time" ) // 调用示例 func main() { var w = sync.WaitGroup{} var ch = make(chan bool) w.Add(2) go func() { time.Sleep(time.Second * 2) fmt.Println("我2秒") w.Done() }() go func() { time.Sleep(time.Second * 6) fmt.Println("我6秒") w.Done() }() go func() { w.Wait() ch <- false }() select { case <-time.After(time.Second * 7): fmt.Println("我超时了") case <-ch: fmt.Println("我结束了") } }
标签:WaitGroup,ch,协程,fmt,sync,func,time,go 来源: https://www.cnblogs.com/zxqblogrecord/p/15830426.html