package main
import (
"fmt"
"runtime"
"time"
)
func main() {
runtime.GOMAXPROCS(1)
go func() {
for i := 1; i <= 10; i++ {
fmt.Println(2*i - 1)
runtime.Gosched()
}
}()
go func() {
for i := 1; i <= 10; i++ {
fmt.Println(2 * i)
runtime.Gosched()
}
}()
time.Sleep(3 * time.Second)
}
原理:runtime.GOMAXPROCS(1)设置单核,runtime.Gosched()让出时间片
网友评论