doureng6738 2016-11-16 00:29
浏览 3
已采纳

正确执行我的goroutines

Background: I am trying to implement a logic which find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20. I have implemented a sequential version and got an answer as 232792560.

Question: When I try to build some concurrency on this problem ( see uncommented block of code), it does run but never shows any result. Can anyone of you guide me on where I am going wrong?

Note: I am very much new to golang; and I am aware that, this is not the best problem for concurrency since there is no guarantee that I will have the smallest positive number as first result. However, I tried it out of my curiosity.

    package main

    import(
        "fmt"
    )

    func divide(num int) bool {
        for i := 1; i <= 20; i++ {
            if num % i != 0 {
                return false
            }
        }
        return true
    }

    func main() {
        num:=0
        //simple function
        /*for {
            num++;
            result := divide(num)
                if result {
                    fmt.Println("Smallest number is: ", num)
                    break
                }
            }*/


        //go-routine
        //go-routine
    var wg sync.WaitGroup
    for {
        num++;
        wg.Add(1)
        go func(x int) {
            result := divide(x)
            if result {
                fmt.Println("Smallest number is: ", x)
                defer wg.Done()
            }

        }(num)

    }
    wg.Wait()
  fmt.Println("End.")

 }
  • 写回答

2条回答 默认 最新

  • dongxiaoguang9108 2016-11-16 02:49
    关注

    It doesn't make sense to launch unlimited number of goroutines - it will be performing worse, than non-concurrent solution. You can try to use a "worker pool" pattern and batch the calculations.

    package main
    
    import (
        "fmt"
        "runtime"
    )
    
    func divide(num int) bool {
        for i := 1; i <= 20; i++ {
            if num%i != 0 {
                return false
            }
        }
        return true
    }
    
    const step = 100000
    
    func main() {
        result := make(chan int)
        jobs := make(chan int)
        for w := 0; w < runtime.NumCPU(); w++ {
            go func() {
                for n := range jobs {
                    for i := n; i < n+step; i++ {
                        if divide(i) {
                            result <- i
                        }
                    }
                }
            }()
        }
        num := 1
        for {
            select {
            case jobs <- num:
                num += step
            case x := <-result:
                fmt.Println("Smallest number is:", x)
                return
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题