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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料