Go语言通过WaitGroup实现控制并发的示例详解
来源:脚本之家
时间:2023-02-25 10:15:32 500浏览 收藏
各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题是《Go语言通过WaitGroup实现控制并发的示例详解》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到语言WaitGroup等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!
与Channel区别
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 完整代码
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 运行结果如下
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实现控制并发的示例详解》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于golang的内容请关注golang学习网公众号!
-
860 收藏
-
843 收藏
-
826 收藏
-
809 收藏
-
792 收藏
-
Golang · Go教程 | 15小时前 | 并发 · 标准库 · 命令行 · Go教程 · SHA-256 · 文件校验 · 命令行工具 SHA-256 文件校验 worker pool Go教程 目录扫描328 收藏
-
Golang · Go教程 | 18小时前 | 并发 · HTTP · 性能优化 · 故障排查 · Go教程 · Go Goroutine 连接复用 pprof http.Client close Response.Body201 收藏
-
Golang · Go教程 | 4天前 | 并发 · 闭包 · for range · 迁移 · Go教程 · Go 1.22 · Goroutine 闭包 循环变量 Go教程 Go 1.22 for range113 收藏
-
331 收藏
-
Golang · Go教程 | 4天前 | 单元测试 · 错误处理 · Go教程 · errors.Join · errors.Is · errors.Is Go错误处理 Go教程 errors.Join 多错误返回 批量校验352 收藏
-
Golang · Go教程 | 4天前 | Context · 超时控制 · Go教程 · http.Client · Transport · Go context 请求超时 Transport http.Client Client.Timeout ResponseHeaderTimeout218 收藏
-
Golang · Go教程 | 4天前 | 文件下载 · Go教程 · 审计日志 · 接口安全 · 路径穿越 · Go 文件下载 审计日志 HTTP接口 filepath.Clean 安全下载 路径穿越362 收藏
-
273 收藏
-
Golang · Go教程 | 4天前 | CI/CD · gitHub actions · Go教程 · 自托管 Runner · 持续集成 · Go 持续集成 CI Go test GitHub Actions self-hosted runner 自托管 runner340 收藏
-
124 收藏
-
Golang · Go教程 | 5天前 | HTTP · 文件下载 · Go教程 · Range请求 · ServeContent · 断点续传 Content-Range Go教程 HTTP Range ServeContent 206 Partial Content 视频拖动250 收藏
-
Golang · Go教程 | 5天前 | csv · Go教程 · 后端架构 · 流式响应 · 大文件导出 · 大文件下载 FLUSH CSV导出 Go教程 流式写出 csv.Writer rows.Next251 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习