都是一个货色,要么使用无缓冲channel, 要么使用有缓冲chan+waitgroup等待协程退出,或者使用全局变量判断是否终止协程
2个协程交替打印奇数和偶数
无缓冲channel实现
go
package main
import "fmt"
func main() {
maxval := 10
ch1 := make(chan struct{})
ch2 := make(chan struct{})
close := make(chan struct{})
go func() {
for i := 1; i <= maxval ; i++ {
if i%2 == 1 {
<-ch1
fmt.Printf("%d\n", i)
ch2 <- struct{}{}
}
}
}()
go func() {
for i := 1; i <= maxval ; i++ {
if i%2 == 0 {
<-ch2
fmt.Printf("%d\n", i)
if i == maxval {
close <- struct{}{}
} else {
ch1 <- struct{}{}
}
}
}
}()
ch1 <- struct{}{}
<-close
}
有缓冲channel实现
go
package main
import "fmt"
func main() {
maxval := 10
ch1 := make(chan struct{}, 1)
ch2 := make(chan struct{}, 1)
close := make(chan struct{})
go func() {
for i := 1; i <= maxval; i++ {
if i%2 == 1 {
<-ch1
fmt.Printf("%d\n", i)
ch2 <- struct{}{}
}
}
}()
go func() {
for i := 1; i <= maxval; i++ {
if i%2 == 0 {
<-ch2
fmt.Printf("%d\n", i)
ch1 <- struct{}{}
}
}
close <- struct{}{}
}()
ch1 <- struct{}{}
<-close
}
有缓冲channel+ waitgroup
go
package main
import (
"fmt"
"sync"
)
func main() {
maxval := 10
ch1 := make(chan struct{}, 1)
ch2 := make(chan struct{}, 1)
wg := &sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
for i := 1; i <= maxval; i++ {
if i%2 == 1 {
<-ch1
fmt.Printf("%d\n", i)
ch2 <- struct{}{}
}
}
}()
go func() {
defer wg.Done()
for i := 1; i <= maxval; i++ {
if i%2 == 0 {
<-ch2
fmt.Printf("%d\n", i)
ch1 <- struct{}{}
}
}
}()
ch1 <- struct{}{}
wg.Wait()
}
N个协程交替打印1到maxVal
go
package main
import (
"fmt"
)
func main() {
cur := 0
maxval := 10
N := 3
chs := make([]chan struct{}, 3)
closeCh := make(chan struct{})
for i := 0; i < N; i++ {
chs[i] = make(chan struct{})
}
for i := 0; i < N; i++ {
go func(i int) {
for {
_, ok := <-chs[i]
if !ok {
//fmt.Printf("go %d close\n", i)
return
}
fmt.Printf("go %d print %d\n", i, cur)
if cur == maxval {
closeCh <- struct{}{}
return
}
cur++
chs[(i+1)%N] <- struct{}{}
}
}(i)
}
chs[0] <- struct{}{}
<-closeCh
for i := 0; i < N; i++ {
close(chs[i])
}
fmt.Printf("main end\n")
}
使用三个协程分别打印A,B,C打印这个100次
go
package main
import (
"fmt"
)
func main() {
cnt := 2
N := 3
chs := make([]chan struct{}, 3)
closeCh := make(chan struct{})
for i := 0; i < N; i++ {
chs[i] = make(chan struct{}, 1)
}
for i := 0; i < N; i++ {
go func(i int) {
b := byte('A') + byte(i)
for j := 0; j < cnt; j++ {
<-chs[i]
fmt.Printf("go %d print %c\n", i, b)
chs[(i+1)%N] <- struct{}{}
}
if i == N-1 {
closeCh <- struct{}{}
}
}(i)
}
chs[0] <- struct{}{}
<-closeCh
for i := 0; i < N; i++ {
close(chs[i])
}
fmt.Printf("main end\n")
}