package main
import (
"sync"
"fmt"
"time"
)
var (
switchFlow chan int
wg sync.WaitGroup
)
func routine(i int, serialNumber int) {
time.Sleep(100 * time.Millisecond)
loop:
for {
select {
case s := <- switchFlow:
if s == serialNumber {
fmt.Println("routine: ", i+1)
break loop
} else {
//fmt.Println("接受到的编号是: ", s)
switchFlow <- s
}
default:
time.Sleep(10 * time.Millisecond)
}
}
wg.Done()
switchFlow <- serialNumber+1
}
func main() {
switchFlow = make(chan int)
wg.Add(100)
for i := 0; i < 100; i++ {
go routine(i, 10+i)
}
//引爆点
switchFlow <- 10
wg.Wait()
close(switchFlow)
fmt.Println("程序结束")
}
多个协程顺序打印数字
猜你喜欢
转载自blog.csdn.net/jujueduoluo/article/details/80500450
今日推荐
周排行