猿问

Golang - 如何将多个渠道链接在一起?

尝试安排项目,将项目发送到“状态”通道以获取状态,然后将项目发送到“删除”通道以删除项目。我正在设置两个通道的大小,预计在项目删除完成后我会“完成”。在“完成”之前执行“删除”之后,代码似乎正在停止。为什么我会收到“致命错误:所有 goroutines 都睡着了 - 死锁!” 在“删除项目”之后和“完成”之前?哪个例程正在睡觉?

https://go.dev/play/p/W49niF5xngQ

package main

import (

    "fmt"

    "time"

)


func main() {

    numbers := []int{1, 2, 4}

    for _, n := range numbers {

        fmt.Printf("Schedule and delete %d items.\n", n)

        statusChan := make(chan string, n)

        deleteChan := make(chan string, n)

        done := make(chan bool)


        go func(n int, statusChan chan<- string) {

            for i := 0; i < n; i++ {

                i := i


                go func(n int, statusChan chan<- string) {

                    fmt.Printf("Scheduling item number ... %d\n", i)

                    itemNum := fmt.Sprintf("item_num_%d\n", i)

                    time.Sleep(500 * time.Millisecond)

                    statusChan <- itemNum

                }(n, statusChan)

            }

        }(n, statusChan)


        go func(statusChan <-chan string, deleteChan chan<- string) {

            for itemNum := range statusChan {

                fmt.Printf("Checking status of item number ... %s\n", itemNum)

                time.Sleep(500 * time.Millisecond)

                deleteChan <- itemNum

            }

        }(statusChan, deleteChan)


        go func(deleteChan <-chan string, done chan<- bool) {

            for itemNum := range deleteChan {

                fmt.Printf("Delete item: %s", itemNum)

                time.Sleep(500 * time.Millisecond)

            }

            fmt.Printf("Done with scheduling and deleting %d item.\n", n)

            done <- true

        }(deleteChan, done)

        <-done

    }

}


绝地无双
浏览 104回答 1
1回答

达令说

这似乎符合您的想法。我使用“***”作为“全部完成”的信号。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; numbers := []int{1, 2, 4}&nbsp; &nbsp; for _, n := range numbers {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Schedule and delete %d items.\n", n)&nbsp; &nbsp; &nbsp; &nbsp; statusChan := make(chan string, n)&nbsp; &nbsp; &nbsp; &nbsp; deleteChan := make(chan string, n)&nbsp; &nbsp; &nbsp; &nbsp; done := make(chan bool)&nbsp; &nbsp; &nbsp; &nbsp; go func(n int, statusChan chan<- string) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Scheduling item number ... %d\n", i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemNum := fmt.Sprintf("item_num_%d\n", i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(500 * time.Millisecond)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statusChan <- itemNum&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statusChan <- "***"&nbsp; &nbsp; &nbsp; &nbsp; }(n, statusChan)&nbsp; &nbsp; &nbsp; &nbsp; go func(statusChan <-chan string, deleteChan chan<- string) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for itemNum := range statusChan {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Checking status of item number ... %s\n", itemNum)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deleteChan <- itemNum&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }(statusChan, deleteChan)&nbsp; &nbsp; &nbsp; &nbsp; go func(deleteChan <-chan string, done chan<- bool) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for itemNum := range deleteChan {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if itemNum == "***" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Delete item: %s", itemNum)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Done with scheduling and deleting %d item.\n", n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done <- true&nbsp; &nbsp; &nbsp; &nbsp; }(deleteChan, done)&nbsp; &nbsp; &nbsp; &nbsp; <-done&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Go
我要回答