代码拉取完成,页面将自动刷新
package routine
import (
"sync/atomic"
"time"
)
type functinType func(interface{}) error
// Worker is the actual executor who runs the tasks,
// it starts a goroutine that accepts tasks and
// performs function calls.
type Worker struct {
// pool who owns this routine.
pool *Pool
// task is a job should be done.
task chan functinType
// recycleTime will be update when putting a routine back into queue.
recycleTime time.Time
input chan interface{}
}
// run starts a goroutine to repeat the process
// that performs the function calls.
func (w *Worker) run() {
go func() {
//监听任务列表,一旦有任务立马取出运行
count := 1
var input interface{}
var f functinType
for count <=2{
select {
case str_temp, ok := <- w.input:
if !ok {
return
}
count ++
input = str_temp
case f_temp, ok := <-w.task:
if !ok {
//如果接收到关闭
atomic.AddInt32(&w.pool.running, -1)
close(w.task)
return
}
count ++
f = f_temp
}
}
err := f(input)
if err != nil{
//fmt.Println("执行任务失败")
}
//回收复用
w.pool.putWorker(w)
return
}()
}
// stop this routine.
func (w *Worker) stop() {
w.sendTask(nil)
close(w.input)
}
// sendTask sends a task to this routine.
func (w *Worker) sendTask(task functinType) {
w.task <- task
}
func (w *Worker) sendarg(input interface{}) {
w.input <- input
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。