golang - 实现并发数控制的方法

golang并发

谈到golang这门语言,很自然的想起了他的的并发goroutine。这也是这门语言引以为豪的功能点。并发处理,在某种程度上,可以提高我们对机器的使用率,提升系统业务处理能力。但是并不是并发量越大越好,太大了,硬件环境就会吃不消,反而会影响到系统整体性能,甚至奔溃。所以,在使用golang提供便捷的goroutine时,既要能够实现开启并发,也要学会如果控制并发量。

开启golang并发

golang开启并发处理非常简单,只需要在调用函数时,在函数前边添加上go关键字即可。如下边例子所示:

?

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| package main import ( ``"fmt" ``"time" ) type Demo struct { ``input chan string ``output chan string ``max_goroutine chan int } func NewDemo() *Demo { ``d := new(Demo) ``d.input = make(chan string, 24) ``d.output = make(chan string, 24) ``d.max_goroutine = make(chan int, 20) ``return d } func (this *Demo) Goroutine() { ``var i = 1000 ``for { ``this.input <- time.Now().Format("2006-01-02 15:04:05") ``time.Sleep(time.Second * 1) ``if i < 0 { ``break ``} ``i-- ``} ``close(this.input) } func (this *Demo) Handle() { ``for t := range this.input { ``fmt.Println("datatime is :", t) ``this.output <- t ``} } func main() { ``demo := NewDemo() ``go demo.Goroutine() ``demo.Handle() } |

上边代码,在调用Demo的Goroutine方法时,在前边加上了go关键字,则函数Goroutine并发执行开启成功。

可见,在golang中开启并发非常的方便。

下边再来看看,在golang中,怎么实现并发量的控制。

当goroutine并发执行的任务达到一定值时,主程序等待goroutine执行完成退出,一旦发现并发数量低于某一个设定的值,就从新开始执行主程序逻辑。

实现代码如下:

?

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| package main import ( ``"fmt" ``"time" ) type Demo struct { ``input chan string ``output chan string ``goroutine_cnt chan int } func NewDemo() *Demo { ``d := new(Demo) ``d.input = make(chan string, 8192) ``d.output = make(chan string, 8192) ``d.goroutine_cnt = make(chan int, 10) ``return d } func (this *Demo) Goroutine() { ``this.input <- time.Now().Format("2006-01-02 15:04:05") ``time.Sleep(time.Millisecond * 500) ``<-this.goroutine_cnt } func (this *Demo) Handle() { ``for t := range this.input { ``fmt.Println("datatime is :", t, "goroutine count is :", len(this.goroutine_cnt)) ``this.output <- t + "handle" ``} } func main() { ``demo := NewDemo() ``go demo.Handle() ``for i := 0; i < 10000; i++ { ``demo.goroutine_cnt <- 1 ``go demo.Goroutine() ``} ``close(demo.input) } |

如上边示例,Goroutine()函数,每隔500毫秒写入一个时间戳到管道中,不考虑管道的读取时间,也就是说,每个Goroutine会存在大概500毫秒时间,如果不做控制的话,一瞬间可以开启上万个甚至更多的goroutine出来,这样系统就会奔溃。

在上述代码中,我们引入了带10个buffer的chan int字段,每创建一个goroutine时,就会向这个chan中写入一个1,每完成一个goroutine时,就会从chan中弹出一个1。当chan中装满10个1时,就会自动阻塞,等待goroutine执行完,弹出chan中的值时,才能继续开启goroutine。通过chan阻塞特点,实现了goroutine的最大并发量控制。

相关推荐
站大爷IP2 分钟前
Python文件与目录比较全攻略:从基础操作到性能优化
python
人生在勤,不索何获-白大侠13 分钟前
day16——Java集合进阶(Collection、List、Set)
java·开发语言
LIN-JUN-WEI31 分钟前
[ESP32]VSCODE+ESP-IDF环境搭建及blink例程尝试(win10 win11均配置成功)
c语言·开发语言·ide·vscode·单片机·学习·编辑器
望获linux1 小时前
【Linux基础知识系列】第四十三篇 - 基础正则表达式与 grep/sed
linux·运维·服务器·开发语言·前端·操作系统·嵌入式软件
ahead~1 小时前
【大模型入门】访问GPT_API实战案例
人工智能·python·gpt·大语言模型llm
留不住丨晚霞2 小时前
说说SpringBoot常用的注解?
java·开发语言
大模型真好玩2 小时前
准确率飙升!GraphRAG如何利用知识图谱提升RAG答案质量(额外篇)——大规模文本数据下GraphRAG实战
人工智能·python·mcp
19892 小时前
【零基础学AI】第30讲:生成对抗网络(GAN)实战 - 手写数字生成
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·近邻算法
华科云商xiao徐2 小时前
Java多线程爬虫动态线程管理实现
java·爬虫·数据挖掘
hardStudy_h2 小时前
C++——内联函数与Lambda表达式
开发语言·jvm·c++