具体看下面2个例子:
无 buffer 的:
http://play.golang.org/p/RwPbCcueWh
func main() {
ch := make(chan bool)
go func() {
println("fuck buffer channel")
<-ch
}()
ch <- true
}
有buffer的:
http://play.golang.org/p/YV9H6WPYuJ
func main() {
ch := make(chan bool, 1)
go func() {
println("fuck buffer channel")
<-ch
}()
ch <- true
}
第二个程序(有buffer的chan)无法保证收/发同步, 导致main退出时 println
还未执行.
说明带buffer的chan是不能保证收/发同步.
下面是一个构造的测试例子
http://play.golang.org/p/vQQCv9nG15
package main
import (
"fmt"
"sync"
"time"
)
func main() {
testNoBufferChan()
testBufferChan()
}
func testNoBufferChan() {
wg := new(sync.WaitGroup)
ch := make(chan bool)
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(10 * time.Second)
fmt.Println(time.Now(), "NoBufferChan recv begin")
<-ch
fmt.Println(time.Now(), "NoBufferChan recv end")
}()
fmt.Println(time.Now(), "NoBufferChan send begin")
ch <- true
fmt.Println(time.Now(), "NoBufferChan send end")
wg.Wait()
}
func testBufferChan() {
wg := new(sync.WaitGroup)
ch := make(chan bool, 1)
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(10 * time.Second)
fmt.Println(time.Now(), "BufferChan recv begin")
<-ch
fmt.Println(time.Now(), "BufferChan recv end")
}()
fmt.Println(time.Now(), "BufferChan send begin")
ch <- true
fmt.Println(time.Now(), "BufferChan send end")
wg.Wait()
}
在 play 上的结果(play上的是假时间):
2009-11-10 23:00:00 +0000 UTC NoBufferChan send begin
2009-11-10 23:00:10 +0000 UTC NoBufferChan recv begin
2009-11-10 23:00:10 +0000 UTC NoBufferChan recv end
2009-11-10 23:00:10 +0000 UTC NoBufferChan send end
2009-11-10 23:00:10 +0000 UTC BufferChan send begin
2009-11-10 23:00:10 +0000 UTC BufferChan send end
2009-11-10 23:00:20 +0000 UTC BufferChan recv begin
2009-11-10 23:00:20 +0000 UTC BufferChan recv end
在本机运行的结果:
2014-05-09 10:41:17.5611304 +0800 CST NoBufferChan send begin
2014-05-09 10:41:27.5637025 +0800 CST NoBufferChan recv begin
2014-05-09 10:41:27.5637025 +0800 CST NoBufferChan recv end
2014-05-09 10:41:27.5637025 +0800 CST NoBufferChan send end
2014-05-09 10:41:27.5647026 +0800 CST BufferChan send begin
2014-05-09 10:41:27.5647026 +0800 CST BufferChan send end
2014-05-09 10:41:37.5662746 +0800 CST BufferChan recv begin
2014-05-09 10:41:37.5662746 +0800 CST BufferChan recv end
可以发现带Buffer的chan收/发完成时间相差了10秒.
有疑问加站长微信联系(非本文作者)