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()
	}
}
上一篇:2017年计划安排


下一篇:数据湖之Hudi(6):Hudi快速体验使用(上)