Go循环打印cat-dog-fish。。。。。

package main

import (
	"fmt"
	"sync"
)

// 三个协程交替打印 cat dog fish
var repeatCount = 10

func main() {
	// wg 用来防止主协程提前先退出
	wg := &sync.WaitGroup{}
	wg.Add(3)

	chCat := make(chan struct{}, 1)
	chDog := make(chan struct{}, 1)
	chFish := make(chan struct{}, 1)
	defer func() {
		close(chCat)
		close(chDog)
		close(chFish)
	}()

	go printAnimal(chCat, chDog, "cat", wg)
	go printAnimal(chDog, chFish, "dog", wg)
	go printAnimal(chFish, chCat, "fish", wg)
	chCat <- struct{}{}
	wg.Wait()
}

// wg 需要传指针
func printAnimal(in, out chan struct{}, s string, wg *sync.WaitGroup) {
	count := 0
	for {
		<-in
		count++
		fmt.Printf("%s,%d\n", s, count)
		out <- struct{}{}
		if count >= repeatCount {
			wg.Done()
			return
		}
	}
}

posted @ 2024-02-19 11:11  朝阳1  阅读(54)  评论(0)    收藏  举报