码迷,mamicode.com
首页 > 其他好文 > 详细

go 并发

时间:2020-12-30 10:49:19      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:class   div   import   work   bye   ons   tar   started   finish   

占位...

from https://gobyexample.com/worker-pools

package main

import (
    "fmt"
    "time"
)

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

func main() {

    const numJobs = 5
    jobs := make(chan int, numJobs)
    results := make(chan int, numJobs)

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

    for j := 1; j <= numJobs; j++ {
        jobs <- j
    }
    close(jobs)

    for a := 1; a <= numJobs; a++ {
        <-results
    }
}

  

go 并发

标签:class   div   import   work   bye   ons   tar   started   finish   

原文地址:https://www.cnblogs.com/eiguleo/p/14185966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!