Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "runtime"
- "time"
- )
- func worker(id int, jobs <-chan int, results chan<- int) {
- for j := range jobs {
- fmt.Println("worker ", id, "processing job ", j)
- time.Sleep(time.Second)
- results <- j * 2
- }
- }
- func main() {
- runtime.GOMAXPROCS(runtime.NumCPU())
- jobs := make(chan int, 100)
- results := make(chan int, 100)
- for j := 1; j <= 11; j++ {
- jobs <- j
- }
- close(jobs)
- for w := 1; w <= 4; w++ {
- go worker(w, jobs, results)
- }
- res := 0
- for j := 1; j <= 11; j++ {
- res += <-results
- }
- fmt.Println("results ", res)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement