计数/显示活动goroutine的数量 [英] count / display the number of active goroutines

查看:150
本文介绍了计数/显示活动goroutine的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个队列和一个函数,既出队和入队。



这是我正在使用的代码,但是我想要确保正确的数量的goroutine在队列中操作,只要在列表中有一些。我想知道是否有一种打印当前活跃的goroutine数量的方法。



链接到操场

  var element int 

func deen (queue chan int){

element:=< -queue
fmt.Println(element is,element)
如果元素%2 == 0 {
fmt.Println(new element is,element)
queue< - (element * 100 + 11)
queue< - (element * 100 + 33)
}
}

func main(){
queue:= make(chan int,10)
queue< - 1
queue< - 2
queue< - 3
queue< - 0
for len(queue)!= 0 {
for i:= 0; i< 2; i ++ {
go deen(queue)
}
}
fmt.Scanln()
fmt.Println(list is has len,len(queue)) // this must be 0

}


解决方案>

runtime.NumGoroutine


  1. 你的循环会继续产生goroutines。



  2. 一种方法是使用sync.WaitGroup。

      func deen(wg * sync.WaitGroup,queue chan int){
    for element:= range queue {
    wg.Done ()
    fmt.Println(element is,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 b fmt.Println(list is has len,len(queue))//必须为0
    }

    playground


    I have a queue and a function that does both dequeueing and enqueueing. I want to make sure that the right amount of goroutines operate on the queue, as long as there is something in the list.

    This is the code I am using, but I was wondering if there is a way of printing the amount of currently active goroutines

    Link to playground

    var element int
    
    func deen(queue chan int) {
    
        element := <-queue
        fmt.Println("element is ", element)
        if element%2 == 0 {
            fmt.Println("new element is ", element)
            queue <- (element*100 + 11)
            queue <- (element*100 + 33)
        }
    }
    
    func main() {
        queue := make(chan int, 10)
        queue <- 1
        queue <- 2
        queue <- 3
        queue <- 0 
        for len(queue) != 0 {
            for i := 0; i < 2; i++ {
                go deen(queue)
            }
        }
        fmt.Scanln()
        fmt.Println("list is has len", len(queue)) //this must be 0
    
    }    
    

    解决方案

    There's runtime.NumGoroutine but you're approaching this wrong.

    1. Your loops will keep spawning goroutines.
    2. this will unnecessarily burn cpu cycles because of the for loop.

    One approach is to use a sync.WaitGroup.

    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
    }
    

    playground

    这篇关于计数/显示活动goroutine的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆