该示例取自“围棋之旅”:https : //tour.golang.org/concurrency/1
显然,程序输出应该有 10 行:5 行代表“hello”,5 行代表“world”。
但我们有:
Linux - 9 行
MacOS - 10 行
Linux 输出(9 行):
$ go run 1.go
hello
world
hello
world
hello
world
world
hello
hello
MacOS X 输出(10 行):
$ go run 1.go
hello
world
world
hello
hello
world
hello
world
hello
world
任何人都可以解释 -为什么?
Linux uname -a:
Linux desktop 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1 (2015-05-24) x86_64 GNU/Linux
macOS uname -a:
Darwin 14.5.0 Darwin Kernel Version 14.5.0: Thu Jul 9 22:56:16 PDT 2015; root:xnu-2782.40.6~1/RELEASE_X86_64 x86_64
来自巡演的源代码:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(1000 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
慕工程0101907
相关分类