2018-12-04 19:26:46 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2019-04-03 00:03:46 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/container/gqueue"
|
|
|
|
|
"github.com/gogf/gf/os/gtimer"
|
2018-12-04 19:26:46 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2019-04-03 00:03:46 +08:00
|
|
|
|
q := gqueue.New()
|
|
|
|
|
// 数据生产者,每隔1秒往队列写数据
|
|
|
|
|
gtimer.SetInterval(time.Second, func() {
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
q.Push(i)
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-12-04 19:26:46 +08:00
|
|
|
|
|
2019-04-03 00:03:46 +08:00
|
|
|
|
// 消费者,不停读取队列数据并输出到终端
|
|
|
|
|
for {
|
|
|
|
|
if v := q.Pop(); v != nil {
|
|
|
|
|
fmt.Println(" Pop:", v)
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-04 19:26:46 +08:00
|
|
|
|
}
|