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

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

如何使用 Channels 使 goroutines 相互通信

如何使用 Channels 使 goroutines 相互通信

Go
胡子哥哥 2022-04-26 10:33:32
我是 Golang 的新手,我正在嘗試使用 goroutines 以便他們可以在它們之間進(jìn)行對(duì)話。我有一些代碼可以啟動(dòng)一個(gè)具有 operation1 的 goroutine,我稱它為跳舞。當(dāng)它完成時(shí),它會(huì)向另一個(gè)執(zhí)行另一個(gè)操作 2 的 goroutine 發(fā)出信號(hào),比如說(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) {這不起作用時(shí)   //startSleep := make(chan bool)為什么需要為通道提供緩沖區(qū)長(zhǎng)度才能使其工作?我嘗試不使用緩沖區(qū)長(zhǎng)度,但如果我不將 1 作為第二個(gè)參數(shù)傳遞,它會(huì)說(shuō)所有 goroutine 都處于睡眠狀態(tài)。    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 回答

?
絕地?zé)o雙

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

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


func main() {

    startDance := make(chan bool)

    startDance <- true

}

但是當(dāng)您在上面的代碼中指定一個(gè)大小(比如 1)時(shí),它不會(huì)是死鎖,因?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)做)。我建議您閱讀一些書籍以了解有關(guān) Go 并發(fā)性的更多信息(https://www.golang-book.com/books/intro/10_


查看完整回答
反對(duì) 回復(fù) 2022-04-26
  • 1 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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