按照Marcio 的线程池实现,是否可以确定所有工作何时完成?
等待 JobQueue 清空是微不足道的:
// Wait for the job queue to clear
for len(JobQueue) > 0 {
// Just wait
}
然而在那之后可能还有 goroutines 在等待 workers,或者 workers 还没有完成所有的任务:
func (d *Dispatcher) dispatch() {
for {
select {
case job := <-JobQueue:
// a job request has been received
go func(job Job) {
// try to obtain a worker job channel that is available.
// this will block until a worker is idle
jobChannel := <-d.WorkerPool
// dispatch the job to the worker job channel
jobChannel <- job
}(job)
}
}
}
最好的方法是什么?在dispatcher中添加一个WaitGroup,以及查询WaitGroup状态的方法?对此的任何指示将不胜感激。
幕布斯7119047
相关分类