xpool

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

* @Date: 2023-07-11 13:55:42 * @LastEditTime: 2023-07-12 14:28:40 * @Description:

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool ...

func New

func New(max int) *Pool

New ...

func (*Pool) Do

func (p *Pool) Do(f func())

Do ...

func (*Pool) Wait

func (p *Pool) Wait()

Wait ...

type Task

type Task struct {
	F    TaskFunc
	Args interface{}
}

定义任务实体,里面有方法和参数

func (*Task) Execute

func (t *Task) Execute(args ...interface{})

任务执行

type TaskFunc

type TaskFunc func(args ...interface{})

定义接口 可传任意参数

type WorkPool

type WorkPool struct {
	Pool       chan *Task      //定义任务池
	WorkCount  int             //工作线程数量,决定初始化几个goroutine
	StopCtx    context.Context //上下文
	StopCancel context.CancelFunc
	WG         sync.WaitGroup //阻塞计数器
}

定义线程池对象

func NewWrokPool

func NewWrokPool(workerCount int, len int) *WorkPool

实例化一个新线程池

func (*WorkPool) PushTask

func (w *WorkPool) PushTask(task *Task)

任务入队

func (*WorkPool) Start

func (w *WorkPool) Start() *WorkPool

启动线程池,触发任务调度

func (*WorkPool) Stop

func (w *WorkPool) Stop()

停止执行任务,回收正在执行任务的协程 协程计数器减1 直到变成0退出,否则阻塞

func (*WorkPool) Work

func (w *WorkPool) Work(wid int)

任务调度 go协程从channel里取任务执行Execute方法

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL