select 我的理解是看下面的哪个channel有数据进来了,谁先有数据就先调度谁
不知道我理解的对不。
但是这样就有个疑问了,老师在讲select的时候
select {
case n := <-c1:
values = append(values, n)
case n := <-c2:
values = append(values, n)
case activeWorker <- activeValue:
values = values[1:]
case <-time.After(800 * time.Millisecond):
fmt.Println("timeout")
case <-tick:
fmt.Println(
"queue len =", len(values))
case <-tm:
fmt.Println("bye")
return
}里面的time.After,tick,tm(timeout). 理论上是不是并不能保证实时执行?最终结果得看是否被调度到。
如果其他的channel的入通道的速度快于计时,那不就是并不能保证准时进行调度timeout或者其他计时channel了吗?
那么如何保证呢?
我想到的是把获取channel和计时相关的分开,用两个select进行调用,这样能保证计时准确实时生效了吧:
for {
select {
case n := <-c1:
values = append(values, n)
case n := <-c2:
values = append(values, n)
case activeWorker <- activeValue:
values = values[1:]
}
select {
case <-time.After(800 * time.Millisecond):
fmt.Println("timeout")
case <-tick:
fmt.Println(
"queue len =", len(values))
case <-tm:
fmt.Println("bye")
return
}
}请问这样理解对吗?