Channel能够很好的帮助我们控制并发,但是在开发习惯上与显示的表达不太相同,所以在Go语言中可以利用sync包中的WaitGroup实现并发控制,更加直观。
我们将之前的示例加以改造,引入sync.WaitGroup来实现并发控制。
首先我们在主函数中定义WaitGroup
var wg sync.WaitGroup
每执行一个任务,则调用Add()方法
wg.Add(1)
在主函数中我们利用Wait()方法等待并发结束
wg.Wait()
在调用的函数中,我们需要将WaitGroup以指针方式传入,否则将造成Deadlock
// 主函数内 go ready(5, &wg) // 函数 func ready(s int, wg *sync.WaitGroup)
同时在函数执行完成后,调用wg.Done,我们使用defer实现
defer wg.Done()
package main import ( "fmt" "sync" "time" ) func ready(s int, wg *sync.WaitGroup) { defer wg.Done() fmt.Printf("Run func in a goroutine and wait for %v\n", s) time.Sleep(time.Second * time.Duration(s)) fmt.Printf("Run func in a goroutine and wait for %v end\n", s) } func main() { var wg sync.WaitGroup wg.Add(1) go ready(5, &wg) mainWaitSec := 2 fmt.Printf("Run Main function and wait for %v\n", mainWaitSec) time.Sleep(time.Second * time.Duration(mainWaitSec)) fmt.Printf("Run Main function and wait for %v done\n", mainWaitSec) wg.Wait() }
WaitGroup传入给函数时,需要以指针方式传递,否则会造成Deadlock
如果不想在函数中传递WaitGroup,也可以采用以下这种方式,通过并发匿名函数的方式,在主函数逻辑中对并发进行精准控制
var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) waitSec := i + 1 go func() { defer wg.Done() ready(waitSec) }() }
package main import ( "fmt" "sync" "time" ) func ready(s int) { fmt.Printf("Run func in a goroutine and wait for %v\n", s) time.Sleep(time.Second * time.Duration(s)) fmt.Printf("Run func in a goroutine and wait for %v end\n", s) } func main() { var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) waitSec := i + 1 go func() { defer wg.Done() ready(waitSec) }() } mainWaitSec := 2 fmt.Printf("Run Main function and wait for %v\n", mainWaitSec) time.Sleep(time.Second * time.Duration(mainWaitSec)) fmt.Printf("Run Main function and wait for %v done\n", mainWaitSec) wg.Wait() }
运行结果如下
Run Main function and wait for 2
Run func in a goroutine and wait for 2
Run func in a goroutine and wait for 4
Run func in a goroutine and wait for 5
Run func in a goroutine and wait for 1
Run func in a goroutine and wait for 3
Run func in a goroutine and wait for 1 end
Run Main function and wait for 2 done
Run func in a goroutine and wait for 2 end
Run func in a goroutine and wait for 3 end
Run func in a goroutine and wait for 4 end
Run func in a goroutine and wait for 5 end
到此这篇关于Go语言通过WaitGroup实现控制并发的示例详解的文章就介绍到这了,更多相关Go语言 WaitGroup控制并发内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
编程 | 2023-02-24 21:36
编程 | 2023-02-21 12:51
编程 | 2023-02-21 12:47
编程 | 2023-02-21 00:15
编程 | 2023-02-21 00:08
编程 | 2023-02-20 21:46
编程 | 2023-02-20 21:42
编程 | 2023-02-20 21:36
编程 | 2023-02-20 21:32
编程 | 2023-02-20 18:12
网友评论