go-study/lock_free/delay_queue.go

29 lines
512 B
Go

package lock_free
import "time"
type DelayLkQueue[T any] struct {
LkQueue[T]
}
// 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 {
time.Sleep(time.Millisecond) // 自旋
continue
}
for _, n := range notify {
n <- value
}
}
}