zoukankan      html  css  js  c++  java
  • Buffered Channels and Worker Pools

    原文链接:https://golangbot.com/buffered-channels-worker-pools/
    

    buffered channels

    • 带有缓冲区的channel 只有在缓冲区满之后 channel才会阻塞

    WaitGroup

    • 如果有多个 goroutine在后台执行 那么需要在主线程中 多次等待 可以有一个简单的方法 就是 通过WaitGroup 可以控制 Goroutines 直到它们都执行完成

    例子

    import (  
        "fmt"
        "sync"
        "time"
    )
    
    func process(i int, wg *sync.WaitGroup) {  
        fmt.Println("started Goroutine ", i)
        time.Sleep(2 * time.Second)
        fmt.Printf("Goroutine %d ended
    ", i)
        wg.Done()
    }
    
    func main() {  
        no := 3
        var wg sync.WaitGroup
        for i := 0; i < no; i++ {
            wg.Add(1)
            go process(i, &wg)
        }
        wg.Wait()![](https://images2018.cnblogs.com/blog/736597/201803/736597-20180312115109926-1714494090.png)
    
    
        fmt.Println("All go routines finished executing")
    }
    

    Worker Pool Implementation

    先贴一下个人理解的 程序执行的流程图

    import (
    	"fmt"
    	"math/rand"
    	"sync"
    	"time"
    )
    
    type Job struct {
    	id       int
    	randomno int
    }
    type Result struct {
    	job         Job
    	sumofdigits int
    }
    
    var jobs = make(chan Job, 10)
    var results = make(chan Result, 10)
    
    func digits(number int) int {
    	sum := 0
    	no := number
    	for no != 0 {
    		digit := no % 10
    		sum += digit
    		no /= 10
    	}
    	time.Sleep(5 * time.Second)
    	return sum
    }
    func worker(wg *sync.WaitGroup) {
    	for job := range jobs {
    		output := Result{job, digits(job.randomno)}
    		results <- output
    	}
    	wg.Done()
    }
    func createWorkerPool(noOfWorkers int) {
    	var wg sync.WaitGroup
    	for i := 0; i < noOfWorkers; i++ {
    		wg.Add(1)
    		go worker(&wg)
    	}
    	wg.Wait()
    	close(results)
    }
    func allocate(noOfJobs int) {
    	for i := 0; i < noOfJobs; i++ {
    		randomno := rand.Intn(999)
    		job := Job{i, randomno}
    		jobs <- job
    	}
    	close(jobs)
    }
    func result(done chan bool) {
    	for result := range results {
    		fmt.Printf("Job id %d, input random no %d , sum of digits %d
    ", result.job.id, result.job.randomno, result.sumofdigits)
    	}
    	done <- true
    }
    
    func main() {
    	startTime := time.Now()
    
    	noOfJobs := 100
    	go allocate(noOfJobs)
    
    	done := make(chan bool)
    	go result(done)
    	noOfWorkers := 10
    	createWorkerPool(noOfWorkers)
    	<-done
    
    	endTime := time.Now()
    
    
    	diff := endTime.Sub(startTime)
    	fmt.Println("total time taken ", diff.Seconds(), "seconds")
    }
    
  • 相关阅读:
    用于创建和管理 Azure 虚拟机的常用 PowerShell 命令
    在 Azure Resource Manager 中为虚拟机设置密钥保管库
    使用 Azure 资源管理器向 Windows VM 应用策略
    Azure 门户中基于角色的访问控制入门
    为 Azure Resource Manager 中的虚拟机设置 WinRM 访问权限
    如何加密 Windows VM 上的虚拟磁盘
    适用于 Windows VM 的 Azure 示例基础结构演练
    Azure 中虚拟机的备份和还原选项
    1.1 基本算法和记号
    tomcat的class加载的优先顺序
  • 原文地址:https://www.cnblogs.com/alin-qu/p/8548512.html
Copyright © 2011-2022 走看看