第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 goroutine 中等待主線程睡眠

在 goroutine 中等待主線程睡眠

Go
慕森卡 2023-06-01 18:24:00
編寫這個非?;镜拇a來理解頻道。如果一個 goroutine 中有等待,為什么 main goroutine 在等待它?我讀到主 goroutine 需要等待時間,因為在調(diào)用 goroutine 后控件會立即傳回給它。為什么goroutines不像java中的主線程和子線程那樣可以并行運行?func main() {    channel := make(chan int)    go func() {        time.Sleep(3*time.Second)    }()    for {        fmt.Println("../");        <-channel    }}
查看完整描述

1 回答

?
哆啦的時光機

TA貢獻1779條經(jīng)驗 獲得超6個贊

我認為您的主線程正在等待來自頻道的內(nèi)容


func main() {

    channel := make(chan int)

    go func() {

        time.Sleep(3*time.Second)

        channel <- 1 // Sending something to the channel to let the main thread continue

        channel <- 2

    }()


    for {

        fmt.Println("../");

        <-channel // Waiting for something to come from the channel

    }

}

關(guān)于您的具體問題:

如果一個 goroutine 中有等待,為什么 main goroutine 在等待它?

它不等待它,它可能在通道上等待。

我讀到主 goroutine 需要等待時間,因為在調(diào)用 goroutine 后控件會立即傳回給它。

如果您的 main 沒有在通道上等待(或堆棧在無限循環(huán)中),它已經(jīng)完成并關(guān)閉了應(yīng)用程序。GoRoutines 與主線程一起關(guān)閉(類似于 Java 守護線程)

為什么goroutines不像java中的主線程和子線程那樣可以并行運行?

他們實際上做(:


查看完整回答
反對 回復(fù) 2023-06-01
  • 1 回答
  • 0 關(guān)注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號