gqueue新增Close方法便于队列优雅退出

This commit is contained in:
John
2018-02-02 14:56:02 +08:00
parent 3c41e14a7e
commit 45bf89b61a
5 changed files with 50 additions and 2 deletions

View File

@ -48,6 +48,11 @@ func (q *IntQueue) Pop() int {
return 0
}
// 关闭队列(通知所有通过Pop阻塞的协程退出)
func (q *IntQueue) Close() {
q.events <- struct{}{}
}
// 获取当前队列大小
func (q *IntQueue) Size() int {
return len(q.events)

View File

@ -49,6 +49,11 @@ func (q *InterfaceQueue) Pop() interface{} {
return nil
}
// 关闭队列(通知所有通过Pop阻塞的协程退出)
func (q *InterfaceQueue) Close() {
q.events <- struct{}{}
}
// 获取当前队列大小
func (q *InterfaceQueue) Size() int {
return len(q.events)

View File

@ -48,6 +48,11 @@ func (q *StringQueue) Pop() string {
return ""
}
// 关闭队列(通知所有通过Pop阻塞的协程退出)
func (q *StringQueue) Close() {
q.events <- struct{}{}
}
// 获取当前队列大小
func (q *StringQueue) Size() int {
return len(q.events)

View File

@ -48,6 +48,11 @@ func (q *UintQueue) Pop() uint {
return 0
}
// 关闭队列(通知所有通过Pop阻塞的协程退出)
func (q *UintQueue) Close() {
q.events <- struct{}{}
}
// 获取当前队列大小
func (q *UintQueue) Size() int {
return len(q.events)

View File

@ -1,8 +1,36 @@
package main
import "gitee.com/johng/gf/g/encoding/gparser"
import (
"fmt"
"time"
)
func main() {
gparser.Load("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/config.yml")
events1 := make(chan int, 100)
events2 := make(chan int, 100)
go func() {
for{
select {
case t1 := <-events1:
fmt.Println(t1)
case t2 := <-events2:
fmt.Println(t2)
}
}
}()
go func() {
time.Sleep(2*time.Second)
events1 <- 1
events2 <- 2
time.Sleep(2*time.Second)
close(events1)
close(events2)
}()
select {
}
}