问题描述
编辑:我的问题与它有一个不同的代码变体,因为其他原因不起作用,我需要解释原因。
我试图解决这里的作业问题:(使用 time.After 编写您自己的睡眠函数>
)。这是我迄今为止基于该章讨论的例子的尝试:
$ b $ myc (duration int){
for {
select {
case< -time.After(time.Second * time.Duration(duration)):
fmt.Println(睡觉!)
默认值:
fmt.Println(等待)
}
}
}
func main(){
go myOwnSleep(3)
var input string
fmt.Scanln(& input)
}
我的思考过程是,的无限将继续执行 select 语句的 default ,直到 time.After 功能的返回通道谈话。当前代码存在问题,后者不会发生,而默认的语句被无限调用。
我在做错了什么?
在 for 循环的每次迭代中执行选择语句,其中包括评估通道操作数。
在每次迭代时间.After()将被调用,并且将创建一个新频道!
如果持续时间超过 0 ,这个频道没有准备好接收,所以默认情况下会被执行。这个通道不会被再次测试/检查,下一次迭代会创建一个新的通道,该通道不会准备好接收,所以再次选择默认情况 - 像往常一样。
这个解决方案非常简单,但可以在),其他goroutines(包括将会(或将要)在频道上发送该值的那个goroutine)可能会被阻止并且不会被执行。睡眠(例如 time.Sleep(time.Millisecond))可能会释放CPU内核无法完成的工作(并允许其他goroutine运行)。
EDIT: My question is different from How to write my own Sleep function using just time.After? It has a different variant of the code that's not working for a separate reason and I needed explanation as to why.
I'm trying to solve the homework problem here: https://www.golang-book.com/books/intro/10 (Write your own Sleep function using time.After).
Here's my attempt so far based on the examples discussed in that chapter:
package main import ( "fmt" "time" ) func myOwnSleep(duration int) { for { select { case <-time.After(time.Second * time.Duration(duration)): fmt.Println("slept!") default: fmt.Println("Waiting") } } } func main() { go myOwnSleep(3) var input string fmt.Scanln(&input) }
http://play.golang.org/p/fb3i9KY3DD
My thought process is that the infinite for will keep executing the select statement's default until the time.After function's returned channel talks. Problem with the current code being, the latter does not happen, while the default statement is called infinitely.
What am I doing wrong?
In each iteration of your for loop the select statement is executed which involves evaluating the channel operands.
In each iteration time.After() will be called and a new channel will be created!
And if duration is more than 0, this channel is not ready to receive from, so the default case will be executed. This channel will not be tested/checked again, the next iteration creates a new channel which will again not be ready to receive from, so the default case is chosen again - as always.
The solution is really simple though as can be seen in this answer:
func Sleep(sec int) { <-time.After(time.Second* time.Duration(sec)) }
Fixing your variant:
If you want to make your variant work, you have to create one channel only (using time.After()), store the returned channel value, and always check this channel. And if the channel "kicks in" (a value is received from it), you must return from your function because more values will not be received from it and so your loop will remain endless!
func myOwnSleep(duration int) { ch := time.After(time.Second * time.Duration(duration)) for { select { case <-ch: fmt.Println("slept!") return // MUST RETURN, else endless loop! default: fmt.Println("Waiting") } } }
Note that though until a value is received from the channel, this function will not "rest" and just execute code relentlessly - loading one CPU core. This might even give you trouble if only 1 CPU core is available (runtime.GOMAXPROCS()), other goroutines (including the one that will (or would) send the value on the channel) might get blocked and never executed. A sleep (e.g. time.Sleep(time.Millisecond)) could release the CPU core from doing endless work (and allow other goroutines to run).
这篇关于基于时间写睡眠功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!