婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > golang goroutine順序輸出方式

golang goroutine順序輸出方式

熱門標簽:外呼系統顯本地手機號 評價高的400電話辦理 阿克蘇地圖標注 電話機器人軟件免費 excel地圖標注分布數據 外呼系統用什么卡 百度地圖標注后傳給手機 壽光微信地圖標注 涿州代理外呼系統

range字符串,使用goroutine打印

因為goroutine隨機執行

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順序循環打印ABC

分別使用sync.WaitGroup和context

使用sync.WaitGroup, 可控制循環次數

package main
import (
	"fmt"
	"sync"
)
//控制循環次數
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控制打印數量

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

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 解決Golang中goroutine執行速度的問題
  • golang gin 框架 異步同步 goroutine 并發操作
  • GOLANG使用Context管理關聯goroutine的方法
  • Golang 探索對Goroutine的控制方法(詳解)
  • 關于Golang中for-loop與goroutine的問題詳解
  • Golang 語言控制并發 Goroutine的方法

標簽:蘭州 吐魯番 汕頭 銅川 雞西 重慶 梅河口 欽州

巨人網絡通訊聲明:本文標題《golang goroutine順序輸出方式》,本文關鍵詞  golang,goroutine,順序,輸出,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《golang goroutine順序輸出方式》相關的同類信息!
  • 本頁收集關于golang goroutine順序輸出方式的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 西城区| 开原市| 岳阳市| 城步| 阜城县| 信宜市| 山阴县| 衡阳市| 洛隆县| 延寿县| 华亭县| 昌吉市| 许昌县| 镇原县| 慈溪市| 临安市| 年辖:市辖区| 玉山县| 泸定县| 蓝山县| 磐安县| 桃园县| 德州市| 罗江县| 淮阳县| 东阿县| 葫芦岛市| 保康县| 昔阳县| 政和县| 灵台县| 芒康县| 拉萨市| 天柱县| 永登县| 乌恰县| 哈尔滨市| 江西省| 乐亭县| 武威市| 贵州省|