当前位置:网站首页>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
边栏推荐
- Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
- 如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
- [untitled]
- `Thymeleaf`模板引擎全面解析
- Jerry's seamless looping [chapter]
- Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
- 根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
- 简谈企业Power BI CI /CD 实施框架
- Baidu map API drawing points and tips
- HarmonyOS.2
猜你喜欢

打败 二叉树!

如何避免下重复订单

SaaS management system solution of smart Park: enabling the park to realize information and digital management

Overview of SAP marketing cloud functions (III)

Method of establishing unity thermodynamic diagram

**Puzzling little problem in unity - light and sky box

Kunpeng arm server compilation and installation paddlepaddle

unity 等高线创建方法

厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构

leetcode:1504. Count the number of all 1 sub rectangles
随机推荐
Docker installation redis
Unit contour creation method
2022 coal mine gas drainage operation certificate examination questions and simulation examination
日常知识科普
Defoaming
Go语言三个高效编程的技巧
Grendao usage problems
百度地图API绘制点及提示信息
pgsql查询分组中某个字段最大或者最小的一条数据
Online text entity extraction capability helps applications analyze massive text data
HarmonyOS. two
A review of text contrastive learning
文本对比学习综述
简谈企业Power BI CI /CD 实施框架
2022 recurrent training question bank and answers for hoisting signal Rigger (special type of construction work)
4 reasons for "safe left shift"
pip uninstall all packages except builtin package
Simulated 100 questions and answers of fluorination process examination in 2022
Development of B2B transaction collaborative management platform for kitchen and bathroom electrical appliance industry and optimization of enterprise inventory structure
P2PDB 白皮书