Golang Go语言中并发问题咨询
Golang Go语言中并发问题咨询
func main() {
runtime.GOMAXPROCS(1)
ch := make(chan int)
go count(ch, 10000)
go count(ch, 10001)
time.Sleep(10000 * time.Millisecond)
fmt.Printf(“exit\n”)
}
func count(r chan int, who int) {
for {
if who%2 == 0 {
r <- who
fmt.Printf("|write <- who|%d\n", who)
} else {
<-r
fmt.Printf("| <-r recv|%d\n", who)
}
}
}
输出是
| <-r recv|10001
| <-r recv|10001
|write <- who|10000
|write <- who|10000
为什么不是一个一个交替的形式
| <-r recv|10001
|write <- who|10000
| <-r recv|10001
|write <- who|10000
更多关于Golang Go语言中并发问题咨询的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html