2 Star 0 Fork 336

hxchjm / go-zero

forked from kevwan / go-zero 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
pool.go 965 Bytes
一键复制 编辑 原始数据 按行查看 历史
kevwan 提交于 2020-08-08 16:40 . update package reference
package main
import (
"bufio"
"fmt"
"os"
"sync"
"sync/atomic"
"time"
"github.com/tal-tech/go-zero/core/lang"
"github.com/tal-tech/go-zero/core/syncx"
)
func main() {
var count int32
var consumed int32
pool := syncx.NewPool(80, func() interface{} {
fmt.Printf("+ %d\n", atomic.AddInt32(&count, 1))
return 1
}, func(interface{}) {
fmt.Printf("- %d\n", atomic.AddInt32(&count, -1))
}, syncx.WithMaxAge(time.Second))
var waitGroup sync.WaitGroup
quit := make(chan lang.PlaceholderType)
waitGroup.Add(100)
for i := 0; i < 100; i++ {
go func() {
defer func() {
waitGroup.Done()
fmt.Println("routine quit")
}()
for {
select {
case <-quit:
return
default:
x := pool.Get().(int)
atomic.AddInt32(&consumed, 1)
pool.Put(x)
}
}
}()
}
bufio.NewReader(os.Stdin).ReadLine()
close(quit)
fmt.Println("quitted")
waitGroup.Wait()
fmt.Printf("consumed %d\n", atomic.LoadInt32(&consumed))
}
Go
1
https://gitee.com/hxchjm/go-zero.git
git@gitee.com:hxchjm/go-zero.git
hxchjm
go-zero
go-zero
3eb88c4e4bf1

搜索帮助