2018-09-07 17:48:19 +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/gtime"
|
|
|
|
|
"github.com/gogf/gf/os/gtimer"
|
2018-09-07 17:48:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2019-04-03 00:03:46 +08:00
|
|
|
|
q := gqueue.New()
|
|
|
|
|
// 数据生产者,每隔1秒往队列写数据
|
|
|
|
|
gtimer.SetInterval(time.Second, func() {
|
|
|
|
|
v := gtime.Now().String()
|
|
|
|
|
q.Push(v)
|
|
|
|
|
fmt.Println("Push:", v)
|
|
|
|
|
})
|
2018-09-07 17:48:19 +08:00
|
|
|
|
|
2019-04-03 00:03:46 +08:00
|
|
|
|
// 3秒后关闭队列
|
|
|
|
|
gtimer.SetTimeout(3*time.Second, func() {
|
|
|
|
|
q.Close()
|
|
|
|
|
})
|
2018-09-07 17:48:19 +08:00
|
|
|
|
|
2019-04-03 00:03:46 +08:00
|
|
|
|
// 消费者,不停读取队列数据并输出到终端
|
|
|
|
|
for {
|
|
|
|
|
if v := q.Pop(); v != nil {
|
|
|
|
|
fmt.Println(" Pop:", v)
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-07 17:48:19 +08:00
|
|
|
|
}
|