Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Mar 24, 2023
1 parent e90131e commit f25a6a9
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 375 deletions.
77 changes: 24 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,40 @@
GOPROXY=https://goproxy.cn go get -v github.com/lxzan/concurrency@latest
```

#### Feature

- 最大并发协程数量限制
- 支持 `contex.Contex`
- 支持 `panic recover`, 返回包含错误堆栈的 `error`
- 任务调度不依赖 `time.Ticker`

#### Usage

- WorkerQueue 工作队列, 可以不断往里面添加任务, 一旦有CPU资源空闲就去执行
- WorkerGroup 任务组, 添加一组任务, 等待执行完成, 可以很好的替代`WaitGroup`.

```go
package main

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

func Add(args interface{}) error {
arr := args.([]int)
ans := 0
for _, item := range arr {
ans += item
func main() {
sum := int64(0)
w := concurrency.NewWorkerGroup[int64]()
for i := int64(1); i <= 10; i++ {
w.Push(i)
}
fmt.Printf("args=%v, ans=%d\n", args, ans)
return nil
}

func Mul(args interface{}) error {
arr := args.([]int)
ans := 1
for _, item := range arr {
ans *= item
w.OnMessage = func(args int64) error {
fmt.Printf("%v ", args)
atomic.AddInt64(&sum, args)
return nil
}
fmt.Printf("args=%v, ans=%d\n", args, ans)
return nil
}

func main() {
args1 := []int{1, 3}
args2 := []int{1, 3, 5}
w := concurrency.NewWorkerQueue()
w.AddJob(
concurrency.Job{Args: args1, Do: Add},
concurrency.Job{Args: args1, Do: Mul},
concurrency.Job{Args: args2, Do: Add},
concurrency.Job{Args: args2, Do: Mul},
)
w.StopAndWait(30 * time.Second)
w.Start()
fmt.Printf("sum=%d\n", sum)
}
```

```
args=[1 3], ans=4
args=[1 3 5], ans=15
args=[1 3], ans=3
args=[1 3 5], ans=9
4 5 6 7 8 9 10 1 3 2 sum=55
```

- WorkerGroup 工作组, 添加一组任务, 等待执行完成, 可以很好的替代`WaitGroup`.
- WorkerQueue 任务队列, 可以不断往里面添加任务, 一旦有CPU资源空闲就去执行

```go
package main
Expand All @@ -81,26 +53,25 @@ import (
"fmt"
"github.com/lxzan/concurrency"
"sync/atomic"
"time"
)

func main() {
sum := int64(0)
w := concurrency.NewWorkerGroup()
w := concurrency.NewWorkerQueue()
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
},
var x = i
job := concurrency.FuncJob(func() {
fmt.Printf("%v ", x)
atomic.AddInt64(&sum, x)
})
w.Push(job)
}
w.StartAndWait()
w.Stop(time.Second)
fmt.Printf("sum=%d\n", sum)
}
```

```
4 5 6 7 8 9 10 1 3 2 sum=55
3 9 10 4 1 6 8 5 2 7 sum=55
```
16 changes: 8 additions & 8 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package concurrency
type (
// 任务抽象
Job interface {
Do() error
Do()
}

// 无参数的快捷任务
QuickJob func() error
FuncJob func()
)

type parameterizedJob struct {
args []interface{}
do func(args ...interface{}) error
args interface{}
do func(args interface{})
}

func (f QuickJob) Do() error {
return f()
func (f FuncJob) Do() {
f()
}

// 带参数任务
func ParameterizedJob(f func(args ...interface{}) error, args ...interface{}) Job {
func ParameterizedJob(args interface{}, f func(args interface{})) Job {
return &parameterizedJob{args: args, do: f}
}

func (c *parameterizedJob) Do() error { return c.do(c.args...) }
func (c *parameterizedJob) Do() { c.do(c.args) }
175 changes: 0 additions & 175 deletions map.go

This file was deleted.

Loading

0 comments on commit f25a6a9

Please sign in to comment.