当前位置:网站首页>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
边栏推荐
- 怎样评价国产报表工具和BI软件
- 百度地图API绘制点及提示信息
- 10 Ces autographes très stylisés.
- box-sizing
- 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!
- v-for 中 key的作用和原理
- Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
- Jerry added an input capture channel [chapter]
- 一文搞定 UDP 和 TCP 高频面试题!
- The difference between V-IF and v-show
猜你喜欢

HarmonyOS-3

Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire

IDEA连接mysql自定义生成实体类代码

Method of inputting dots under letters in markdown/latex

v-if 和 v-show 的区别

智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
![Jerry's serial port receiving IO needs to set the digital function [chapter]](/img/04/48e7da42101a53827463d479952f67.png)
Jerry's serial port receiving IO needs to set the digital function [chapter]
![Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]](/img/f7/8d026c0e4435fc8fd7a63616b4554d.png)
Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]

Py之toad:toad的简介、安装、使用方法之详细攻略
![Jerry's infrared filtering [chapter]](/img/6b/7c4b52d39a4c90f969674a5c21b2c7.png)
Jerry's infrared filtering [chapter]
随机推荐
融云通信“三板斧”,“砍”到了银行的心坎上
Kotlin coordination channel
Common singleton mode & simple factory
PgSQL queries the largest or smallest data of a field in a group
P2pdb white paper
【环境搭建】zip 分卷压缩
AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
4个不可不知的“安全左移”的理由
文本对比学习综述
Baidu map API drawing points and tips
专精特新“小巨人”再启动,“企业上云”数字赋能
Gatling performance test
Idea connection MySQL custom generated entity class code
港股上市公司公告 API 数据接口
Telecommuting: camping at home office gadgets | community essay solicitation
智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理
Solution of channel management system for food and beverage industry: realize channel digital marketing layout
2022 Quality Officer - Equipment direction - post skills (Quality Officer) recurrent training question bank and online simulation examination
OpenHarmony 1
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]