```
package main
import (
"fmt"
"runtime"
)
func task(q int,c chan int) {
// 模拟耗时操作
fmt.Println("starting heavy_task")
for i := 0;i < q;i++{
c <- i
time.Sleep(800 * time.Millisecond)
}
fmt.Println("Done heavy_task")
}
func main() {
// 利用chan来同步代码
var cores = runtime.NumCPU()
res := make([]int,cores)
c := make(chan int,cores)
go task(cores,c)
for i := 0; i < cores; i++ {
fmt.Println("waiting data...")
res[i] = <-c
fmt.Println("received data->",res[i])
}
fmt.Println("res->",res)
}
```
输出:
![image.png](https://static.studygolang.com/190228/bc0ae2bf6442e4eab2cb1c2634618055.png)
请问heavy_task函数中最后一句“Done heavy_task”为啥没有打印出来呢?
有疑问加站长微信联系(非本文作者)