import (
"fmt"
"sync"
)
type Job interface {
Do()
}
type Worker struct {
id int
jobChannel chan Job
done chan bool
}
type Pool struct {
workers []*Worker
jobQueue chan Job
wg sync.WaitGroup
}
func NewWorker(id int, wg *sync.WaitGroup) *Worker {
worker := Worker{
id: id,
jobChannel: make(chan Job),
done: make(chan bool),
}
go func() {
for job := range worker.jobChannel {
job.Do()
}
wg.Done()
}()
return &worker
}
func NewPool(numWorkers int) *Pool {
pool := Pool{
workers: make([]*Worker, numWorkers),
jobQueue: make(chan Job),
}
for i := 0; i