https://github.com/handsomestwei/go-routine-pool
golang routine协程池,类似java的Executors
https://github.com/handsomestwei/go-routine-pool
executor fsm golang
Last synced: about 1 month ago
JSON representation
golang routine协程池,类似java的Executors
- Host: GitHub
- URL: https://github.com/handsomestwei/go-routine-pool
- Owner: handsomestWei
- License: apache-2.0
- Created: 2020-08-03T09:43:13.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T02:30:27.000Z (over 5 years ago)
- Last Synced: 2025-10-05T09:45:23.587Z (8 months ago)
- Topics: executor, fsm, golang
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-routine-pool
golang routine协程池,包含一个简单的有限状态机。类似java的Executors
# Usage
使用`pool.NewPool`创建不等待的协程池
```
// 初始化协程池,指定协程数
pool := pool.NewPool(2)
var n int32 = 0
for i := 0; i < 10; i++ {
n := atomic.AddInt32(&n, 1)
pool.Submit(func() {
fmt.Println(fmt.Sprintf("n=%d", n))
})
}
// 不用等待所有协程处理结束。直接往下处理
fmt.Println("end")
```
使用`pool.NewWaitPool`创建等待的协程池
```
// 初始化协程池,指定协程数
pool := pool.NewWaitPool(2)
var n int32 = 0
for i := 0; i < 10; i++ {
n := atomic.AddInt32(&n, 1)
pool.Submit(func() {
fmt.Println(fmt.Sprintf("n=%d", n))
})
}
// 等待
pool.Wait()
// 等所有协程处理结束,再继续往下处理
fmt.Println("end")
```