其他分享
首页 > 其他分享> > golang多线程

golang多线程

作者:互联网

gopkg.in/go-playground/pool.v3

package main

import (
	"fmt"
	"gopkg.in/go-playground/pool.v3"
	"time"
)


func newThreadFunc(p pool.Pool, value int) pool.WorkUnit {
	var fn pool.WorkFunc
	fn = func(pool.WorkUnit) (interface{}, error){
		time.Sleep(time.Second * 10)
		fmt.Println("Hello, this is %d.", value)
		return true, nil
	}
	return p.Queue(fn)
}

func main() {
	p := pool.NewLimited(5)
	defer p.Close()

	works := make([]pool.WorkUnit, 0)

	for i := 0; i < 20; i++ {
		works = append(works, newThreadFunc(p, i))
	}
	fmt.Println("Start!")
	for _, item := range works {
		item.Wait()
	}
}

标签:works,fmt,WorkUnit,golang,fn,func,多线程,pool
来源: https://www.cnblogs.com/wxx999/p/14828699.html