<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # **優雅地結束goroutines** 本節內容將介紹如何使用Go標準庫中的`sync`包來解決上一節提到的`goroutine`中的任務還未執行完成,`main()`函數就提前結束的問題。 本節的代碼文件為syncGo.go,我們基于上一節的`create.go`來擴展`syncGo.go`。 `syncGo.go`的第一部分代碼如下: ```go package main import ( "flag" "fmt" "sync" ) ``` 如上所示,我們不再需要time包,我們將使用sync包中的功能來等待所有的goroutine執行完成。 在第10章“并發 - 高級主題”中,我們將會學習兩種方式來對goroutine進行超時處理。 第二部分代碼如下: ```go func main() { n := flag.Int("n", 20, "Number of goroutines") flag.Parse() count := *n fmt.Printf("Going to create %d goroutines.\n", count) var waitGroup sync.WaitGroup ``` 在上面的代碼中,我們定義了sync.WaitGroup類型的變量,查看sync包的源碼我們可以發現,waitgroup.go文件位于sync目錄中,sync.WaitGroup的定義只不過是一個包含三個字段的結構體: ```go type WaitGroup struct { noCopy noCopy state1 [12]byte sema uint32 } ``` `syncGo.go`的輸出將顯示有關`sync.WaitGroup`變量工作方式的更多信息。 第三部分代碼如下: ```go fmt.Printf("%#v\n", waitGroup) for i := 0; i < count; i++ { waitGroup.Add(1) go func(x int) { defer waitGroup.Done() fmt.Printf("%d ", x) }(i) } ``` 在這里,你可以使用for循環創建所需數量的`goroutine`。(當然,也可以寫多個順序的Go語句。) 每次調用`sync.Add()`都會增加`sync.WaitGroup`變量中的計數器。需要注意的是,在go語句之前調用`sync.Add(1)`非常重要,以防止出現任何形式的競爭。當每個`goroutine`完成其工作時,將執行`sync.Done()`函數,以減少相同的計數器。 最后一部分代碼如下: ```go fmt.Printf("%#v\n", waitGroup) waitGroup.Wait() fmt.Println("\nExiting...") } ``` `sync.Wait()`調用將阻塞主程序,直到`sync.WaitGroup`變量中的計數器為零,從而保證所有`goroutine`能執行完成。 `syncGo.go`的輸出如下: ```bash $ go run syncGo.go Going to create 20 goroutines. sync.WaitGroup{noCopy:sync.noCopy{}, state1:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sema:0x0} sync.WaitGroup{noCopy:sync.noCopy{}, state1:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x14, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sema:0x0} 19 7 8 9 10 11 12 13 14 15 16 17 0 1 2 5 18 4 6 3 Exiting... $ go run syncGo.go -n 30 Going to create 30 goroutines. sync.WaitGroup{noCopy:sync.noCopy{}, state1:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sema:0x0} 1 0 4 5 17 7 8 9 10 11 12 13 2 sync.WaitGroup{noCopy:sync.noCopy{}, state1:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x17, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sema:0x0} 29 15 6 27 24 25 16 22 14 23 18 26 3 19 20 28 21 Exiting... $ go run syncGo.go -n 30 Going to create 30 goroutines. sync.WaitGroup{noCopy:sync.noCopy{}, state1:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sema:0x0} sync.WaitGroup{noCopy:sync.noCopy{}, state1:[12]uint8{0x0, 0x0, 0x0, 0x0, 0x1e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, sema:0x0} 29 1 7 8 2 9 10 11 12 4 13 15 0 6 5 22 25 23 16 28 26 20 19 24 21 14 3 17 18 27 Exiting... ``` `syncGo.go`的輸出因執行情況而異。另外,當`goroutines`的數量為30時,一些`goroutine`可能會在第二個`fmt.Printf(“%#v \ n”,waitGroup)`語句之前完成它們的工作。最后需要注意`sync.WaitGroup`中的`state1`字段是一個保存計數器的元素,該計數器根據`sync.Add()`和`sync.Done()`調用而增加和減少。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看