funcshow(s string) { for i := 0; i < 3; i++ { fmt.Println(s) // time.Sleep(1) }
} funcmain() { fmt.Printf("CPU num:%d\n", runtime.NumCPU()) runtime.GOMAXPROCS(1) fmt.Printf("CPU num:%d\n", runtime.NumCPU()) go show("java") go show("C++") go show("python") show("C#") } package main
import ( "fmt" "runtime" )
funcshow(s string) { for i := 0; i < 3; i++ { fmt.Println(s) // time.Sleep(1) }
} funcmain() { fmt.Printf("CPU num:%d\n", runtime.NumCPU()) runtime.GOMAXPROCS(1) fmt.Printf("CPU num:%d\n", runtime.NumCPU()) go show("java") go show("C++") go show("python") show("C#") }
再设置 CPU 核心数的时候,如果设置为1则在这个程序中则看不到交替运行输出的场景,设置为其它值则可以,不过我记得 CPU 单核好像也能上下文切换来着的,可能它这里不作切换?