Created
October 30, 2019 10:27
-
-
Save jayjayswal/c05b99cbc2df73cffde04aca0de5197d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"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() { | |
job := make(chan int, 10) | |
result := make(chan int, 10) | |
for w := 1; w <= 2; w++ { | |
go worker(w, job, result) | |
} | |
for j := 1; j <= 9; j++ { | |
job <- j | |
} | |
close(job) | |
for a := 1; a <= 9; a++ { | |
<-result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment