Concurrency 计数/显示活动goroutine的数量

Concurrency 计数/显示活动goroutine的数量,concurrency,go,goroutine,Concurrency,Go,Goroutine,我有一个队列和一个函数,它同时执行出队列和入队列。我想确保在队列上运行正确数量的goroutine,只要列表中有内容 这是我正在使用的代码,但我想知道是否有一种方法可以打印当前活动goroutine的数量 var元素int func deen(队列内部){ 元素:=有很多,但你的做法是错误的 你的循环将继续生成goroutines 由于for循环,这将不必要地消耗cpu周期 一种方法是使用sync.WaitGroup func deen(wg *sync.WaitGroup, queue ch

我有一个队列和一个函数,它同时执行出队列和入队列。我想确保在队列上运行正确数量的goroutine,只要列表中有内容

这是我正在使用的代码,但我想知道是否有一种方法可以打印当前活动goroutine的数量

var元素int
func deen(队列内部){
元素:=有很多,但你的做法是错误的

  • 你的循环将继续生成goroutines
  • 由于for循环,这将不必要地消耗cpu周期
  • 一种方法是使用sync.WaitGroup

    func deen(wg *sync.WaitGroup, queue chan int) {
        for element := range queue {
            fmt.Println("element is ", element)
            if element%2 == 0 {
                fmt.Println("new element is ", element)
                wg.Add(2)
                queue <- (element*100 + 11)
                queue <- (element*100 + 33)
            }
            wg.Done()
        }
    }
    
    func main() {
        var wg sync.WaitGroup
        queue := make(chan int, 10)
        queue <- 1
        queue <- 2
        queue <- 3
        queue <- 0
        for i := 0; i < 4; i++ {
            wg.Add(1)
            go deen(&wg, queue)
        }
        wg.Wait()
        close(queue)
        fmt.Println("list len", len(queue)) //this must be 0
    
    }
    
    func deen(wg*sync.WaitGroup,queue chan int){
    对于元素:=范围队列{
    fmt.Println(“元素是”,元素)
    如果元素%2==0{
    fmt.Println(“新元素是”,元素)
    工作组.添加(2)
    
    排队谢谢,但WaitGroup不是要等事情完成了吗?我真的想确保他们不会因为一些外部事件而过早死亡reason@metoGoroutines不会“死”这样,如果一个goroutine死了,那么你的程序很有可能崩溃,我会添加一个例子。这很有趣。只是一个简单的后续问题。wg.Done部分也可以放在Println之后,但肯定是在if之前,对吗?@meto实际上对于那个特定的例子来说,这根本不重要,只要它在
    if
    @meto No,因为wg.add在退出主for循环之前不会执行,所以goroutines不会立即执行。
    func deen(wg *sync.WaitGroup, queue chan int) {
        for element := range queue {
            fmt.Println("element is ", element)
            if element%2 == 0 {
                fmt.Println("new element is ", element)
                wg.Add(2)
                queue <- (element*100 + 11)
                queue <- (element*100 + 33)
            }
            wg.Done()
        }
    }
    
    func main() {
        var wg sync.WaitGroup
        queue := make(chan int, 10)
        queue <- 1
        queue <- 2
        queue <- 3
        queue <- 0
        for i := 0; i < 4; i++ {
            wg.Add(1)
            go deen(&wg, queue)
        }
        wg.Wait()
        close(queue)
        fmt.Println("list len", len(queue)) //this must be 0
    
    }
    
    func deen(wg *sync.WaitGroup, queue chan int) {
        for element := range queue {
            wg.Done()
            fmt.Println("element is ", element)
            if element%2 == 0 {
                fmt.Println("new element is ", element)
                wg.Add(2)
                queue <- (element*100 + 11)
                queue <- (element*100 + 33)
            }
        }
    }
    
    func main() {
        var wg sync.WaitGroup
        queue := make(chan int, 10)
        queue <- 1
        queue <- 2
        queue <- 3
        queue <- 0
        for i := 0; i < 4; i++ {
            wg.Add(1)
            go deen(&wg, queue)
        }
        wg.Wait()
        close(queue)
        fmt.Println("list is has len", len(queue)) //this must be 0
    }