1.6 Go并发模式实战:Select、Context、WaitGroup三大并发原语深度应用
引言
Go语言提供了丰富的并发原语,其中Select、Context和WaitGroup是最常用的三个。掌握它们的使用方法和最佳实践,是编写高质量并发程序的关键。本文将深入解析这三个并发原语的应用场景和实战技巧。
一、Select深度应用
1.1 Select多路复用
Select允许goroutine同时等待多个channel操作。
packagemainimport("fmt""time")funcmain(){ch1:=make(chanstring)ch2:=make(chanstring)ch3:=make(chanstring)gofunc(){time.Sleep(1*time.Second)ch1<-"来自ch1"}()gofunc(){time.Sleep(2*time.Second)ch2<-"来自ch2"}()gofunc(){time.Sleep(3*time.Second)ch3<-"来自ch3"}()// 等待任意一个channel有数据select{casemsg:=<-ch1:fmt.Println("收到:",msg)casemsg:=<-ch2:fmt.Println("收到:",msg)casemsg:=<-ch3:fmt.Println("收到:",msg)}}1.2 Select实现优先级
packagemainimport("fmt""time")funcmain(){highPriority:=make(chanstring)lowPriority:=make(chanstring)gofunc(){fori:=0;i<5;i++{lowPriority<-fmt.Sprintf("低优先级%d",i)time.Sleep(200*time.Millisecond)}}()gofunc(){time.Sleep(500*time.Millisecond)highPriority<-"高优先级消息"}()fori:=0;i<5;i++{select{casemsg:=<-highPriority:fmt.Println("处理:",msg)casemsg:=<-lowPriority:fmt.Println("处理:",msg)}}}1.3 Select实现超时控制
packagemainimport("fmt""time")funcfetchData(chchan<-string){// 模拟耗时操作time.Sleep(3*time.Second)ch<-"数据获取成功"}funcmain(){ch:=make(chanstring)gofetchData(ch)select{caseresult:=<-ch:fmt.Println("成功:",result)case<-time.After(2*time.Second):fmt.Println("请求超时")}}1.4 Select实现心跳检测
packagemainimport("fmt""time")funcworker(heartbeat<-chantime.Time,done<-chanstruct{}){for{select{case<-heartbeat:fmt.Println("收到心跳")case<-done:fmt.Println("工作完成")return}}}funcmain(){heartbeat:=time.NewTicker(500*time.Millisecond)done:=make(chanstruct{})goworker(heartbeat.C,done)time.Sleep(3*time.Second)close(done)heartbeat.Stop()}二、Context深度应用
2.1 Context基础
Context用于在goroutine之间传递取消信号、超时和截止时间。
packagemainimport("context""fmt""time")funcdoWork(ctx context.Context){for{select{case<-ctx.Done():fmt.Println("工作被取消")returndefault:fmt.Println("工作中...")time.Sleep(500*time.Millisecond)}}}funcmain(){ctx,cancel:=context.WithCancel(