启动两个线程, 一个输出 1,3,5,7…99, 另一个输出 2,4,6,8…100 最后 STDOUT 中按序输出 1,2,3,4,5…100
package main
import (
"fmt"
"time"
)
var pool = 100
//奇数
func oddNum(c chan int) {
for i := 1; i <= pool; i++ {
c <- i
if i%2 == 1 {
fmt.Println(i)
}
}
}
//偶数
func even(c chan int) {
for i := 1; i <= pool; i++ {
<-c
if i%2 == 0 {
fmt.Println(i)
}
}
}
func main() {
c := make(chan int)
go oddNum(c)
go even(c)
time.Sleep(time.Second * 1)
}