2024-12-03 10:10:02 +08:00
|
|
|
|
package lock_free
|
|
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
|
|
type DelayLkQueue[T any] struct {
|
|
|
|
|
LkQueue[T]
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 10:25:34 +08:00
|
|
|
|
// NewDelayLkQueue 创建延迟无锁队列
|
|
|
|
|
func NewDelayLkQueue[T any]() *DelayLkQueue[T] {
|
|
|
|
|
return &DelayLkQueue[T]{*NewLkQueue[T]()}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 10:10:02 +08:00
|
|
|
|
// DelayEnqueue 延迟入队
|
|
|
|
|
func (q *DelayLkQueue[T]) DelayEnqueue(value T, duration time.Duration) {
|
|
|
|
|
time.AfterFunc(duration, func() {
|
|
|
|
|
q.Enqueue(value)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ContinuousDequeue 持续监听出队通知
|
|
|
|
|
func (q *DelayLkQueue[T]) ContinuousDequeue(notify ...chan T) {
|
|
|
|
|
for {
|
|
|
|
|
value, ok := q.Dequeue()
|
|
|
|
|
if !ok {
|
2024-12-03 10:13:05 +08:00
|
|
|
|
time.Sleep(time.Millisecond) // 队列为空,休眠1毫秒
|
2024-12-03 10:10:02 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, n := range notify {
|
|
|
|
|
n <- value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|