校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > golang實現并發數控制的方法

golang實現并發數控制的方法

熱門標簽:釘釘有地圖標注功能嗎 鄭州亮點科技用的什么外呼系統 建造者2地圖標注 惠州電銷防封電話卡 濱州自動電銷機器人排名 浙江高頻外呼系統多少錢一個月 汕頭小型外呼系統 黃岡人工智能電銷機器人哪個好 阿里云ai電話機器人

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實現并發數控制的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 如何利用Golang寫出高并發代碼詳解
  • golang高并發的深入理解
  • golang并發ping主機的方法

標簽:東營 晉中 阿壩 瀘州 滄州 泰安 駐馬店 昭通

巨人網絡通訊聲明:本文標題《golang實現并發數控制的方法》,本文關鍵詞  golang,實現,并發,數,控制,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《golang實現并發數控制的方法》相關的同類信息!
  • 本頁收集關于golang實現并發數控制的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 镇康县| 梁河县| 沁水县| 竹溪县| 旺苍县| 信宜市| 昆明市| 东至县| 乌拉特后旗| 屏南县| 五台县| 潼关县| 丹江口市| 东兰县| 舒兰市| 靖州| 东平县| 大渡口区| 仙游县| 开远市| 阿瓦提县| 济阳县| 横山县| 苗栗县| 祁门县| 瓦房店市| 南溪县| 遂昌县| 句容市| 泊头市| 天祝| 农安县| 丽江市| 甘南县| 玉树县| 五大连池市| 慈利县| 彭州市| 定日县| 嘉义县| 包头市|