佳木斯湛栽影视文化发展公司

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

golang goroutine順序輸出方式

熱門標簽:AI電銷 網(wǎng)站排名優(yōu)化 百度競價排名 地方門戶網(wǎng)站 服務(wù)外包 Linux服務(wù)器 呼叫中心市場需求 鐵路電話系統(tǒng)

range字符串,使用goroutine打印

因為goroutine隨機執(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

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

標簽:衡水 湘潭 黃山 仙桃 湖南 銅川 蘭州 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標題《golang goroutine順序輸出方式》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    荥阳市| 张家港市| 和平区| 乡城县| 微山县| 江口县| 察雅县| 滨海县| 烟台市| 吴旗县| 桂平市| 阿克苏市| 青州市| 峨眉山市| 茶陵县| 云龙县| 五大连池市| 华亭县| 宾阳县| 咸丰县| 曲麻莱县| 外汇| 怀柔区| 中超| 黄山市| 淮南市| 绥滨县| 晋宁县| 平罗县| 渝北区| 板桥市| 印江| 河津市| 思南县| 兴义市| 华池县| 高邑县| 河曲县| 溧阳市| 布拖县| 团风县|