我收到致命错误:所有 goroutine 都在睡觉 - 死锁!,即使我正在使用缓冲通道并获得结果。但是在得到所有结果后,我得到了错误。
package main
import (
"fmt"
"time"
)
func main() {
StartJobExample()
}
var MAX_NUM = 1000
func StartJobExample(){
t1 := time.Now()
jobs := make(chan int,MAX_NUM)
result:=make( chan int,MAX_NUM)
go worker(jobs,result)
writeNums(jobs)
for j := range result{
fmt.Println("number read is ",j)
}
close(result)
t2 := time.Now()
fmt.Println("Time taken in operation ",t2.Sub(t1).Seconds())
}
func worker(jobs <-chan int ,result chan<- int){
for i:= range jobs{
result<-addTwo(i)
}
}
func writeNums(jobs chan<- int){
for i:=0;i<MAX_NUM;i++{
fmt.Println("adding job ",i)
jobs<-i
}
close(jobs)
}
func addTwo(i int)int{
return i+2
}
jeck猫
相关分类