Workspace
  • Introduction
  • Algorithm
    • 二叉树
    • 排序算法
  • Basic Knowledge
    • CAP定理
    • CAS-SSO-单点登陆
    • 单向认证-双向认证
  • CICD
  • Cloud Native
  • Docker
    • Docker特性
    • Docker资源隔离
  • Golang
    • Standard Library
      • Archive
        • Builtin
        • Zip
    • Golang-container包
    • Golang-fallthrough关键字
    • Golang For Slect
    • Golang-Goroutine泄露
    • Golang Interface
    • Golang-json.Unmarshal
    • Golang Label
    • Golang Map String Struct
    • Golang Map To Struct
    • Golang Override Package Function
    • Golang-Slice删除元素
    • Golang Switch
    • Golang-sync.Cond
    • Golang-sync.Map
    • Golang-sync.once
    • Golang-type关键字
    • Golang-代码生成
    • golang-并发数控制
    • Golang-并发退出
    • Golang-插件系统
    • Golang-继承
    • Golang之channel
    • Golang之continue
    • Golang之make与new和nil
    • Golang之map
    • Golang之reflect
    • Golang之类型判断
    • Golang代码质量检测
    • Golang变量避坑
    • Golang字符串遍历
    • golang并发控制代码示例
    • Golang性能优化
    • Golang死锁
    • goroutine-协程-线程-进程
    • go值传递
    • go内存逃逸分析
    • go并发MGP模式
    • go并发控制
    • 垃圾回收-三色法
  • Istio
    • 服务网格
  • Jenkins
    • Jenkin On K 8 S
    • Jenkins Mac
  • Kubernetes
    • Deployment
    • k8s容器内查看-cpu-memory分配情况
    • kube-proxy原理
    • Kubernetes Informers
    • Kubernetes扩展点
    • Kubernetes部署策略
    • Pod Non Root
    • Pod驱逐
    • PV PVC Storage Class
    • Security Context
    • 优雅热更新
  • Python
    • Python-vs-Golang协程区别
  • Serviceless
  • Shell
    • Shell小技巧
  • VPN
    • OC Serv
  • Redis
Powered by GitBook
On this page

Was this helpful?

  1. Golang

golang-并发数控制

用 sync.WaitGroup 和 channel 在一起实现

    ch := make(chan int, 10)
    var wg sync.WaitGroup
    for i:=0;i<10;i++{
        go func() {
            for i2 := range ch {
                fmt.Println("work start : ", i2)
                wg.Done()
            }
        }()
    }

    jobCount := 20
    for i:=0;i<jobCount;i++{
        ch<- 1
        wg.Add(1)
        fmt.Printf("job No.: %d gouroutine No.: %d\n", i, runtime.NumGoroutine() )
    }
    wg.Wait()

控制十个并发数,如果其中一个goutine有错误,就退出,但要执行完正在执行的任务。

func main() {
    consumer([]int{1, 2, 3, 4, 5, 6, 9, -11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10, -1})
}

func consumer(task []int) error {
    ch := make(chan int, 10)
    exitch := make(chan int)
    var wg sync.WaitGroup
    for i := 0; i < cap(10); i++ {
        go func() {
            for i := range ch {
                wg.Done()
                fmt.Println("Start task :", i)
                if err := handerFunc(i); err != nil {
                    exitch <- 1
                }

            }
        }()
    }
    for _, v := range task {
        flagexit := false
        select {
        case <-exitch:
            flagexit = true
        default:
            ch <- v
            wg.Add(1)
            fmt.Println("task queue :", v)
        }
        if flagexit {
            break
        }
    }

    wg.Wait()
    return nil
}
func handerFunc(v int) error {
    if v > 0 {
        return nil
    }
    return errors.New("error v <= 0")
}
PreviousGolang-代码生成NextGolang-并发退出

Last updated 4 years ago

Was this helpful?