Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "runtime"
  6. "time"
  7. )
  8.  
  9. func worker(id int, jobs <-chan int, results chan<- int) {
  10. for j := range jobs {
  11. fmt.Println("worker ", id, "processing job ", j)
  12. time.Sleep(time.Second)
  13. results <- j * 2
  14. }
  15. }
  16.  
  17. func main() {
  18. runtime.GOMAXPROCS(runtime.NumCPU())
  19. jobs := make(chan int, 100)
  20. results := make(chan int, 100)
  21.  
  22. for j := 1; j <= 11; j++ {
  23. jobs <- j
  24. }
  25. close(jobs)
  26.  
  27. for w := 1; w <= 4; w++ {
  28. go worker(w, jobs, results)
  29. }
  30.  
  31. res := 0
  32. for j := 1; j <= 11; j++ {
  33. res += <-results
  34. }
  35. fmt.Println("results ", res)
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement