golang多线程
Posted 奔跑的兔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
}
}
以上是关于golang多线程的主要内容,如果未能解决你的问题,请参考以下文章