1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
run.go 958 Bytes
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-08-14 23:34 . queue
package queue
import (
"gitee.com/h79/goutils/common/system"
"sync"
"time"
)
type Call struct {
PopCount int
Duration time.Duration
Wait func(dur time.Duration)
Begin func(count int) any
Process func(be any, data any)
Quited func(lock sync.Locker, qu Queue)
}
func Run(c Call, lock sync.Locker, qu Queue) {
var count int
var popCount = c.PopCount
if popCount <= 0 {
popCount = 10
}
if c.Wait == nil {
c.Wait = func(dur time.Duration) {
time.Sleep(dur)
}
}
if c.Quited == nil {
c.Quited = func(lock sync.Locker, qu Queue) {
}
}
ten := make([]any, popCount)
for {
count = 0
lock.Lock()
for i := 0; i < popCount; i++ {
v := qu.Pop()
if v == nil {
break
}
ten[i] = v
count++
}
lock.Unlock()
var bn = c.Begin(count)
for i := 0; i < count; i++ {
c.Process(bn, ten[i])
}
c.Wait(c.Duration)
select {
case <-system.Closed():
c.Quited(lock, qu)
return
default:
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.21.16

搜索帮助