From dc6aca46d4a878d99aafc862e42956d28bc39305 Mon Sep 17 00:00:00 2001 From: lixizan Date: Sat, 10 Dec 2022 11:28:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8057355..997ed87 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,21 @@ ## Concurrency -#### WorkerQueue - [![Build Status](https://github.com/lxzan/concurrency/workflows/Go%20Test/badge.svg?branch=master)](https://github.com/lxzan/concurrency/actions?query=branch%3Amaster) -> 工作队列, 可以不断往里面添加任务, 一旦有CPU资源空闲就去执行 +#### Feature +- 最大并发协程数量限制 +- 支持 `contex.Contex` +- 支持 `panic recover`, 返回包含错误堆栈的 `error` +- 任务调度不依赖 `time.Ticker` + + +#### Usage +- WorkerQueue 工作队列, 可以不断往里面添加任务, 一旦有CPU资源空闲就去执行 ```go package main import ( - "context" "fmt" "github.com/lxzan/concurrency" "time" @@ -39,8 +44,8 @@ func Mul(args interface{}) error { func main() { args1 := []int{1, 3} args2 := []int{1, 3, 5} - w := concurrency.NewWorkerQueue(context.Background(), 8) - w.Push( + w := concurrency.NewWorkerQueue() + w.AddJob( concurrency.Job{Args: args1, Do: Add}, concurrency.Job{Args: args1, Do: Mul}, concurrency.Job{Args: args2, Do: Add}, @@ -58,15 +63,12 @@ args=[1 3 5], ans=9 ``` -#### WorkerGroup - -> 工作组, 添加一组任务, 等待执行完成, 可以很好的替代`WaitGroup`. +- WorkerGroup 工作组, 添加一组任务, 等待执行完成, 可以很好的替代`WaitGroup`. ```go package main import ( - "context" "fmt" "github.com/lxzan/concurrency" "sync/atomic" @@ -74,9 +76,9 @@ import ( func main() { sum := int64(0) - w := concurrency.NewWorkerGroup(context.Background(), 4) + w := concurrency.NewWorkerGroup() for i := int64(1); i <= 10; i++ { - w.Push(concurrency.Job{ + w.AddJob(concurrency.Job{ Args: i, Do: func(args interface{}) error { fmt.Printf("%v ", args) @@ -88,7 +90,6 @@ func main() { w.StartAndWait() fmt.Printf("sum=%d\n", sum) } - ``` ```