Golang(九)简单 Goroutine 池实现
0. 前言
- 最近使用 Golang 写一个并发执行的测试脚本
- 之前习惯使用 Java,习惯性想先建一个线程池。然后意识到 Golang 没有封装好的线程池
- 结合之前学习的 Goroutine 原理和 Golang 大道至简的设计思想,可能 Goroutine 的开销和切换代价比较低,不需要对并发数有过多限制
- 但是 Goroutine 启动数量过多的话总感觉不太好,于是利用锁和通道实现了简单的线程池做并发控制,欢迎大家点评
- 源码地址:https://github.com/wangao1236/GoPool
1. 相关接口
- 接口仿照 Java 的 ExecutorService 和 Runnable 接口定义:
// Task represents an in-process Goroutine task.
type Task interface {
// Run method corresponds to Run method of Java's Runnable interface.
Run()
} // Executor defines the actions associated with the Goroutine pool.
type Executor interface {
// Execute method corresponds to Execute method of Java's ExecutorService interface.
Execute(task Task)
// Wait waits for all the tasks to complete.
Wait()
// Done returns a channel which is closed if all the tasks completed.
Done() chan struct{}
}
- 接口调用:
func TestNewExecutor(t *testing.T) {
t.Log(t.Name())
ex := NewExecutor()
for _, domain := range domains {
ex.Execute(&TestTask{
fmt.Sprintf("ping %s -c 10", domain),
})
}
ex.Wait()
}
- 首先定义一个 Executor,然后通过 Execute 传入 Task 对象,调用 Wait 方法等待所有任务结束
2. 具体实现
- 主要利用 sync.Mutex 和 []channel struct{} 维护一个等待执行的任务队列
- 任务传入时,等待一个 startCh 通道信号
- 对于符合执行条件(未设定并发数或者当前执行任务小于并发数时)关闭 startCh 信号,解除阻塞
- 任务执行完毕后,关闭 stopCh 信号,允许等待队列里的任务继续执行
- 对于所有任务执行完毕(正在执行数和等待执行数均为 0),关闭 done 信号,解除整个 Executor 的阻塞,表示所有任务执行完毕
- 部分代码如下:
package pkg import (
"sync"
) // Task represents an in-process Goroutine task.
type Task interface {
// Run method corresponds to Run method of Java's Runnable interface.
Run()
} // Executor defines the actions associated with the Goroutine pool.
type Executor interface {
// Execute method corresponds to Execute method of Java's ExecutorService interface.
Execute(task Task)
// Wait waits for all the tasks to complete.
Wait()
// Done returns a channel which is closed if all the tasks completed.
Done() chan struct{}
} type executor struct {
lock sync.Mutex
waitingTasks []chan struct{}
activeTasks int64
concurrencyLimit int64
done chan struct{}
} func (ex *executor) Execute(task Task) {
ex.start(task)
} func (ex *executor) Wait() {
<-ex.done
} func (ex *executor) Done() chan struct{} {
return ex.done
} func (ex *executor) start(task Task) {
startCh := make(chan struct{})
stopCh := make(chan struct{}) go startTask(startCh, stopCh, task)
ex.enqueue(startCh)
go ex.waitDone(stopCh) } // NewExecutor returns a new Executor.
func NewExecutor(concurrencyLimit int64) Executor {
ex := &executor{
waitingTasks: make([]chan struct{}, ),
activeTasks: ,
concurrencyLimit: concurrencyLimit,
done: make(chan struct{}),
}
return ex
} func startTask(startCh, stopCh chan struct{}, task Task) {
defer close(stopCh) <-startCh
task.Run()
} func (ex *executor) enqueue(startCh chan struct{}) {
ex.lock.Lock()
defer ex.lock.Unlock() if ex.concurrencyLimit == || ex.activeTasks < ex.concurrencyLimit {
close(startCh)
ex.activeTasks++
} else {
ex.waitingTasks = append(ex.waitingTasks, startCh)
}
} func (ex *executor) waitDone(stopCh chan struct{}) {
<-stopCh ex.lock.Lock()
defer ex.lock.Unlock() if len(ex.waitingTasks) == {
ex.activeTasks--
if ex.activeTasks == {
close(ex.done)
}
} else {
close(ex.waitingTasks[])
ex.waitingTasks = ex.waitingTasks[:]
}
}
- 通过 Executor 传入的任务首先开始执行 start 方法
- start 方法里定义了该任务的 startCh 和 stopCh 信号
- 各启动一个 Goroutine 等待任务开始和任务结束
- 同时把表示任务的 startCh 加入等待队列中表示,队列需要靠 sync.Mutex 保护
- 当一个任务结束时,解除 waitDone 方法的阻塞,启动队首的任务,解除 startTask 里的阻塞
- 所有任务结束后,解除 Wait 方法里的阻塞
- 完整代码参见上述链接
Golang(九)简单 Goroutine 池实现的更多相关文章
- golang中goroutine池的使用
1. 概念本质上是生产者.消费者模型可以有效的控制goroutine数量,防止暴涨案例:生成一个随机数,计算该随机数每一个数字相加的和,例如:123:1+2+3=6主协程负责生产数据发送到待处理通道中 ...
- Golang之chan/goroutine(转)
原文地址:http://tchen.me/posts/2014-01-27-golang-chatroom.html?utm_source=tuicool&utm_medium=referra ...
- gf框架之grpool - 高性能的goroutine池
Go语言中的goroutine虽然相对于系统线程来说比较轻量级,但是在高并发量下的goroutine频繁创建和销毁对于性能损耗以及GC来说压力也不小.充分将goroutine复用,减少goroutin ...
- 菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t
菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn ...
- 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]
菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- [Golang] 一个简易代理池
晚上写了一个代理池,就是在一个代理网站上爬取代理ip和端口以及测试是否可用.接下来可能考虑扩展成一个比较大的 golang实现的代理池. 简易版代码: package main import ( &q ...
- Linux多线程实践(9) --简单线程池的设计与实现
线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收.所以 ...
- Linux下简单线程池的实现
大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时间内必须处理数目巨大的连接请求,但是处理时间却是比较短的.在传统的多线程服务器模型中是这样实现的:一旦有个服务请求到达,就创建一个新的服务 ...
- Golang设计模式—简单工厂模式(Simple Factory Pattern)
Golang设计模式--简单工厂模式 背景 假设我们在做一款小型翻译软件,软件可以将德语.英语.日语都翻译成目标中文,并显示在前端. 思路 我们会有三个具体的语言翻译结构体,或许以后还有更多,但现在分 ...
随机推荐
- PHP--常用配置项
一.简介 PHP的配置项可以在配置文件php.ini中配置,也可以在脚本中使用ini_set()函数临时配置. 二.常用配置项 1.错误信息相关配置 1)display_errors 设定PHP是否将 ...
- YII 项目部署时, 显示空白内容
本地开发完成,想部署到服务器上,选用了GIT来在服务器上获取上传的本地项目,结果clone后,访问网址后,YII就是个空白页,啥信息也没有,无语.. 刚开始以为是权限问题,后来给访问的目录加了777, ...
- Linux目录和文件——查询目录和文件的命令
Linux目录和文件——查询目录和文件的命令 摘要:本文主要学习了在Linux系统中是如何查询目录和文件的. which命令 which命令是根据PATH环境变量设置的路径,去搜索执行文件. 基本语法 ...
- C/C++中new的使用规则
本人未重视new与指针的使用,终于,终于在前一天船翻了,而且没有爬上岸: 故此,今特来补全new的用法,及其一些规则: 话不多说 C++提供了一种“动态内存分配”机制,使得程序可以在运行期间,根据实际 ...
- 设计模式之(十)装饰模式(DECORATOR)
在购买了一个房子后,如果是毛坯房,肯定不合适直接入住的.需要对它进行装修:地面找平贴地砖.批墙贴墙纸.吊顶装订以及买需要的家具,住进去以后也可能根据需要再添加或者去掉一些家具或者修改一些东西.所以的这 ...
- 英语LIGNALOO沉香lignaloo单词
沉香lignaloo,是瑞香科.沉香属的一种乔木,高5-15米.树皮暗灰色,几平滑,纤维坚韧:小枝圆柱形,具绉纹,幼时被疏柔毛,后逐渐脱落,无毛或近无毛.产于中国广东.海南.广西.福建等地.喜生于低海 ...
- SWPUCTF 2019 web
web1 知识点 ## information_schema绕过 ##无列名注入 注入点在广告申请广告名字处,申请广告名为 查看广告详细返回错误 接下来就是常规的union注入的套路,但是发现or被过 ...
- InnoDB Multi-Versioning
InnoDB 是一个数据多版本的存储引擎,它会保持它修改的数据的旧版本数据以此来支持事务特性,比如并发操作和事务的回滚.这些旧版本数据存储在一个叫做rollback segment的数据结构中(回滚段 ...
- Django 练习班级管理系统二 -- 添加班级数据
在上一篇中(Django 练习班级管理系统一 https://www.cnblogs.com/klvchen/p/11078174.html),使用的是莫泰对话框的方式提交数据,适用于数据量少的操作. ...
- java 并发编程面试题及答案
1.在java中守护线程和本地线程区别? java中的线程分为两种:守护线程(Daemon)和用户线程(User). 任何线程都可以设置为守护线程和用户线程,通过方法Thread.setDaemon( ...