-
Notifications
You must be signed in to change notification settings - Fork 0
/
c.go
54 lines (48 loc) · 992 Bytes
/
c.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
52
53
54
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
func worker(id int, jobs <-chan int, results chan<- int) {
for job := range jobs {
fmt.Printf("Worker %d processing job %d\n", id, job)
r := rand.Intn(10)
fmt.Printf("sleep %d\n", r)
time.Sleep(time.Duration(r) * time.Second)
results <- job * 2
}
}
func main() {
numJobs := 10
numWorkers := 1
jobs := make(chan int, numJobs)
results := make(chan int, numJobs)
var wg sync.WaitGroup
// Start worker goroutines
for i := 1; i <= numWorkers; i++ {
wg.Add(1)
go func(workerID int) {
defer wg.Done()
worker(workerID, jobs, results)
}(i)
}
fmt.Print("enqueue jobs")
// Enqueue jobs
for i := 1; i <= numJobs; i++ {
jobs <- i
}
close(jobs)
fmt.Print("wait for all workers to finish")
// Wait for all workers to finish
go func() {
wg.Wait()
close(results)
}()
fmt.Print("collect all result \n")
// Collect results
for result := range results {
fmt.Printf("Result: %d\n", result)
}
}