golang實現(xiàn)并發(fā)數控制的方法
golang并發(fā)
談到golang這門語言,很自然的想起了他的的并發(fā)goroutine。這也是這門語言引以為豪的功能點。并發(fā)處理,在某種程度上,可以提高我們對機器的使用率,提升系統(tǒng)業(yè)務處理能力。但是并不是并發(fā)量越大越好,太大了,硬件環(huán)境就會吃不消,反而會影響到系統(tǒng)整體性能,甚至奔潰。所以,在使用golang提供便捷的goroutine時,既要能夠實現(xiàn)開啟并發(fā),也要學會如果控制并發(fā)量。
開啟golang并發(fā)
golang開啟并發(fā)處理非常簡單,只需要在調用函數時,在函數前邊添加上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并發(fā)執(zhí)行開啟成功。
可見,在golang中開啟并發(fā)非常的方便。
下邊再來看看,在golang中,怎么實現(xiàn)并發(fā)量的控制。
當goroutine并發(fā)執(zhí)行的任務達到一定值時,主程序等待goroutine執(zhí)行完成退出,一旦發(fā)現(xiàn)并發(fā)數量低于某一個設定的值,就從新開始執(zhí)行主程序邏輯。
實現(xiàn)代碼如下:
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出來,這樣系統(tǒng)就會奔潰。
在上述代碼中,我們引入了帶10個buffer的chan int字段,每創(chuàng)建一個goroutine時,就會向這個chan中寫入一個1,每完成一個goroutine時,就會從chan中彈出一個1。當chan中裝滿10個1時,就會自動阻塞,等待goroutine執(zhí)行完,彈出chan中的值時,才能繼續(xù)開啟goroutine。通過chan阻塞特點,實現(xiàn)了goroutine的最大并發(fā)量控制。
以上這篇golang實現(xiàn)并發(fā)數控制的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 使用google-perftools優(yōu)化nginx在高并發(fā)時的性能的教程(完整版)
- Golang極簡入門教程(三):并發(fā)支持
- Go語言并發(fā)技術詳解
- Go語言并發(fā)模型的2種編程方案
- GO語言并發(fā)編程之互斥鎖、讀寫鎖詳解
- Go語言如何并發(fā)超時處理詳解
- 如何利用Golang寫出高并發(fā)代碼詳解
- 詳解Golang 中的并發(fā)限制與超時控制
- golang中sync.Map并發(fā)創(chuàng)建、讀取問題實戰(zhàn)記錄
- Go 并發(fā)實現(xiàn)協(xié)程同步的多種解決方法
- 在Go中構建并發(fā)TCP服務器
- Go 并發(fā)控制context實現(xiàn)原理剖析(小結)
- Go并發(fā)調用的超時處理的方法
- golang 并發(fā)安全Map以及分段鎖的實現(xiàn)方法
- golang并發(fā)下載多個文件的方法
- Golang 實現(xiàn)分片讀取http超大文件流和并發(fā)控制
- golang gin 框架 異步同步 goroutine 并發(fā)操作
- Go并發(fā)4種方法簡明講解
相關文章
解讀unsafe.Pointer和uintptr的區(qū)別
這篇文章主要介紹了解讀unsafe.Pointer和uintptr的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
golang?cache帶索引超時緩存庫實戰(zhàn)示例
這篇文章主要為大家介紹了golang?cache帶索引超時緩存庫實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Go語言crypto包創(chuàng)建自己的密碼加密工具實現(xiàn)示例
Go語言借助它的簡單性和強大的標準庫,實現(xiàn)一個自己的密碼加密工具,本文將會結合代碼示例深入探討如何使用Go語言的crypto包來實現(xiàn)自己的加密工具2023-11-11
GoLang?socket網絡編程傳輸數據包時進行長度校驗的方法
在GoLang?socket網絡編程中,為了確保數據交互的穩(wěn)定性和安全性,通常會通過傳輸數據的長度進行校驗,發(fā)送端首先發(fā)送數據長度,然后發(fā)送數據本體,接收端則根據接收到的數據長度和數據本體進行比較,以此來確認數據是否傳輸成功2024-11-11

