algorithm - 带有等待时间的for循环内的Golang End Goroutine

标签 algorithm go parallel-processing channel goroutine

我确实有一个goroutine,应该通过嵌套数组进行迭代。在数组的每个元素之后,for loop应该等待一段时间,然后迭代到下一个元素。

如果使用select语句将 bool 值传递给给该goroutine的chan,则goroutine应该立即停止。立即意味着停止嵌套数组的for循环,并停止等待/持续时间来处理下一个元素。

目前,我确实有以下内容:

package main

import (
    "fmt"
    "time"
)

type listEntry struct {
    text     string
    duration time.Duration
}

func timeTrack(start time.Time, name string) {
    elapsed := time.Since(start)
    fmt.Printf("%s took %s\n", name, elapsed)
}

func main() {
    defer timeTrack(time.Now(), "move")
    fmt.Println("-- START --")
    var list [2][10]*listEntry

    var sublist [10]*listEntry
    sublist[0] = &listEntry{"0.0.", time.Duration(2 * time.Second)}
    sublist[1] = &listEntry{"0.1.", time.Duration(3 * time.Second)}
    sublist[2] = &listEntry{"0.2.", time.Duration(4 * time.Second)}
    sublist[3] = &listEntry{"0.3.", time.Duration(9 * time.Second)}
    sublist[4] = &listEntry{"0.4.", time.Duration(32 * time.Second)}
    sublist[5] = &listEntry{"0.5.", time.Duration(21 * time.Second)}
    sublist[6] = &listEntry{"0.6.", time.Duration(19 * time.Second)}
    sublist[7] = &listEntry{"0.7.", time.Duration(11 * time.Second)}
    sublist[8] = &listEntry{"0.8.", time.Duration(9 * time.Second)}
    sublist[9] = &listEntry{"0.9.", time.Duration(6 * time.Second)}

    list[0] = sublist

    var sublist2 [10]*listEntry
    sublist2[0] = &listEntry{"1.0.", time.Duration(2 * time.Second)}
    sublist2[1] = &listEntry{"1.1.", time.Duration(20 * time.Second)}
    sublist2[2] = &listEntry{"1.2.", time.Duration(12 * time.Second)}
    sublist2[3] = &listEntry{"1.3.", time.Duration(9 * time.Second)}
    sublist2[4] = &listEntry{"1.4.", time.Duration(32 * time.Second)}
    sublist2[5] = &listEntry{"1.5.", time.Duration(21 * time.Second)}
    sublist2[6] = &listEntry{"1.6.", time.Duration(19 * time.Second)}
    sublist2[7] = &listEntry{"1.7.", time.Duration(11 * time.Second)}
    sublist2[8] = &listEntry{"1.8.", time.Duration(9 * time.Second)}
    sublist2[9] = &listEntry{"1.9.", time.Duration(6 * time.Second)}

    list[1] = sublist2

    finish := make(chan bool)
    go move(list, finish)
    time.Sleep(10 * time.Second)
    fmt.Println("-> FINISH CHAN")
    // finish <- true
    close(finish)
    time.Sleep(1 * time.Second)
    fmt.Println("-- FINISHED --")
}

func move(list [2][10]*listEntry, finish chan bool) {
    for index := 0; index < len(list); index++ {
        fmt.Printf("List: %d\n", index)
        for sublistIndex := 0; sublistIndex < len(list[index]); sublistIndex++ {
            fmt.Printf("Sublist: %d.%d - %v\n", index, sublistIndex, list[index][sublistIndex].duration)
            select {
            case <-finish:
                fmt.Println("move: finish 2")
                return
            case <-time.After(list[index][sublistIndex].duration):
                continue
            }
            // time.Sleep(list[index][sublistIndex].duration)
        }
    }
}

链接到Go Playground:Go Playground Example of the code

输出如下:

-- START --
Go ...
List: 0
Sublist: 0.0 - 2s
Sublist: 0.1 - 3s
Sublist: 0.2 - 4s
Sublist: 0.3 - 9s
-> FINISH CHAN
move: finish 2
-- FINISHED --
move took 11.0005366s

因此执行时间基本上与预期的时间一致。请在主线程中等待。伟大的!

但是实现看起来不是很好。伙计们,您有个好主意以更优雅的方式来组织代码吗?第一个for loop和第一个select语句是否甚至必要?

最佳答案

您可以使用Sync.waitgroup而不是time.Sleep。
更新的代码-https://play.golang.org/p/WG-RgqtxO_2

关于algorithm - 带有等待时间的for循环内的Golang End Goroutine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58542462/

相关文章:

java - 并行合并排序基准测试 - 确定找到的阈值

Cuda Hello World 示例

c# - Array.Sort in 使用非平凡的比较函数

algorithm - 在两个矩形之间绘制不重叠的圆弧

algorithm - 将扩展(80 位)转换为字符串

docker - 使用 nginx 反向代理的 Go 服务器

c++ - 在不包含在另一个范围内的范围内找到第一个元素

reflection - Golang帮助反射获取值

go - 如何在 Golang 中响应所有字段包含带有标签 omitempty 的字段?

java - 可动态调整大小的线程池