亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 gooutins 和渠道的并發受限消費者

使用 gooutins 和渠道的并發受限消費者

Go
牛魔王的故事 2021-06-29 13:57:02
我試圖重現“良好管理資源的方法是啟動固定數量的句柄 goroutines,所有這些 goroutines 都從請求通道讀取?!?從Effective Go 中發現fatal error: all goroutines are asleep - deadlock!這個想法很簡單:有 1 個隊列和 1 個結果通道和幾個有限數量的“工人”。我的代碼在 Go Playgroundqueue := make(chan *Request)result := make(chan int)quit := make(chan bool)go Serve(queue, quit)for i := 0; i < 10; i++ {    req := Request{i, result}    queue <- &req}close(queue)for i := 0; i < 10; i++ {    fmt.Printf("Finished %d\n", <-result)}fmt.Printf("All finished\n")quit <- true功能服務:func handle(queue chan *Request) {    for r := range queue {        //process(r)        fmt.Printf("Processing %d\n", r.i)        r.r <- r.i    }}func Serve(clientRequests chan *Request, quit chan bool) {    MaxOutstanding := 2    // Start handlers    for i := 0; i < MaxOutstanding; i++ {        go handle(clientRequests)    }    <-quit // Wait to be told to exit.}怎么了?或者可能有更簡單的解決方案來實現數量有限的處理請求的工人?
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號