work

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

利用无缓冲chan创建goroutine池来控制一组task的执行 1. work 包的目的是展示如何使用无缓冲的通道来创建一个 goroutine 池,这些 goroutine 执行 并控制一组工作,让其并发执行。在这种情况下,使用无缓冲的通道要比随意指定一个缓冲区大 小的有缓冲的通道好,因为这个情况下既不需要一个工作队列,也不需要一组 goroutine 配合执行。 无缓冲的通道保证两个 goroutine 之间的数据交换。

2. 这种使用无缓冲的通道的方法允许使用者知道什么时候 goroutine 池正在执行工作, 而且如果池里的所有goroutine 都忙,无法接受新的工作的时候,也能及时通过通道来通知调用者。 使用无缓冲的通道不会有工作在队列里丢失或者卡住,所有工作都会被处理。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Println(msg ...interface{})
}

Logger log interface

var LogEntry Logger = log.New(os.Stderr, "", log.LstdFlags)

type Pool

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

Pool提供一个goroutine池,可以完成任何已提交的worker任务

func New

func New(gNum int) *Pool

New 创建一个工作池

func (*Pool) Add

func (p *Pool) Add(w Worker)

Add 生产者:采用无缓冲通道提交任务到工作池 当任务提交后,消费者就会立即执行任务,p.wg计数器数量减去1 w是一个接口值,必须是具体实现类型的一个实例指针

func (*Pool) Shutdown

func (p *Pool) Shutdown()

Shutdown 等待所有的goroutine执行完毕,它关闭了 work 通道 这会导致所有池里的 goroutine 停止工作 调用pg.wg 的 Wait 方法,会等待所有 goroutine 终止

type Worker

type Worker interface {
	Task()
}

Worker worker必须满足Task方法

Jump to

Keyboard shortcuts

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