coroutine-tool

module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: MIT

README

Go Reference MIT


coroutine-tool

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

安装

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

Demo

package main

import (
	"gitee.com/adam-qiang/coroutine-tool/pool"
	"gitee.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, "")
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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