16 Star 69 Fork 55

ShirDon-廖显东/Go语言高级开发与实战-随书代码

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
4.4.5-goroutine1.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
ShirDon-廖显东 提交于 2021-12-28 09:45 +08:00 . commit
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言高级开发与实战》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 知乎:https://www.zhihu.com/people/shirdonl
// 公众号:源码大数据
// 仓库地址:https://gitee.com/shirdonl/goAdvanced
// 仓库地址:https://github.com/shirdonl/goAdvanced
//++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
"sync"
"sync/atomic"
)
//任务处理器
type TaskHandler func(interface{})
//定义任务结构体
type Task struct {
Param interface{}
Handler TaskHandler
}
//协程池接口
type WorkerPoolImpl interface {
AddWorker() // 增加 worker
SendTask(Task) // 发送任务
Release() // 释放
}
//协程池
type WorkerPool struct {
wg sync.WaitGroup
inCh chan Task
}
//添加worker
func (d *WorkerPool) AddWorker() {
d.wg.Add(1)
go func() {
for task := range d.inCh {
task.Handler(task.Param)
}
d.wg.Done()
}()
}
//释放
func (d *WorkerPool) Release() {
close(d.inCh)
d.wg.Wait()
}
//发送任务
func (d *WorkerPool) SendTask(t Task) {
d.inCh <- t
}
//实例化
func NewWorkerPool(buffer int) WorkerPoolImpl {
return &WorkerPool{
inCh: make(chan Task, buffer),
}
}
func main() {
//设置缓冲大小
bufferSize := 100
var workerPool = NewWorkerPool(bufferSize)
workers := 4
for i := 0; i < workers; i++ {
workerPool.AddWorker()
}
var sum int32
testFunc := func(i interface{}) {
n := i.(int32)
atomic.AddInt32(&sum, n)
}
var i, n int32
n = 100
for ; i < n; i++ {
task := Task{
i,
testFunc,
}
workerPool.SendTask(task)
}
workerPool.Release()
fmt.Println(sum)
}
//4950
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shirdonl/goAdvanced.git
git@gitee.com:shirdonl/goAdvanced.git
shirdonl
goAdvanced
Go语言高级开发与实战-随书代码
0f051d9f4e35

搜索帮助