goroutine 是 Go語言中的輕量級線程實現,由 Go 運行時(runtime)管理。Go 程序會智能地將 goroutine 中的任務合理地分配給每個 CPU。
01介紹
Golang 語言的優勢之一是天生支持并發,我們在 Golang 語言開發中,通常使用的并發控制方式主要有 Channel,WaitGroup 和 Context,本文我們主要介紹一下 Golang 語言中并發控制的這三種方式怎么使用?關于它們各自的詳細介紹在之前的文章已經介紹過,感興趣的讀者朋友們可以按需翻閱。
02Channel
在 Golang 語言中,Channel 不僅可以用于協程之間通信,還可以使用 Channel 控制子協程,而且使用 Channel 實現并發控制比較簡單,比如以下示例,我們在 Golang 應用程序中啟動兩個協程,分別是主協程和子協程,主協程需要等待子協程運行結束后再退出程序。
示例代碼:
func main () {
done := make(chan struct{})
go func() {
fmt.Println("goroutine run over")
done - struct{}{}
}()
- done
fmt.Println("main goroutine run over")
}
閱讀上面這段代碼,我們在子 goroutine 運行結束后,通過 Channel 通知主 goroutine 退出程序,實際上也可以反過來處理,主 goroutine 通知子 goroutine 退出程序,主 goroutine 向 channel 中發送數據,子 goroutine 等待接收 channel 中的數據。
03sync.WaitGroup
如果在 Golang 應用程序中,需要讓主 goroutine 等待多個 goroutine 都運行結束后再退出程序,我們應該怎么實現呢?是的,同樣可以使用 Channel 實現,但是,有一個更優雅的實現方式,那就是 WaitGroup,顧名思義,WaitGroup 就是等待一組 goroutine 運行結束。
示例代碼:
func main () {
wg := sync.WaitGroup{}
wg.Add(10)
for i := 0; i 10; i++ {
go func(id int) {
fmt.Println(id, "運行結束")
wg.Done()
}(i)
}
wg.Wait()
fmt.Println("main goroutine run over")
}
閱讀上面這段代碼,我們啟動 10 個子 goroutine,主 goroutine 需要等待 10 個子 goroutine 都運行結束后再退出程序,我們使用的是 WaitGroup,它有三個方法,分別是 Add、Done 和 Wait,實際上 WaitGroup 維護了一個計數器,這三個方法都是圍繞這個計數器工作,Add 用于設置計數器的數值,Done 用于扣減計數器的數值,Wait 在計數器數值為 0 之前一直阻塞。關于 WaitGroup 的源碼解讀,在之前的文章中已介紹過,限于篇幅,這里就不再贅述。
04Context
Channel 和 WaitGroup 通常用于父子兩個層級的 goroutine 的應用程序的并發控制中,如果在 Golang 應用程序中,子協程繼續派生出協程,我們應該怎么控制呢?這種多級 goroutine 的應用程序,我們可以使用 Context 實現并發控制。
示例代碼:
func main() {
ctx, cancel := context.WithCancel(context.Background())
go firstCtx(ctx)
time.Sleep(5 * time.Second)
fmt.Println("stop all sub goroutine")
cancel()
time.Sleep(5 * time.Second)
}
func firstCtx(ctx context.Context) {
go secondCtx(ctx)
for {
select {
case -ctx.Done():
fmt.Println("first done")
return
default:
fmt.Println("first running")
time.Sleep(2 * time.Second)
}
}
}
func secondCtx(ctx context.Context) {
for {
select {
case -ctx.Done():
fmt.Println("second done")
return
default:
fmt.Println("second running")
time.Sleep(2 * time.Second)
}
}
}
閱讀上面這段代碼,在子協程 firstCtx 啟動子協程 secondCtx,主 goroutine 創建 context,并把 context 傳遞到所有子協程,然后主 goroutine 通過調用 cancle 停掉所有子協程。
05總結
本文我們介紹了不同場景中分別適合哪種控制并發 goroutine 的方式,其中,channel 適合控制少量 并發 goroutine,WaitGroup 適合控制一組并發 goroutine,而 context 適合控制多級并發 goroutine。
到此這篇關于Golang 語言控制并發 Goroutine的方法的文章就介紹到這了,更多相關Golang并發控制Goroutine內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 解決Golang中goroutine執行速度的問題
- golang goroutine順序輸出方式
- golang gin 框架 異步同步 goroutine 并發操作
- GOLANG使用Context管理關聯goroutine的方法
- Golang 探索對Goroutine的控制方法(詳解)
- 關于Golang中for-loop與goroutine的問題詳解