想要达到效果,一往stop管道中发送信号,则协程立马结束,但是目前结果一直在for循环中执行(一直占用着cpu),协程并没有马上结束,是否可以达到我想要的效果
package main
import (
"fmt"
"time"
)
func main() {
stop := make(chan bool)
fmt.Println("start:",time.Now())
go func() {
for {
select {
case <-stop:
fmt.Println("shoudao")
return
default:
//fmt.Println("jiankong")
for i:=0;i<7;i++ {
fmt.Println("jiankong:",time.Now())
time.Sleep(3 * time.Second)
}
}
}
}()
fmt.Println("start1:",time.Now())
time.Sleep(10 * time.Second)
fmt.Println("tongzhijiankong:",time.Now())
stop <- true
fmt.Println("tongzhijiankong end:",time.Now())
time.Sleep(5 * time.Second)
fmt.Println("end:",time.Now())
}
有疑问加站长微信联系(非本文作者)