Help us shape the future of Go! Take the 2026 Go Developer Survey

pool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2017 License: MIT Imports: 1 Imported by: 2

README

简单Go线程池实现

线程池目前实现:

  • Array blocking queue有界队列, 超过队列容量时新工作入队直接失败
  • Linked blocking queue无界队列, 新工作入队时一直等待直到入队成功
  • 同步阻塞队列与优先级队列待实现

使用方式见pool_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	Run  Task
	Args []interface{}
}

工作

type Queue

type Queue interface {
	// contains filtered or unexported methods
}

队列接口

type QueueType

type QueueType int

队列类型

const (
	SynchronousQueue      QueueType = iota // 同步阻塞队列
	ArrayBlockingQueue                     // 有界队列, 超过队列容量时新工作入队直接失败
	LinkedBlockingQueue                    // 无界队列, 新工作入队时一直等待直到入队成功
	PriorityBlockingQueue                  // 优先级队列
)

type Scheduler

type Scheduler interface {
	// 添加工作到线程池队列中
	// 如果队列已满,则返回失败
	Add(job Job) bool

	// 添加任务到线程池队列中
	// 当队列容量已满时:
	// 有界队列, 超过队列容量时新工作入队直接失败
	// 无界队列, 新工作入队时一直等待直到入队成功
	Join(t Task, args ...interface{}) bool
	// contains filtered or unexported methods
}

调度器接口

func Default

func Default() (s Scheduler)

创建默认的线程池队列调度器 队列长度: 1000 并发数量: 10

func New

func New(qType QueueType, queueCap, workerNum int) (s Scheduler, err error)

创建新的线程池队列调度器 qType: 线程池队列类型 queueCap: 队列最大容量 workerNum: 并发工作数量

func NewBlocking

func NewBlocking(workerNum int) (s Scheduler, err error)

创建新的线程池队列调度器 workerNum: 并发工作数量

type Task

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

任务

Jump to

Keyboard shortcuts

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