Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Dec 12, 2022
2 parents fb41eef + 4138893 commit c53a824
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,44 @@ GOPROXY=https://goproxy.cn go get -v github.com/lxzan/concurrency@latest
- 最大并发协程数量限制
- 支持 `contex.Contex`
- 支持 `panic recover`, 返回包含错误堆栈的 `error`
- 任务调度不依赖 `time.Ticker`
- 任务调度不依赖 `time.Ticker``channel`


#### Usage

- WorkerGroup 工作组, 添加一组任务, 等待执行完成, 可以很好的替代`WaitGroup`.

```go
package main

import (
"fmt"
"github.com/lxzan/concurrency"
"sync/atomic"
)

func main() {
sum := int64(0)
w := concurrency.NewWorkerGroup()
for i := int64(1); i <= 10; i++ {
w.AddJob(concurrency.Job{
Args: i,
Do: func(args interface{}) error {
fmt.Printf("%v ", args)
atomic.AddInt64(&sum, args.(int64))
return nil
},
})
}
w.StartAndWait()
fmt.Printf("sum=%d\n", sum)
}
```

```
4 5 6 7 8 9 10 1 3 2 sum=55
```

- WorkerQueue 工作队列, 可以不断往里面添加任务, 一旦有CPU资源空闲就去执行

```go
Expand Down Expand Up @@ -66,37 +100,3 @@ args=[1 3 5], ans=15
args=[1 3], ans=3
args=[1 3 5], ans=9
```


- WorkerGroup 工作组, 添加一组任务, 等待执行完成, 可以很好的替代`WaitGroup`.

```go
package main

import (
"fmt"
"github.com/lxzan/concurrency"
"sync/atomic"
)

func main() {
sum := int64(0)
w := concurrency.NewWorkerGroup()
for i := int64(1); i <= 10; i++ {
w.AddJob(concurrency.Job{
Args: i,
Do: func(args interface{}) error {
fmt.Printf("%v ", args)
atomic.AddInt64(&sum, args.(int64))
return nil
},
})
}
w.StartAndWait()
fmt.Printf("sum=%d\n", sum)
}
```

```
4 5 6 7 8 9 10 1 3 2 sum=55
```

0 comments on commit c53a824

Please sign in to comment.