##### 为什么每次都能够执行成功?(go版本1.11,GOMAXPROCS 默认为CPU最大核数,即代码并行执行)
```golang
package main
import (
"fmt"
"time"
)
func main() {
arra := []int64{1,2,3,4}
arrb := []string{"a","b","c","d"}
c := make(chan bool)
go func(arra []int64) {
for i:=0;i < len(arra);i++ {
if flag := <-c;flag{
fmt.Println(arra[i])
c <- false
}
}
}(arra)
go func(arrb []string) {
for i:=0;i < len(arrb);i++ {
if flag := <- c;!flag{
fmt.Println(arrb[i])
c <- true
}
}
}(arrb)
c <- false
time.Sleep(time.Second * 2)
}
```
更多评论
##### 代码并行执行时,执行顺序不定,以下代码输出无规律
```go
package main
import (
"fmt"
"time"
)
func main() {
go func() {
fmt.Println(1)
}()
go func() {
fmt.Println(2)
}()
go func() {
fmt.Println(3)
}()
time.Sleep(time.Second * 1)
}
```
#1
For unbuffered channel, the sender will block on the channel until the receiver receives the data from the channel, whilst the receiver will also block on the channel until sender sends data into the channel.
#3