成都网站建设设计

将想法与焦点和您一起共享

Golang并发数的控制

package main

import (
    "fmt"
    "sync"
    "time"
)

func main() {

    wg := &sync.WaitGroup{}

    limiter := make(chan bool, 10)
    for i := 0; i < 100; i++ {
        wg.Add(1)
        limiter <- true
        go download(i, limiter, wg)
    }
    wg.Wait()
}

func download(index int, limiter chan bool, wg *sync.WaitGroup) {
    defer wg.Done()
    fmt.Println("start to download :", index)
    time.Sleep(1 * time.Second)
    <-limiter
}

分享名称:Golang并发数的控制
当前网址:http://chengdu.cdxwcx.cn/article/joopep.html