/coroutine-tool

利用GO语言的原生协程并发处理任务

Primary LanguageGoMIT LicenseMIT

Go Reference MIT


coroutine-tool

利用GO语言的原生协程并发处理任务

安装

 go get github.com/adam-qiang/coroutine-tool@latest

Demo

package main

import (
	"coroutine-github.com/adam-qiang/coroutine-tool/pool"
	"coroutine-github.com/adam-qiang/coroutine-tool/task"
	"time"
)

func main() {
	//创建一个任务
	task1 := task.CreateTask(func() error {
		time.Sleep(time.Second * 1)
		return nil
	})
	task2 := task.CreateTask(func() error {
		time.Sleep(time.Second * 10)
		return nil
	})

	task3 := task.CreateTask(func() error {
		return nil
	})

	//创建一个协程池
	createPool := pool.CreatePool(4)

	//向协程池中放入任务
	go func() {
		for i := 0; i < 50; i++ {
			createPool.EntryChannel <- task1
			createPool.EntryChannel <- task2
			createPool.EntryChannel <- task3
		}

	}()

	//执行线程池
	createPool.Run(3*50, true, "")
}