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的最大并发量控制。
以上这篇golang实现并发数控制的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- 任天堂今晚举行直面会!第三方及独立游戏展示
- 《哆啦A梦的铜锣烧店物语》发售!开罗公式+哆啦A梦
- 任天堂公布《塞尔达传说》系列时间线:野炊与王泪独立在外
- 五条人.2012-一些风景2CD【刀马旦】【WAV+CUE】
- 陈奕迅.2013-Easons.Life演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 许美静.1995-遗憾(新马版)【上华】【WAV+CUE】
- 《叶倩文 歌声情缘》[WAV+CUE][410MB]
- 《张国荣 首首动听经典不容错过 追忆的风 2CD》[WAV+CUE][870MB]
- 《腾格尔 容中尔甲 亚东 高原三星 男人篇 3CD》[WAV/分轨][1GB]
- 命运圣契公测实测可用兑换码大全 命运圣契最新兑换码分享
- 黑神话悟空上品疾蝠精魄获取方法一览|上品疾蝠精魄收集攻略
- 《七龙珠电光炸裂!ZERO》GT角色预告片曝光,15位新角色登场
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- NewViennaOctetViennaWindSoloists-TheDeccaRecordings(2024)18CD[24-48][FLAC]-7