dsd30433 2016-10-25 03:49
浏览 30
已采纳

需要了解goroutines [重复]

This question already has an answer here:

I am new to Go language programming and learning it step by step.
While practicing it, I found random behavior of goroutines.
If I call goroutine (function having sleep of 1 second), some times it completed successfully and some times it doesn't:

package main

import (
    "fmt"
    "time"
)

func t(i int) {
    fmt.Println("In func t")
    time.Sleep(1)
}

func t1(i int) {
    fmt.Println("In func t1")
    time.Sleep(1)
}

func main() {
    fmt.Println("Hello Good Morning")
    go t(1)
    t1(2)
    time.Sleep(5)
    fmt.Println("End of func main")
}

O/p 1 :

  Hello Good Morning
  In func t1
  In func t
  End of func main

O/p 2 :

  Hello Good Morning
  In func t1
  End of func main

Could someone explain why goroutine is not guaranteeing the execution of that goroutine function call.

</div>
  • 写回答

2条回答 默认 最新

  • douyin7829 2016-10-25 04:16
    关注

    Program execution:

    When the function main returns, the program exits. It does not wait for other (non-main) goroutines to complete.

    1- main is goroutine too, you need to wait for other goroutines to finish, and you may use

    time.Sleep(5 * time.Second)
    

    for 5 Seconds wait, try it on The Go Playground:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func t(i int) {
        fmt.Println("In func t")
        time.Sleep(1 * time.Second)
    }
    
    func t1(i int) {
        fmt.Println("In func t1")
        time.Sleep(1 * time.Second)
    }
    
    func main() {
        fmt.Println("Hello Good Morning")
        go t(1)
        t1(2)
        time.Sleep(5 * time.Second)
        fmt.Println("End of func main")
    }
    

    output:

    Hello Good Morning
    In func t1
    In func t
    End of func main
    

    And see Docs:

    // Sleep pauses the current goroutine for at least the duration d.
    // A negative or zero duration causes Sleep to return immediately.
    func Sleep(d Duration)
    

    2- You may use sync.WaitGroup to wait for other goroutines, try it on The Go Playground:

    package main
    
    import (
        "fmt"
        "sync"
        "time"
    )
    
    var w sync.WaitGroup
    
    func say(s string) {
        for i := 0; i < 2; i++ {
            time.Sleep(100 * time.Millisecond)
            fmt.Println(s)
        }
        w.Done()
    }
    
    func main() {
        w.Add(1)
        go say("A")
    
        w.Add(1)
        say("B")
    
        w.Wait()
    }
    

    output:

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

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置