Golang 两个携程交替输出1-10
Golang 两个携程交替输出1-10
百度搜索了一些相关话题,感觉不是很好所以自己写了一个。技术栈waitgroup主进程等待,context携程退出,chan通信。理论上有这三点基本都可以,不过感觉个人实现的还不是很完美,望各位指正~
func Test4(t *testing.T) {s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}wg := sync.WaitGroup{}wg.Add(1) //注意这里为什么是1不是2:虽然是两个携程同时在运行,但是wg.Wait()只需要知道一个goroutine执行完毕,即不等待。后续通过context来终止携程运行。c1 := make(chan int, 1)defer close(c1)c2 := make(chan int, 1)defer close(c2)c1 <- 0ctx, cancel := context.WithCancel(context.Background())go func(ctx context.Context, c1, c2 chan int) {for {select {case index := <-c1:if index%2 == 0 {fmt.Println("1--", index)fmt.Println("=====================goroutine1 print :", s[index])if index+1 >= len(s) {wg.Done()} else {c2 <- index + 1}}case <-ctx.Done():fmt.Println("----------------------------goroutine2 exit ...")return }}}(ctx, c1, c2)go func(ctx context.Context, c1, c2 chan int) {for {select {case index := <-c2:if index%2 == 1 {fmt.Println("2--", index)fmt.Println("=====================goroutine1 print :", s[index])if index+1 >= len(s) {wg.Done()} else {c1 <- findex + 1}}case <-ctx.Done():fmt.Println("----------------------------goroutine2 exit ...")return}}}(ctx, c1, c2)wg.Wait()cancel()
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
