如何修改下述代码使其不会报deadlock(使用管道)

qiqi3570 · · 627 次点击
```go package main import ( "fmt" ) func InitNum(ch chan int) { for i := 1; i <= 2000; i++ { ch <- i } close(ch) } func readNum(ch chan int, reschan chan map[int]int, remap chan bool) { for { temp, ok := <-ch if !ok { break } else { res := 0 remp1 := make(map[int]int) for i := 1; i <= temp; i++ { res = res + i } remp1[temp] = res reschan <- remp1 } } remap <-true } func main() { ch := make(chan int, 2000) reschan := make(chan map[int]int, 2000) remap := make(chan bool, 8) for i := 0; i < 8; i++ { go readNum(ch, reschan, remap) } go InitNum(ch) go func(){ for i := 0; i < 8; i++ { <-remap } close(reschan) } for{ temp,ok:=<-reschan if !ok{ break } fmt.Println(temp) } } ```
#1