1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package util
- /**
- 线程处理工具类
- **/
- import (
- "sync"
- )
- type Thead struct {
- Size int
- pool chan bool
- wait *sync.WaitGroup
- }
- func NewThreads(size int) *Thead {
- th := &Thead{size, make(chan bool, size), &sync.WaitGroup{}}
- return th
- }
- func (th *Thead) Open() {
- th.pool <- true
- th.wait.Add(1)
- }
- func (th *Thead) Close() {
- <-th.pool
- th.wait.Add(-1)
- }
- func (th *Thead) Run(fn func(arg ...any), args ...any) {
- th.pool <- true
- go func() {
- defer func() {
- <-th.pool
- }()
- fn(args...)
- }()
- }
- func (th *Thead) Wait() {
- th.wait.Wait()
- }
|