-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobqueue.go
51 lines (41 loc) · 985 Bytes
/
jobqueue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"flag"
"fmt"
"sync"
"github.com/justindh/fizzbuzz/fizzbuzz"
)
var wg sync.WaitGroup
func main() {
// get cli args
amount := flag.Int("amount", 16, "Amount of Numbers to FizzBuzz test")
workers := flag.Int("workers", 1, "Amount of concurrent workers")
flag.Parse()
wg.Add(*amount)
// create the job queue
jobs := make(chan int, *amount)
// please to collect the results
results := make(chan string, *amount)
// spin up workers that will wait for jobs
for w := 1; w <= *workers; w++ {
go worker(w, jobs, results)
}
// build the job queue
for j := 1; j <= *amount; j++ {
jobs <- j
}
close(jobs)
wg.Wait()
for i := 1; i <= *amount; i++ {
fmt.Println(<-results)
//<-results
}
}
func worker(workerid int, jobs <-chan int, results chan<- string) {
for j := range jobs {
//log.Printf("Worker %d starting %d", workerid, j)
results <- fizzbuzz.Fizzbuzz(j)
//log.Printf("Worker %d finished %d", workerid, j)
wg.Done()
}
}