duanqing2209 2016-03-31 14:07
浏览 165
已采纳

为什么golang select语句不会随机选择案例

This is similar to go tutorial select statement, but I didn't get answer from that post. So I asked here. Thanks for answering.

In http://tour.golang.org/concurrency/5, it seems "case c <- x:" is always ready, which means this case will not block select statement.

Based on the sentence "A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.", when "case <-quit:" is also ready, the select statement should randomly choose from "case c <- x:" and "case <-quit:". But the program always go into the "case <-quit:" case.

I also changed the select block to be like below. Then in the first 10 loops, the program randomly print 1-6, but the the program quits once (the 11th output) the quit channel has value 0.

My question is that if the ready cases are randomly selected from, then why the 11th selection is alway the quit case.

select {
    case c <- 1:
        x, y = y, x+y
    case c <- 2:
        x, y = y, x+y
    case c <- 3:
        x, y = y, x+y
    case c <- 4:
        x, y = y, x+y
    case c <- 5:
        x, y = y, x+y
    case c <- 6:
        x, y = y, x+y
    case <-quit:
        fmt.Println("quit")
        return
    }
  • 写回答

1条回答 默认 最新

  • doucaigai7176 2016-03-31 14:34
    关注

    On the case statements, you're sending values to c (e.g c <- 1) which blocks until something reads off c as foo := <- c. When something writes to quit it will hit the case where <-quit is at and returning out of the select.

    From this example

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        c := make(chan int)
        quit := make(chan struct{})
    
        go func(q chan<- struct{}) {
            time.Sleep(5 * time.Second)
            q <- struct{}{}
        }(quit)
    
        go func(ch chan<- int) {
            var x int
            for range time.Tick(1 * time.Second) {
                c <- x
                x++
            }
        }(c)
    
        for {
            select {
            case foo := <-c:
                fmt.Println("Foo", foo)
            case bar := <-c:
                fmt.Println("Bar", bar)
            case <-quit:
                fmt.Println("quit")
                return
            }
        }
    }
    

    You can see the values randomly printed out on your machine as:

    $ go run foo.go
    Bar 0
    Bar 1
    Foo 2
    Bar 3
    quit
    
    $ go run foo.go
    Bar 0
    Foo 1
    Bar 2
    Bar 3
    quit
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?