package main
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 < numWorkers; i++ {
pool.workers[i] = NewWorker(i, &pool.wg)
}
go pool.run()
return &pool
}
func (w *Worker) Start(job Job) {
w.jobChannel <- job
}
func (w *Worker) Stop() {
close(w.jobChannel)
<-w.done
}
func (p *Pool) run() {
for job := range p.jobQueue {
worker := p.getAvailableWorker()
worker.Start(job)
}
for _, worker := range p.workers {
worker.Stop()
}
p.wg.Done()
}
func (p *Pool) getAvailableWorker() *Worker {
for {
for _, worker := range p.workers {
select {
case <-worker.done:
default:
return worker
}
}
}
}
func (p *Pool) Submit(job Job) {
p.wg.Add(1)
p.jobQueue <- job
}
func (p *Pool) Shutdown() {
close(p.jobQueue)
p.wg.Wait()
}
type PrintJob struct {
id int
}
func (pj PrintJob) Do() {
fmt.Printf("Printing job %d\n", pj.id)
}
func main() {
pool := NewPool(5)
for i := 0; i < 10; i++ {
pool.Submit(PrintJob{id: i})
}
pool.Shutdown()
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.