douguai6716 2016-10-06 03:01
浏览 18
已采纳

Golang-为什么使用完成频道

It's one of example code of Golang. But cannot understand why 'done' channel need in this case.

https://gobyexample.com/closing-channels

There is no reason to sent true to done channel. We can know jobs channel is done when "sent all jobs" message is printed, isn't it?

I deleted code that relative to done channel and result is still same.

https://play.golang.org/p/xOmzobFpTQ

  • 写回答

4条回答 默认 最新

  • dongtang1918 2016-10-06 03:56
    关注

    No the result is not the same:
    Your main goroutine exits before your received job goroutine in many situations (e.g. different CPU loads, and it is nondeterministic and system dependent behavior), so in that way you cannot guarantee that all jobs received, e.g. just Add

    time.Sleep(500)
    

    before

    fmt.Println("received job", j)
    

    to see this, try it on The Go Playground:

    // _Closing_ a channel indicates that no more values
    // will be sent on it. This can be useful to communicate
    // completion to the channel's receivers.
    
    package main
    
    import (
        "fmt"
        "time"
    )
    
    // In this example we'll use a `jobs` channel to
    // communicate work to be done from the `main()` goroutine
    // to a worker goroutine. When we have no more jobs for
    // the worker we'll `close` the `jobs` channel.
    func main() {
        jobs := make(chan int, 5)
        //done := make(chan bool)
    
        // Here's the worker goroutine. It repeatedly receives
        // from `jobs` with `j, more := <-jobs`. In this
        // special 2-value form of receive, the `more` value
        // will be `false` if `jobs` has been `close`d and all
        // values in the channel have already been received.
        // We use this to notify on `done` when we've worked
        // all our jobs.
        go func() {
            for {
                j, more := <-jobs
                if more {
                    time.Sleep(500)
                    fmt.Println("received job", j)
                } else {
                    fmt.Println("received all jobs")
                    //done <- true
                    return
                }
            }
        }()
    
        // This sends 3 jobs to the worker over the `jobs`
        // channel, then closes it.
        for j := 1; j <= 3; j++ {
            jobs <- j
            fmt.Println("sent job", j)
        }
        close(jobs)
        fmt.Println("sent all jobs")
    
        // We await the worker using the
        // [synchronization](channel-synchronization) approach
        // we saw earlier.
        //<-done
    }
    

    output:

    sent job 1
    sent job 2
    sent job 3
    sent all jobs
    

    instead of:

    sent job 1
    received job 1
    received job 2
    sent job 2
    sent job 3
    received job 3
    received all jobs
    sent all jobs
    

    See:
    Goroutine does not execute if time.Sleep included
    Why is time.sleep required to run certain goroutines?
    Weird channel behavior in go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值