dongyao9762 2015-09-20 04:49
浏览 9
已采纳

goroutine放到频道顺序固定吗?

package main

import "fmt"

func sum(a []int, c chan int) {
    sum := 0
    for _, v := range a {
        sum += v
    }
    c <- sum // send sum to c
}

func main() {
    a := []int{7, 2, 8, -9, 4, 0}

    c := make(chan int)
    go sum(a[len(a)/2:], c)
    go sum(a[:len(a)/2], c)

    x, y := <-c, <-c // receive from c

    fmt.Println(x, y, x+y)
}

x, y := <-c, <-c // receive from c

Why does this line always print the same result?

I think it should be 50/50 chance to print 17 -5 12 or -5 17 12

I think the two go routines should be parallel

Thanks in advance!

package main

import "fmt"
import "time"
import "math/rand"

func sum(a []int, c chan int) {
    sum := 0
    for _, v := range a {
        sum += v
    }
    time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
    c <- sum // send sum to c

}

func main() {
    a := []int{7, 2, 8, -9, 4, 0}

    c := make(chan int)
    go sum(a[len(a)/2:], c)
    go sum(a[:len(a)/2], c)

    x, y := <-c, <-c // receive from c

    fmt.Println(x, y, x+y)
}

I make the thread to sleep, but same thing happens. I am still confusing.

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-09-20 06:53
    关注

    The assumption that it should be a 50/50 chance is incorrect in this situation. To be clear, I'm not suggesting it should always be one or the other but rather that you shouldn't expect it to change each time the program runs. The behavior is not necessarily random (or even pseudo-random).

    A goroutine's guarantee is not that it will be scheduled at a random future time, but rather just that it will run at some time in the future. If the current implementation of the scheduler decided to put goroutines in a simple queue, it wouldn't automatically mean it's broken. For your particular code's case, if you stick a few time.Sleeps or fmt.Printfs in different places, you'd likely see the order change around sometimes.

    I wonder if you're confusing Go's scheduler's selection of the next goroutine with the documented pseudo-random behavior of select when used with channels. The behavior there is defined to be random and it's correct to say the behavior should have a 50/50 chance.

    As far as I've read, the choice of which goroutine the runtime will select is not random. That's not the same as saying it can't be random, but moreso that it's not supposed to be random.

    The important thing is that your code shouldn't care about the order in which goroutines are scheduled. Whether it's always in order like a queue, or backwards (a stack), or random, or something else (likely the reality).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c