第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

不生成相同的輸出并發(fā) Go 工作線程池

不生成相同的輸出并發(fā) Go 工作線程池

Go
素胚勾勒不出你 2022-10-04 19:33:16
我正在編寫一個(gè)程序,該程序從文本文件中逐字逐字讀取,以使用通道和工作線程池模式計(jì)算出現(xiàn)次數(shù)該程序在以程中工作:讀取文本文件(函數(shù))readTextreadText函數(shù)將每個(gè)單詞發(fā)送到通道word每個(gè)戈魯丁執(zhí)行對(duì)地圖中的單詞進(jìn)行計(jì)數(shù)的函數(shù)countWord每個(gè)戈魯丁返回一個(gè)映射,工作器函數(shù)將結(jié)構(gòu)的結(jié)果值傳遞給通道resultC測(cè)試函數(shù)根據(jù)來(lái)自通道的結(jié)果值創(chuàng)建映射resultC打印從步驟 5 創(chuàng)建的地圖該程序有效,但當(dāng)我嘗試放置看到過(guò)程如下所示fmt.Println(0)func computeTotal() {    i := 0    for e := range resultC {        total[e.word] += e.count        i += 1        fmt.Println(i)    }}程序終止而不顯示/計(jì)算所有單詞1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 all goroutines finished 16 17 18 map[but:1 cat's:1 crouched:1 fur:1 he:2 imperturbable:1 it:1 pointed:1 sat:1 snow:1 stiffly:1 the:1 was:2 with:1] total words: 27 38 ... 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 Time taken for reading the book 5.8145ms如果我在此處的計(jì)算總計(jì)函數(shù)語(yǔ)句中取消注釋 fmt.println(), 程序?qū)⒄_顯示結(jié)果,輸出如下所示all goroutines finishedmap[a:83 about:4 above:2 absolute:1 accepted:1 across:1 affection:1 after:1 again:5  wonder:2 wood:5 wooded:1 woody:1 work:1 worked:2 world:4 would:11 wrapped:1 wrong:1 yellow:2 yielded:1 yielding:1 counts continues ......]total words:  856Time taken for reading the book 5.9924ms這是我對(duì)閱讀文本的實(shí)現(xiàn)//ensure close words at the right timingfunc readText() {    file, err := os.Open(FILENAME)    if err != nil {        log.Fatal(err)    }    defer file.Close()    scanner := bufio.NewScanner(file)    scanner.Split(bufio.ScanWords)    for scanner.Scan() {        word := strings.ToLower(scanner.Text())        words <- strings.Trim(word, ".,:;")    }    //time.Sleep(1 * time.Second)    close(words)}這是我使用工人池的計(jì)數(shù)單詞實(shí)現(xiàn)//call countWord func,func workerPool() {    var wg sync.WaitGroup    for i := 1; i <= NUMOFWORKER; i++ {        wg.Add(1)        go worker(&wg)    }    wg.Wait()    fmt.Println("all goroutines finished")    close(resultC)}我一直在尋找為什么程序沒(méi)有顯示一致的結(jié)果,但我還無(wú)法弄清楚。如何對(duì)程序進(jìn)行更改,使其產(chǎn)生相同的結(jié)果?
查看完整描述

2 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

該函數(shù)始終返回計(jì)數(shù) == 1 的結(jié)果。countWord


下面是遞增計(jì)數(shù)的函數(shù)版本:


func countWord(word string, tempMap map[string]int) Result {

    count := tempMap[word] + 1

    tempMap[word] = count

    return Result{word, count}

}

但要抱住這個(gè)想法!該函數(shù)假定結(jié)果為 1。鑒于問(wèn)題中的工人總是按預(yù)期發(fā)送,我們可以通過(guò)直接從發(fā)送來(lái)將工人從圖片中剔除。代碼如下:computeTotalcountResult{word, 1}computeTotalResult{word, 1}readText


func computeTotal() {

    i := 0

    for e := range resultC {

        total[e.word] += e.count

        i += 1

        fmt.Println(i)

    }

}


func readText() {

    file, err := os.Open(FILENAME)

    if err != nil {

        log.Fatal(err)

    }

    defer file.Close()

    scanner := bufio.NewScanner(file)

    scanner.Split(bufio.ScanWords)


    for scanner.Scan() {

        word := strings.ToLower(scanner.Text())

        resultC <- Result{strings.Trim(word, ".,:;"), 1}

    }

    close(resultC)

}


main() {

    ...

    go readText()

    computeTotal()

    fmt.Println(total)

    ...

}

通道操作的開銷可能否定了運(yùn)行和單獨(dú)戈魯廷的任何好處。下面是組合成單個(gè)戈魯廷的代碼:computeTotalreadText


func main() {

    file, err := os.Open(FILENAME)

    if err != nil {

        log.Fatal(err)

    }

    defer file.Close()

    scanner := bufio.NewScanner(file)

    scanner.Split(bufio.ScanWords)


    var total = map[string]int{}

    for scanner.Scan() {

        word := strings.ToLower(strings.Trim(scanner.Text(), ".,:;"))

        total[word]++

    }

    fmt.Println(total)

}

問(wèn)題中的函數(shù)使我認(rèn)為您的目標(biāo)是計(jì)算每個(gè)工人的單詞并合并結(jié)果以獲得總計(jì)。這是代碼:countWord


func computeTotal() {

    for i := 1; i <= NUMOFWORKER; i++ {

        m := <-resultC

        for word, count := range m {

            total[word] += count

        }

    }

}


func workerPool() {

    for i := 1; i <= NUMOFWORKER; i++ {

        go worker()

    }

}


func worker() {

    var tempMap = make(map[string]int)

    for w := range words {

        tempMap[w]++

    }

    resultC <- tempMap

}


...

var resultC = make(chan map[string]int)

...


func main() {

    ...

    go readText()

    workerPool()

    computeTotal()

    ...

}


查看完整回答
反對(duì) 回復(fù) 2022-10-04
?
四季花海

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊

您必須通過(guò)以下方式重寫函數(shù):computeTotal


func computeTotal(done chan struct{}) {

    defer close(done)

    i := 0

    for e := range resultC {

        total[e.word] += e.count

        i += 1

        fmt.Println(i)

    }

}


func main() {


   computeTotalDone := make(chan struct{})

   go computeTotal(computeTotalDone)

   ...

   workerPool() //blocking

   <-computeTotalDone

   fmt.Println(total)

}

添加會(huì)導(dǎo)致無(wú)效結(jié)果的原因是您的實(shí)現(xiàn)具有爭(zhēng)用條件。由于在主函數(shù)和函數(shù)中打印總結(jié)果并行運(yùn)行,因此不能保證在調(diào)用之前處理所有消息。如果沒(méi)有該功能,您的計(jì)算機(jī)上的速度就足以產(chǎn)生正確的結(jié)果。fmt.Printlnfmt.Println(total)computeTotalcomputeTotalfmt.Println(total)fmt.PrintlncomputeTotal


建議的解決方案可確保在調(diào)用之前完成。computeTotalfmt.Println(total)


查看完整回答
反對(duì) 回復(fù) 2022-10-04
  • 2 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)