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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用 Channels 讓 goroutine 相互通信

如何使用 Channels 讓 goroutine 相互通信

Go
狐的傳說(shuō) 2023-08-14 14:51:10
我是 Golang 新手,正在嘗試使用 goroutine,以便它們可以在它們之間進(jìn)行通信。我有一些代碼啟動(dòng)一個(gè)具有操作 1 的 goroutine,我稱(chēng)之為跳舞。當(dāng)它完成時(shí),它會(huì)向另一個(gè) goroutine 發(fā)出信號(hào),該 goroutine 執(zhí)行另一個(gè)操作2,比如說(shuō)睡眠。您可以將強(qiáng)制舞蹈參數(shù)傳遞給舞蹈 goroutine,但如果它已經(jīng)處于舞蹈狀態(tài),它將休眠。package mainimport (    "fmt"    "time")func main(){    test("notdancing", true)    time.Sleep(10*time.Second)}func dance()error{    fmt.Println("Tapping my feet")    time.Sleep(10*time.Second)    return nil}func test(status string, forceDance bool) {當(dāng)   //startSleep := make(chan bool)為什么通道需要提供緩沖區(qū)長(zhǎng)度才能使其工作?我嘗試不設(shè)置緩沖區(qū)長(zhǎng)度,但它說(shuō)如果我不傳遞 1 作為第二個(gè)參數(shù),所有 goroutine 都會(huì)休眠。    startdance := make(chan bool, 1)    startSleep := make(chan bool, 1)    if status == "dancing" && forceDance {        select {        case startSleep <-true:            fmt.Println("Would start to sleep now")        default:            fmt.Println("Sleep Already started. No need to force")        }    }    if status != "dancing" {        fmt.Println("Startingdance")        startdance <- true    }    go func() {        <-startdance        err := dance()        if err == nil {            select {            case startSleep <- true:                fmt.Println("Starting Sleeping, dancing completed")            default:                fmt.Println("Already started Sleeping")            }        } else {            fmt.Println("Not in a mood to dance today")        }    }()    go func() {        <-startSleep        if forceDance {            fmt.Println("Force sleep because forcing to dance while already dancing")        }    }()}我非常感謝對(duì)代碼的任何更正以及使用這種方法的陷阱。
查看完整描述

1 回答

?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

在無(wú)緩沖通道的情況下(未指定大小時(shí)),它無(wú)法保存值,因?yàn)樗鼪](méi)有大小。因此,通過(guò)通道寫(xiě)入/傳輸數(shù)據(jù)時(shí)必須有讀取器在場(chǎng),否則將阻塞調(diào)用。


func main() {

? ? startDance := make(chan bool)

? ? startDance <- true

}

但是,當(dāng)您在上面的代碼中指定大?。ɡ?1)時(shí),就不會(huì)出現(xiàn)死鎖,因?yàn)樗鼘⒂锌臻g來(lái)保存數(shù)據(jù)。((https://robertbasic.com/blog/buffered-vs-unbuffered-channels-in-golang/)。)(https://www.golang-book.com/books/intro/10)你可以看看上述網(wǎng)站是為了更好地了解通道和并發(fā)性

package main


import (

? ? "fmt"

? ? "time"

)


func main() {

? ? startDance := make(chan bool)

? ? startSleep := make(chan bool)

? ? forceSleep := make(chan bool)

? ? go startDance1(startDance, forceSleep, startSleep)

? ? go performSleep(startSleep, startDance)

? ? startDance <- true

? ? fmt.Println("now dance is started ")

? ? forceSleep <- true

? ? select {}

}


func startDance1(startDance chan bool, forceSleep chan bool, startSleep chan bool) {


? ? fmt.Println("waiting to start dance")

? ? select {

? ? case <-startDance:

? ? ? ? fmt.Println("staring dance")

? ? }


? ? for {

? ? ? ? select {

? ? ? ? case <-startDance:

? ? ? ? ? ? fmt.Println("starting dance")

? ? ? ? case <-forceSleep:

? ? ? ? ? ? fmt.Println("aleardy dancing going to sleep")

? ? ? ? ? ? select {

? ? ? ? ? ? case startSleep <- true:


? ? ? ? ? ? default:

? ? ? ? ? ? }

? ? ? ? default:

? ? ? ? ? ? //this is just to show working this

? ? ? ? ? ? // i added default or else this will go into deadlock

? ? ? ? ? ? fmt.Println("dancing")

? ? ? ? ? ? time.Sleep(time.Second * 1)

? ? ? ? }

? ? }

}


func performSleep(startSleep chan bool, startDance chan bool) {

? ? select {

? ? case <-startSleep:

? ? ? ? fmt.Println("staring sleep")

? ? }

? ? fmt.Println("sleeping for 5 seconds ")

? ? time.Sleep(time.Second * 5)

? ? select {

? ? case startDance <- true:

? ? ? ? fmt.Println("started dance")

? ? default:

? ? ? ? fmt.Println("failed to start dance ")

? ? }

}

上面的代碼是對(duì)你的代碼的一個(gè)小小的改進(jìn)(我試圖根據(jù)你的要求來(lái)制作它)。

查看完整回答
反對(duì) 回復(fù) 2023-08-14
  • 1 回答
  • 0 關(guān)注
  • 146 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號(hào)

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