package main
import(
"fmt"
)
func writeNum(numChan chan int){
for i := 1; i <= 2000; i++ {
numChan<- i
}
close(numChan)
}
func readNum(numChan chan int,resChan chan int,exitChan chan bool) {
res := 0
for{
//n,ok := <-numChan
n := <-numChan
if len(numChan) == 0{
break
}
fmt.Printf("读取到的数据=%v\n", n)
for i := 1; i <= n; i++ {
res += i
}
resChan<- res
}
fmt.Println("协程工作完成!!!")
exitChan<- true
close(resChan)
}
func main() {
resChan := make(chan int,2000)
numChan := make(chan int,2000)
exitChan := make(chan bool,8)
go writeNum(numChan)
for i := 1; i <= 8; i++ {
go readNum(numChan,resChan,exitChan)
}
for{
if len(exitChan) == 8{
fmt.Println("主线程即将结束")
close(exitChan)
break
}
}
i := 1
for v := range resChan {
fmt.Printf("res[%v] = %v\n",i,v)
i++
}
}