zoukankan      html  css  js  c++  java
  • golang协程数量控制

    协程数量控制

    在Golang中,Goroutine虽然很好,但是数量太多了,往往会带来很多麻烦,比如耗尽系统资源导致程序崩溃,或者CPU使用率过高导致系统忙不过来。所以我们可以限制下Goroutine的数量,这样就需要在每一次执行go之前判断goroutine的数量,如果数量超了,就要阻塞go的执行。第一时间想到的就是使用通道。每次执行的go之前向通道写入值,直到通道满的时候就阻塞了.

    package main
    
    import (
    	"fmt"
    	"runtime"
    	"sync"
    	"time"
    )
    // Pool Goroutine Pool
    type Pool struct {
    	queue chan int
    	wg *sync.WaitGroup
    }
    // New 新建一个协程池
    func NewPool(size int) *Pool{
    	if size <=0{
    		size = 1
    	}
    	return &Pool{
    		queue:make(chan int,size),
    		wg:&sync.WaitGroup{},
    	}
    }
    // Add 新增一个执行
    func (p *Pool)Add(delta int){
    	// delta为正数就添加
    	for i :=0;i<delta;i++{
    		p.queue <-1
    	}
    	// delta为负数就减少
    	for i:=0;i>delta;i--{
    		<-p.queue
    	}
    	p.wg.Add(delta)
    }
    // Done 执行完成减一
    func (p *Pool) Done(){
    	<-p.queue
    	p.wg.Done()
    }
    // Wait 等待Goroutine执行完毕
    func (p *Pool) Wait(){
    	p.wg.Wait()
    }
    
    func main(){
    	// 这里限制5个并发
    	pool := NewPool(5)
    	fmt.Println("the NumGoroutine begin is:",runtime.NumGoroutine())
    	for i:=0;i<20;i++{
    		pool.Add(1)
    		go func(i int) {
    			time.Sleep(time.Second)
    			fmt.Println("the NumGoroutine continue is:",runtime.NumGoroutine())
    			pool.Done()
    		}(i)
    	}
    	pool.Wait()
    	fmt.Println("the NumGoroutine done is:",runtime.NumGoroutine())
    }
    
    【励志篇】: 古之成大事掌大学问者,不惟有超世之才,亦必有坚韧不拔之志。
  • 相关阅读:
    VMware WorkStation 用 VMTools 官方下载地址 windows-vmtools tools-windows
    LeetCode Golang 9.回文数
    CentOS6.5中配置Rabbitmq3.6.6集群方案
    python之lambda、filter、map、reduce的用法讲解
    跨主机容器之间通信实现方式:etcd+flanned
    mongo3.4安装
    centos 时区的更改 UTC TO CST
    Elasticsearch5安装
    docker1.*.*版本安装
    使用weave来实现多宿主机中的docker容器之间通信
  • 原文地址:https://www.cnblogs.com/tomtellyou/p/14802617.html
Copyright © 2011-2022 走看看