You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
436 B
Go
29 lines
436 B
Go
4 years ago
|
package threading
|
||
|
|
||
|
import (
|
||
|
"zero/core/lang"
|
||
|
"zero/core/rescue"
|
||
|
)
|
||
|
|
||
|
type TaskRunner struct {
|
||
|
limitChan chan lang.PlaceholderType
|
||
|
}
|
||
|
|
||
|
func NewTaskRunner(concurrency int) *TaskRunner {
|
||
|
return &TaskRunner{
|
||
|
limitChan: make(chan lang.PlaceholderType, concurrency),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (rp *TaskRunner) Schedule(task func()) {
|
||
|
rp.limitChan <- lang.Placeholder
|
||
|
|
||
|
go func() {
|
||
|
defer rescue.Recover(func() {
|
||
|
<-rp.limitChan
|
||
|
})
|
||
|
|
||
|
task()
|
||
|
}()
|
||
|
}
|