golang并发疑问

setcookie · · 3590 次点击
package main import ( "fmt" ) var j int = 0 func Count(ch chan int) { j++ ch <- j fmt.Printf("Counting:%d\n", j) } func main() { chs := make(chan int) for i := 0; i < 10; i++ { go Count(chs) } for i := 0; i < 10; i++ { fmt.Println(<-chs) } }
#1
更多评论
package main import ( "fmt" ) var j int = 0 func Count(ch chan int) { j++ fmt.Printf("Counting:%d\n", j) ch <- j } func main() { chs := make(chan int) for i := 0; i < 10; i++ { go Count(chs) } for i := 0; i < 10; i++ { fmt.Println(<-chs) } }
#2
main函数结束时time.Sleep()一下你就明白了
#3