package main
import (
"fmt"
"strconv"
"time"
)
type Task struct {
Name string
Cnt int
}
var taskChan = make(chan Task,1000)
var retChan = make(chan Task,1000)
func main() {
go startRoutines(10000)
go handlResult()
go produce()
time.Sleep(1*time.Minute)
}
func startRoutines(n int) {
for i:=0;i<n;i++{
go doSomeThing()
}
}
func handlResult() {
for {
select {
case r := <- retChan:
fmt.Println(r)
}
}
}
func doSomeThing() {
for {
select{
case t := <-taskChan:
for i:=0;i<1000000;i++{
t.Cnt++
}
retChan <- t
}
}
}
func produce() {
for i:=0;i<100000;i++{
t := Task{
Name: "task_"+strconv.Itoa(i),
Cnt: i,
}
taskChan <- t
}
}
有疑问加站长微信联系(非本文作者)