一个逻辑处理器运行多个goroutine,这些goroutine的被调度顺序是怎么样的?

zhangyang9 · · 1352 次点击
话说为啥不直接粘贴代码,非得截图~别人想运行试试还得一个个敲~
#1
更多评论
```go package main import ( "fmt" "runtime" "sync" ) var wg sync.WaitGroup func main() { runtime.GOMAXPROCS(1) wg.Add(4) fmt.Println("Create Goroutines") go printPrime("A") go printPrime("B") go printPrime("C") go printPrime("D") fmt.Println("Waiting To Finish") wg.Wait() fmt.Println("Terminating Program") } func printPrime(prefix string) { defer wg.Done() next: for outer := 2; outer < 50; outer++ { for inner := 2; inner < outer; inner++ { if outer%inner == 0 { continue next } } fmt.Printf("%s:%d\n", prefix, outer) } fmt.Println("Completed", prefix) } ```
#2
```go package main import ( "fmt" "runtime" "sync" ) var wg sync.WaitGroup func main() { runtime.GOMAXPROCS(1) wg.Add(4) fmt.Println("Create Goroutines") go printPrime("A") go printPrime("B") go printPrime("C") go printPrime("D") fmt.Println("Waiting To Finish") wg.Wait() fmt.Println("Terminating Program") } func printPrime(prefix string) { defer wg.Done() next: for outer := 2; outer < 50; outer++ { for inner := 2; inner < outer; inner++ { if outer%inner == 0 { continue next } } fmt.Printf("%s:%d\n", prefix, outer) } fmt.Println("Completed", prefix) } ```
#3