当前位置:网站首页>GO语言-goroutine协程的使用
GO语言-goroutine协程的使用
2022-06-24 13:03:00 【一边学习一边哭】
前言
协程的英文coroutine,go语言中的协程为goroutine。Go是并发语言,而不是并行语言。
go语言的协程创建十分简单,只需要在调用的函数前面加上go关键字,程序就会同事运行一个新的goroutine。
但是goroutine中没有返回值,会忽略返回值。所以往往需要通过channel进行goroutine之间的通信。
goroutine使用
func main() {
go mytest()
for i := 0; i < 100; i++ {
fmt.Println("主函数输出:", i)
}
}
func mytest() {
for i := 0; i < 500; i++ {
fmt.Println("goroutine中输出数字:", i)
}
}
通过这个简单的示例输出,可以总结goroutine的两个特点:
- goroutine协程中的函数执行和主函数的执行是同时的,输出内容是交替的。
- 主程序结束后,即使子goroutine没有执行完成,也会随主程序一同结束。
sync包-WaitGroup
一般情况下,主程序结束后,子goroutine就会结束。
为了让主程序等待子groutine执行完成,一种方法是使用sleep。但是sleep存在一个问题是,我们没办法知道子goroutine需要执行的具体时长,只能尽量放长sleep的时间。
所以我们可以使用到sync包的WaitGroup解决主进程等待子goroutine的问题。
var wg sync.WaitGroup
func main() {
wg.Add(1) //定义WaitGroup队列长度,长度为1
go mytest()
for i := 0; i < 100; i++ {
fmt.Println("主函数输出:", i)
}
wg.Wait() //主程序最后,等待WaitGroup队列为0时再退出
}
func mytest() {
for i := 0; i < 500; i++ {
fmt.Println("goroutine中输出数字:", i)
}
wg.Done() //相当于wg.Add(-1),一个goroutine结束,调用wg.Done(),队列减一
}
wg.Add(1) 主函数中,定义WaitGroup队列长度
wg.Done() 子goroutine中,函数最后调用,相当于将WaitGroup队列长度-1
wg.Wait() 主函数中,在主函数的最后,等待WaitGroup队列长度为0
边栏推荐
- 谷歌WayMo提出R4D: 采用参考目标做远程距离估计
- [untitled]
- PM should also learn to reflect every day
- Autorf: learn the radiation field of 3D objects from single view (CVPR 2022)
- Linux 安装 CenOS7 MySQL - 8.0.26
- 卷积核、特征图可视化
- 业务与技术双向结合构建银行数据安全管理体系
- Research on MySQL composite index
- Method of establishing unity thermodynamic diagram
- Docker installation redis
猜你喜欢
![二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]

Puzzle (016.2) finger painting Galaxy

初识云原生安全:云时代的最佳保障

Google waymo proposed r4d: remote distance estimation using reference target

食品饮料行业渠道商管理系统解决方案:实现渠道数字化营销布局
![根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]](/img/f7/8d026c0e4435fc8fd7a63616b4554d.png)
根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]

Rasa 3. X learning series - it is a great honor to be a source code contributor of Rasa contributors, and to build and share the rasa community with rasa source code contributors all over the world!

遠程辦公之:在家露營辦公小工具| 社區征文

MySQL复合索引探究

SAP Marketing Cloud 功能概述(四)
随机推荐
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
2022 recurrent training question bank and answers for hoisting signal Rigger (special type of construction work)
在线文本实体抽取能力,助力应用解析海量文本数据
Convolution kernel and characteristic graph visualization
The real project managers are all closed-loop masters!
SAP Marketing Cloud 功能概述(三)
Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)
智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
c语言---18 函数(自定义函数)
One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
A review of text contrastive learning
Unity 热力图建立方法
Jupiter notebook operation
Jerry's infrared filtering [chapter]
AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
Three efficient programming skills of go language
Linux 安装 CenOS7 MySQL - 8.0.26
Harmony os. (2)