golang 线程池

前端之家收集整理的这篇文章主要介绍了golang 线程池前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main

import "fmt"

func worker(id int,jobs chan int,results chan int) {
        for j := range jobs {
                fmt.Println("worker",id,"procesing job",j)
                results <- j * 2
        }
}

func main() {
        job    := make(chan int,100)
        result := make(chan int,200)

        for w := 1; w <=3; w++ {
                go worker(w,job,result)
        }

        for j := 1; j <= 9; j++ {
                job <- j
        }

        for a := 1; a <= 9; a++ {
                fmt.Println(<-result)
        }
}
原文链接:https://www.f2er.com/go/188757.html

猜你在找的Go相关文章