dongzhoutuo0883 2015-09-30 00:40
浏览 70
已采纳

如何运行多个goroutine并以其运行的相同顺序收集结果

I have the following code which has a double-go routine structure:

package main

import(
    "fmt"
    "math/rand"
    "time"
    "strconv"
)

func main(){
    outchan := make(chan string)
    for i:=0;i<10;i++{
        go testfun(i, outchan)
    }
    for i:=0;i<10;i++{
        a := <-outchan
        fmt.Println(a)
    }
}

func testfun(i int, outchan chan<- string){
    outchan2 := make(chan int)
    time.Sleep(time.Millisecond*time.Duration(int64(rand.Intn(10))))
    for j:=0;j<10;j++ {
        go testfun2(j, outchan2)
    }
    tempStr := strconv.FormatInt(int64(i),10)+" - "
    for j:=0;j<10;j++ {
        tempStr = tempStr + strconv.FormatInt(int64(<-outchan2),10)
    }
    outchan <- tempStr
}

func testfun2(j int, outchan2 chan<- int){
    time.Sleep(time.Millisecond*time.Duration(int64(rand.Intn(10))))
    outchan2 <- j
}

The output I was expecting is

0 - 0123456789
1 - 0123456789
2 - 0123456789
3 - 0123456789
4 - 0123456789
5 - 0123456789
6 - 0123456789
7 - 0123456789
8 - 0123456789
9 - 0123456789

But instead I got this:

7 - 7980345261
6 - 4035897621
3 - 9047526831
9 - 4032861975
8 - 9570831624
5 - 3798021546
1 - 0985362471
0 - 1849276035
2 - 9572806143
4 - 5768032419

Could anyone show me how to achieve the output I was expecting? I'm a newbie and please forgive me if the solution is obvious. I've been looking for it for days.

  • 写回答

2条回答 默认 最新

  • doudao1950 2015-09-30 02:52
    关注

    To give you a better idea. The issue is that you're reading a single channel where the values that are pushed onto the channel are in an arbitrary order due to your time.Sleep calls. If you want to issue the time.Sleep calls concurrently to simulate concurrent long-running processes, what you'll want to do is make each goroutine write to a channel with the results.

    This way you can iterate across an in-order list of the results channels blocking until the next channel can be read from (the same idea as the output queue in this answer Maintaining Order in a Multi-Threaded Pipeline) Here's your reworked code with some name changes to make things easier to track:

    package main
    
    import(
        "fmt"
        "math/rand"
        "time"
        "strconv"
    )
    
    func main(){
        var jobs []chan string
        for i := 0; i<10; i++{
            job := make(chan string)
            jobs = append(jobs, job)
            go testfun(i, job)
        }
        for _, result := range jobs {
          fmt.Println(<-result)
        }
    }
    
    func testfun(i int, job chan<- string){
        var innerJobs []chan int
        time.Sleep(time.Millisecond*time.Duration(int64(rand.Intn(10))))
        for j := 0; j<10; j++ {
            innerJob := make(chan int)
            innerJobs = append(innerJobs, innerJob)
            go testfun2(j, innerJob)
        }
        tempStr := strconv.FormatInt(int64(i),10)+" - "
        for _, result := range innerJobs {
          tempStr = tempStr + strconv.FormatInt(int64(<-result),10)
        }
        job <- tempStr
    }
    
    func testfun2(j int, innerJob chan<- int){
        time.Sleep(time.Millisecond*time.Duration(int64(rand.Intn(10))))
        innerJob <- j
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动