簡體   English   中英

Golang在for循環中結束goroutine,等待時間

[英]Golang end goroutine inside for loop with wait time

我確實有一個 goroutine 應該遍歷一個嵌套數組。 在數組的每個元素之后, for loop應該等待一段時間,然后迭代到下一個元素。

如果使用 select 語句將 boolean 值傳遞給給該 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 代碼示例

輸出如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM