-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
47 lines (36 loc) · 705 Bytes
/
worker.go
File metadata and controls
47 lines (36 loc) · 705 Bytes
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
package main
import "log"
type Worker interface {
Run(a, b int) int
}
type WorkerPool struct {
TaskQueue *TaskQueue
}
func (pool *WorkerPool) AddWorker(w Worker) {
go func() {
for elem := range pool.TaskQueue.queue {
args := elem.Args
a := args.A
b := args.B
res := w.Run(a, b)
elem.Result <- TaskResult{Result: res}
close(elem.Result)
}
}()
}
type WorkerAdd struct {
name string
}
func (w *WorkerAdd) Run(a, b int) int {
res := a + b
log.Printf("DEBUG: worker=%s result=%d", w.name, res)
return res
}
type WorkerSub struct {
name string
}
func (w *WorkerSub) Run(a, b int) int {
res := a - b
log.Printf("DEBUG: worker=%s result=%d", w.name, res)
return res
}