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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!