计数/显示活动 goroutine 的数量

我有一个队列和一个既可以出队又可以入队的函数。我想确保正确数量的 goroutines 在队列上运行,只要列表中有东西。

这是我正在使用的代码,但我想知道是否有办法打印当前活动的 goroutines 的数量

我正在从 localhost 读取 json 文档并尝试将其转换为Test类型:


type Test struct {

    one string

    two string

    three string

}


res, err := http.Get("http://localhost/d/")

perror(err)

defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)

perror(err)

var data Test

err = json.Unmarshal(body, &data)

if err != nil {

    fmt.Printf("%T\n%s\n%#v\n",err, err, err)

    switch v := err.(type){

    case *json.SyntaxError:

        fmt.Println(string(body[v.Offset - 40:v.Offset]))

    }

}



fmt.Println("response:")

fmt.Println(string(body))

fmt.Println("type:")

fmt.Println(data)

但输出显示一个空对象:


response:

{

    "one" : "one thing",

    "two" : "two things",

    "three" : "3 things"


}

type:

{  }

我究竟做错了什么?


噜噜哒
浏览 244回答 1
1回答

天涯尽头无女友

有,runtime.NumGoroutine但你接近这个错误。您的循环将不断产生 goroutine。由于 for 循环,这将不必要地消耗 CPU 周期。一种方法是使用sync.WaitGroup。func deen(wg *sync.WaitGroup, queue chan int) {&nbsp; &nbsp; for element := range queue {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("element is ", element)&nbsp; &nbsp; &nbsp; &nbsp; if element%2 == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("new element is ", element)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wg.Add(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queue <- (element*100 + 11)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queue <- (element*100 + 33)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; wg.Done()&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; queue := make(chan int, 10)&nbsp; &nbsp; queue <- 1&nbsp; &nbsp; queue <- 2&nbsp; &nbsp; queue <- 3&nbsp; &nbsp; queue <- 0&nbsp; &nbsp; for i := 0; i < 4; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go deen(&wg, queue)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; close(queue)&nbsp; &nbsp; fmt.Println("list len", len(queue)) //this must be 0}playground--- 带有比赛的旧越野车版本---func deen(wg *sync.WaitGroup, queue chan int) {&nbsp; &nbsp; for element := range queue {&nbsp; &nbsp; &nbsp; &nbsp; wg.Done()&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("element is ", element)&nbsp; &nbsp; &nbsp; &nbsp; if element%2 == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("new element is ", element)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wg.Add(2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queue <- (element*100 + 11)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queue <- (element*100 + 33)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; queue := make(chan int, 10)&nbsp; &nbsp; queue <- 1&nbsp; &nbsp; queue <- 2&nbsp; &nbsp; queue <- 3&nbsp; &nbsp; queue <- 0&nbsp; &nbsp; for i := 0; i < 4; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go deen(&wg, queue)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; close(queue)&nbsp; &nbsp; fmt.Println("list is has len", len(queue)) //this must be 0}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go