package main
import (
"log"
"sync"
"time"
// "github.com/gogf/gf/container/gpool"
)
type pool struct {
queue chan int
wg *sync.WaitGroup
}
func New(size int) *pool {
if size <= 0 {
size = 1
}
return &pool{
queue: make(chan int, size),
wg: &sync.WaitGroup{},
}
}
func (p *pool) Add(delta int) {
for i := 0; i < delta; i++ {
p.queue <- 1
}
for i := 0; i > delta; i-- {
<-p.queue
}
p.wg.Add(delta)
}
func (p *pool) Done() {
<-p.queue
p.wg.Done()
}
func (p *pool) Wait() {
p.wg.Wait()
}
func main() {
// 调整并发数量
pool := New(3)
// println(runtime.NumGoroutine())
for i := 0; i < 1000; i++ {
pool.Add(1)
go func() {
//这里写逻辑
log.Println(time.Now().Format("2006/01/02 15:04:05"))
time.Sleep(time.Second)
pool.Done()
}()
}
pool.Wait()
// println(runtime.NumGoroutine())
}
-
安装kubectl客户端
:下一篇 »