亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何使用 Channels 使 goroutines 相互通信

如何使用 Channels 使 goroutines 相互通信

Go
胡子哥哥 2022-04-26 10:33:32
我是 Golang 的新手,我正在嘗試使用 goroutines 以便他們可以在它們之間進行對話。我有一些代碼可以啟動一個具有 operation1 的 goroutine,我稱它為跳舞。當它完成時,它會向另一個執行另一個操作 2 的 goroutine 發出信號,比如說睡眠。您可以將強制舞蹈參數傳遞給舞蹈 goroutine,但如果它已經處于舞蹈狀態,它將休眠。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) {這不起作用時   //startSleep := make(chan bool)為什么需要為通道提供緩沖區長度才能使其工作?我嘗試不使用緩沖區長度,但如果我不將 1 作為第二個參數傳遞,它會說所有 goroutine 都處于睡眠狀態。    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")        }    }()}我非常感謝對代碼的任何更正以及使用這種方法的缺陷。
查看完整描述

1 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

在無緩沖通道的情況下(未指定大小時)它不能保存一個值,因為它沒有大小。因此,在通過通道寫入/傳輸數據時必須有讀取器在場,否則它將阻塞調用。


func main() {

    startDance := make(chan bool)

    startDance <- true

}

但是當您在上面的代碼中指定一個大?。ū热?1)時,它不會是死鎖,因為它將有空間來保存數據。(((https://robertbasic.com/blog/buffered-vs-unbuffered-channels-in-golang/)。)(https://www.golang-book.com/books/intro/10)你可以看看上面的網站是為了更好地了解通道和并發


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 ")

    }

}

上面的代碼是對你的一個小的改進(我試圖根據你的要求來做)。我建議您閱讀一些書籍以了解有關 Go 并發性的更多信息(https://www.golang-book.com/books/intro/10_


查看完整回答
反對 回復 2022-04-26
  • 1 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號