所属分类:php教程
GOPROXY=https://goproxy.cn go get -v github.com/lxzan/concurrency@latest
登录后复制
contex.Contex
panic recover
, 返回包含错误堆栈的 error
time.Ticker
和 channel
WaitGroup
.package mainimport (
"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
登录后复制
package mainimport (
"fmt"
"github.com/lxzan/concurrency"
"time")func Add(args interface{}) error {
arr := args.([]int)
ans := 0
for _, item := range arr {
ans += item }
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 }
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)}
登录后复制
args=[1 3], ans=4args=[1 3 5], ans=15args=[1 3], ans=3args=[1 3 5], ans=9
登录后复制
以上就是分享一个非常好用的GO并发控制库 !的详细内容,更多请关注zzsucai.com其它相关文章!