当前位置:网站首页>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
边栏推荐
- 2022 Quality Officer - Equipment direction - post skills (Quality Officer) recurrent training question bank and online simulation examination
- Method of establishing unity thermodynamic diagram
- P2pdb white paper
- Antd checkbox, limit the selected quantity
- 【环境搭建】zip 分卷压缩
- conda和pip命令
- CONDA and pip commands
- One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
- AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
- 从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
猜你喜欢
Common singleton mode & simple factory
How to manage tasks in the low code platform of the Internet of things?
初识云原生安全:云时代的最佳保障
【深度学习】NCHW、NHWC和CHWN格式数据的存储形式
Solution of channel management system for food and beverage industry: realize channel digital marketing layout
厨卫电器行业B2B交易协同管理平台开发,优化企业库存结构
Jerry's infrared filtering [chapter]
HarmonyOS.2
【无标题】
leetcode:1504. Count the number of all 1 sub rectangles
随机推荐
Jupiter notebook operation
The function and principle of key in V-for
HarmonyOS.2
从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
conda和pip命令
Solution of channel management system for food and beverage industry: realize channel digital marketing layout
The project manager needs to look at six characteristics to build a team
ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
Jerry's serial port receiving IO needs to set the digital function [chapter]
HarmonyOS-3
Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
#21Set经典案例
Docker installation redis
[untitled]
SSH keygen configuration does not require entering a password every time
English writing of Mathematics -- Basic Chinese and English vocabulary (common vocabulary of geometry and trigonometry)
数字臧品系统开发 NFT数字臧品系统异常处理源码分享
Zhiyuan community weekly 86: Gary Marcus talks about three linguistic factors that can be used for reference in large model research; Google puts forward the Wensheng graph model parti which is compar
Puzzle (016.2) finger painting Galaxy
Method of establishing unity thermodynamic diagram