本质上还是通过channel的队列性质(FIFO)来实现的,以这个思路为主。

 1 func test1() {        
 2     var wg sync.WaitGroup
 3 
 4     var ch1 = make(chan int, 1)
 5     var ch2 = make(chan int, 1)
 6     var ch3 = make(chan int, 1)
 7     var printSelf func(pCont int, inChan chan int, outChan chan int)
 8     printSelf = func(pCont int, inChan chan int, outChan chan int) {
 9         defer wg.Done()
10 
11         select {
12         case _ = <- inChan:
13             fmt.Println("printVal: ", pCont)
14         }
15         outChan <- 0 //可以写入了
16     }
17 
18     maxRun := 100
19     maxGo := 3
20     incr := 0
21 
22     ch1 <- 0
23     for i := 0; i < ((maxRun/maxGo) + 1); i++ {
24         wg.Add(3)
25         go printSelf(incr, ch1, ch2)
26         incr++
27         go printSelf(incr, ch2, ch3)
28         incr++
29         go printSelf(incr, ch3, ch1)
30         incr++
31         wg.Wait()
32     }        
33 }

 

posted on 2023-07-30 22:15  黑熊一只  阅读(13)  评论(0编辑  收藏  举报