当前位置:网站首页>GO同步等待组
GO同步等待组
2022-06-24 23:12:00 【南瓜头777】
Go语言中除了可以使用通道(channel)和互斥锁进行两个并发程序间的同步外,还可以使用等待组进行多个任务的同步,等待组可以保证在并发环境中完成指定数量的任务
在 sync.WaitGroup(等待组)类型中,每个 sync.WaitGroup 值在内部维护着一个计数,此计数的初始默认值为零。
package main
import (
"fmt"
"sync"
"time"
)
var wg sync.WaitGroup //定义一个同步等待的组
func task(i int) {
fmt.Println("task...", i)
//耗时操作任务,网络请求,读取文件
time.Sleep(time.Second)
wg.Done() //减去一个计数
}
func main() {
for i := 0; i < 10; i++ {
wg.Add(1) //添加一个计数
go task(i)
}
wg.Wait() //阻塞直到所有任务完成
fmt.Println("over")
}
边栏推荐
- 产业互联网的概念里有「互联网」字眼,但却是一个和互联网并不关联的存在
- 同花顺是正规平台吗?同花顺开户安全吗
- Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (2) -- convert database to cluster mode
- UnityShader入门精要——PBS基于物理的渲染
- LeetCode 210:课程表 II (拓扑排序)
- Planification du réseau | [quatre couches de réseau] points de connaissance et exemples
- 把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
- Summary of knowledge points of computer level III (database) test preparation topics
- 【Proteus仿真】Arduino UNO+继电器控制照明设备
- 当他们在私域里,掌握了分寸感
猜你喜欢
leecode学习笔记-机器人走到终点的最短路径
Sumati gamefi ecological overview, element design in the magical world
When they are in private, they have a sense of propriety
计网 | 【四 网络层】知识点及例题
Folding screen will become an important weapon for domestic mobile phones to share the apple market
I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
Detailed explanation of cache (for the postgraduate entrance examination of XD)
QT package the EXE file to solve the problem that "the program input point \u zdapvj cannot be located in the dynamic link library qt5cored.dll"
会自动化—10K,能做自动化—20K,你搞懂自动化测试没有?
UnityShader入门精要——PBS基于物理的渲染
随机推荐
centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码
LINQ query (3)
Processon producer process (customized)
Please run IDA with elevated permissons for local debugging.
internship:svn的使用
常用的软件测试工具清单,请查收。
ERROR日志格式与注意点
使用ShaderGraph制作边缘融合粒子Shader的启示
E - Average and Median(二分)
【STL源码剖析】配置器(待补充)
random list随机生成不重复数
【Proteus仿真】Arduino UNO+数码管显示4x4键盘矩阵按键
云原生数据库VS传统数据库
都2022年了,你还不了解什么是性能测试?
Dirvish Chinese document of vim
Call system function security scheme
Computing service network: a systematic revolution of multi integration
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (2) -- convert database to cluster mode
ARM汇编中的栈桢小结
Unity存档系统——Json格式的文件