fatal error: all goroutines are asleep - deadlock!
func worker(id int, jobCh <-chan int, sendCh chan<- int) {
for j := range jobCh {
fmt.Printf("worker-%d, start job-%d\n", id, j)
time.Sleep(time.Second * time.Duration(3*j))
fmt.Printf("worker-%d, end job-%d\n", id, j)
sendCh <- j * 2
}
}
func main() {
jobCh := make(chan int, 100)
sendCh := make(chan int, 100)
for w := 1; w <= 3; w++ {
go worker(w, jobCh, sendCh)
}
for j := 1; j <= 3; j++ {
jobCh <- j
}
// sendCh <- 1
//================1====================
// for s := range sendCh {
// fmt.Printf("get %d\n", s)
// }
//================2====================
for a := 1; a <= 4; a++ {
t1 := time.Now().Second()
fmt.Printf("get time %d\n", t1)
s, ok := <-sendCh
if !ok {
break
}
t2 := time.Now().Second()
fmt.Printf("get time %d, %d\n", t2, t1-t2)
fmt.Printf("get %d\n", s)
}
sendCh <- 4 * 2
//================3====================
// for {
// s, ok := <-sendCh
// if !ok {
// fmt.Print(s)
// break
// }
// fmt.Printf("get %d\n", s)
// }
//================4====================
// time.Sleep(time.Second * 5)
// for {
// breakFlag := 0
// select {
// case s := <-sendCh:
// fmt.Printf("get %d\n", s)
// default:
// fmt.Println("channel is empty !")
// breakFlag = 1
// }
// if breakFlag == 1 {
// break
// }
// }
close(jobCh)
close(sendCh)
}