golang goroutine順序輸出方式
更新時間:2021年04月29日 11:46:48 作者:浩聞城
這篇文章主要介紹了golang goroutine順序輸出方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
range字符串,使用goroutine打印
因為goroutine隨機(jī)執(zhí)行
for _, v := range str { go func() { fmt.Println(string(v)) }() }
輸出:
5
5
5
5
5
可以使用chan順序輸出
for _, c := range str{ ch := make(chan rune) go func(ch <-chan rune) { key := <-ch fmt.Println(string(key)) }(ch) ch <- c }
輸出:
1
2
3
4
5
補充:golang goroutine順序循環(huán)打印ABC
分別使用sync.WaitGroup和context
使用sync.WaitGroup, 可控制循環(huán)次數(shù)
package main import ( "fmt" "sync" ) //控制循環(huán)次數(shù) var count = 5 func main() { wg := sync.WaitGroup{} chanA := make(chan struct{}, 1) chanB := make(chan struct{}, 1) chanC := make(chan struct{}, 1) chanA <- struct{}{} wg.Add(3) go printA(&wg, chanA, chanB) go printB(&wg, chanB, chanC) go printC(&wg, chanC, chanA) wg.Wait() } func printA(wg *sync.WaitGroup, chanA, chanB chan struct{}) { defer wg.Done() for i := 0; i < count; i++ { <-chanA fmt.Println("A") chanB <- struct{}{} } } func printB(wg *sync.WaitGroup, chanB, chanC chan struct{}) { defer wg.Done() for i := 0; i < count; i++ { <-chanB fmt.Println("B") chanC <- struct{}{} } } func printC(wg *sync.WaitGroup, chanC, chanA chan struct{}) { defer wg.Done() for i := 0; i < count; i++ { <-chanC fmt.Println("C") chanA <- struct{}{} } }
使用context.WithCancel,通過time.Sleep控制打印數(shù)量
package main import ( "context" "fmt" "time" ) func main() { chanA := make(chan struct{}, 1) chanB := make(chan struct{}, 1) chanC := make(chan struct{}, 1) chanA <- struct{}{} ctx1, cancel1 := context.WithCancel(context.Background()) ctx2, cancel2 := context.WithCancel(context.Background()) ctx3, cancel3 := context.WithCancel(context.Background()) go printA(ctx1, chanA, chanB) go printB(ctx2, chanB, chanC) go printC(ctx3, chanC, chanA) time.Sleep(100 * time.Microsecond) cancel1() cancel2() cancel3() } func printA(ctx context.Context, chanA, chanB chan struct{}) { for { select { case <-ctx.Done(): fmt.Println("cancel by parent") // 不會輸出 return case <-chanA: fmt.Println("A") chanB <- struct{}{} } } } func printB(ctx context.Context, chanB, chanC chan struct{}) { for { select { case <-ctx.Done(): fmt.Println("cancel by parent") // 不會輸出 return case <-chanB: fmt.Println("B") chanC <- struct{}{} } } } func printC(ctx context.Context, chanC, chanA chan struct{}) { for { select { case <-ctx.Done(): fmt.Println("cancel by parent") // 不會輸出 return case <-chanC: fmt.Println("C") chanA <- struct{}{} } } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Golang中json和jsoniter的區(qū)別使用示例
這篇文章主要介紹了Golang中json和jsoniter的區(qū)別使用示例,本文給大家分享兩種區(qū)別,結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2023-12-12使用golang進(jìn)行http,get或postJson請求
這篇文章主要為大家詳細(xì)介紹了如何使用golang進(jìn)行http,get或postJson請求,文中的示例代碼簡潔易懂,具有一定的借鑒價值,感興趣的小伙伴可以了解一下2023-12-12golang控制結(jié)構(gòu)select機(jī)制及使用示例詳解
這篇文章主要介紹了golang控制結(jié)構(gòu)select機(jī)制及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10詳解Go語言中Goroutine退出機(jī)制的原理及使用
goroutine是Go語言提供的語言級別的輕量級線程,在我們需要使用并發(fā)時,我們只需要通過?go?關(guān)鍵字來開啟?goroutine?即可。本文就來詳細(xì)講講Goroutine退出機(jī)制的原理及使用,感興趣的可以了解一下2022-07-07