package main
import (
"fmt"
"time"
)
type GoPool struct {
MaxLimit int
tokenChan chan struct{}
}
type GoPoolOption func(*GoPool)
func WithMaxLimit(max int) GoPoolOption {
return func(gp *GoPool){
gp.MaxLimit = max
gp.tokenChan = make(chan struct{},gp.MaxLimit)
for i:=0;i<gp.MaxLimit;i++{
gp.tokenChan <- struct{}{}
}
}
}
func NewGoPool(options ...GoPoolOption) *GoPool {
p := &GoPool{}
for _,o := range options{
o(p)
}
return p
}
func (gp *GoPool) Submit(fn func()){
token := <- gp.tokenChan
go func(){
fn()
gp.tokenChan <- token
}()
}
func (gp *GoPool) Wait(){
for i:=0 ;i<gp.MaxLimit;i++{
<- gp.tokenChan
}
close(gp.tokenChan)
}
func (gp *GoPool) size() int {
return len(gp.tokenChan)
}
func main() {
gopool := NewGoPool(WithMaxLimit(1), WithMaxLimit(4))
defer gopool.Wait()
for i:=0;i<5;i++ {
fmt.Println("每个任何开启之前通道的大小:",gopool.size())
gopool.Submit(func() {
fmt.Println("qian",gopool.size())
time.Sleep(2 * time.Second)
fmt.Println(time.Now())
fmt.Println("hou",gopool.size())
})
}
}