Golang 通过 管道控制并发数量

 

package main

import (
    "fmt"
    "math/rand"
    "sync"
    "time"
)

const concurrency = 20

func main() {
    rateChannel := make(chan int, concurrency)
  // 别忘记 关闭管道 defer close(rateChannel)
// channel 控制并发数量 go func() { for { rateChannel <- 1 time.Sleep(time.Duration(1000)) } }() var wg sync.WaitGroup for{ <- rateChannel wg.Add(1) go func(wg *sync.WaitGroup) { rand.Seed(1) i := rand.Int() fmt.Println(i) }(&wg) } }

 

posted @ 2021-02-03 17:49  Black_Climber  阅读(291)  评论(0编辑  收藏  举报