对golang的Channel初始化的有缓存与无缓存解释
paladinosment · · 10195 次点击 · · 开始浏览首先编程的时候遇到疑问,输出跟我所想预想不一样,后来查到了golang社区的帖子,其中一篇帖子 :健哥大人 做出了一些解释。
我摘抄重点过来:
无缓冲的与有缓冲channel有着重大差别,那就是一个是同步的 一个是非同步的。
比如
c1:=make(chan int) 无缓冲
c2:=make(chan int,1) 有缓冲
c1<-1
无缓冲: 不仅仅是向 c1 通道放 1,而是一直要等有别的携程 <-c1 接手了这个参数,那么c1<-1才会继续下去,要不然就一直阻塞着。
有缓冲: c2<-1 则不会阻塞,因为缓冲大小是1(其实是缓冲大小为0),只有当放第二个值的时候,第一个还没被人拿走,这时候才会阻塞。
package main
import (
"fmt"
)
func main() {
jobs := make(chan int, 1)
done := make(chan bool)
go func() {
// fmt.Println("GoStart")
for i := 1; ; i++ {
// fmt.Println("GoforSTART", i)
j, more := <-jobs
if more {
fmt.Println("received job", j)
} else {
fmt.Println("received all jobs")
done <- true
return
}
// fmt.Println("GoforEND", i)
}
}()
for j := 1; j <= 3; j++ {
// fmt.Println("OutFOR", j)
jobs <- j
fmt.Println("sent job", j)
}
close(jobs)
fmt.Println("sent all jobs")
<-done
}
sent job 1 received job 1 sent job 2 received job 2 sent job 3 sent all jobs received job 3 received all jobs这是有缓存的情况,输出跟大家所预想的是一样的,每次遇到
jobs <- j
j, more := <-jobs
jobs := make(chan int, 1)
jobs := make(chan int)并将代码中的注释部分全部还原执行。
OutFOR 1 GoStart GoforSTART 1 received job 1 GoforEND 1 GoforSTART 2 sent job 1 OutFOR 2 sent job 2 OutFOR 3 received job 2 GoforEND 2 GoforSTART 3 received job 3 GoforEND 3 GoforSTART 4 sent job 3 sent all jobs received all jobs
j, more := <-jobs处阻塞,回到外部main()的for循环。
sent job 2 OutFOR 3紧跟
OutFOR 2得到验证。
jobs <- j
示例3:
package main
import (
"fmt"
)
func main() {
jobs := make(chan int)
go func() {
fmt.Println("Goroutin Start")
for {
select {
case src := <-jobs:
fmt.Println("Get ", src)
}
}
}()
for j := 0; j < 5; j++ {
jobs <- j
fmt.Println("Sent job ", j)
}
close(jobs)
fmt.Println("finish.")
}其输出为:
Goroutin Start Get 0 Sent job 0 Sent job 1 Get 1 Get 2 Sent job 2 Sent job 3 Get 3 Get 4 Sent job 4 finish.
当遇到向channel写入值,是否往下执行是交替的,也就是说是随机的。
本文示例环境是go 1.4,本文所做的解释也是因为携程都是利用同一cpu在不同时间片上运行所做的输出分析。
goroutine利用阻塞同步携程之间的数据是非常重要的思想,但大家编程的时候要特别小心。
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
首先编程的时候遇到疑问,输出跟我所想预想不一样,后来查到了golang社区的帖子,其中一篇帖子 :健哥大人 做出了一些解释。
我摘抄重点过来:
无缓冲的与有缓冲channel有着重大差别,那就是一个是同步的 一个是非同步的。
比如
c1:=make(chan int) 无缓冲
c2:=make(chan int,1) 有缓冲
c1<-1
无缓冲: 不仅仅是向 c1 通道放 1,而是一直要等有别的携程 <-c1 接手了这个参数,那么c1<-1才会继续下去,要不然就一直阻塞着。
有缓冲: c2<-1 则不会阻塞,因为缓冲大小是1(其实是缓冲大小为0),只有当放第二个值的时候,第一个还没被人拿走,这时候才会阻塞。
package main
import (
"fmt"
)
func main() {
jobs := make(chan int, 1)
done := make(chan bool)
go func() {
// fmt.Println("GoStart")
for i := 1; ; i++ {
// fmt.Println("GoforSTART", i)
j, more := <-jobs
if more {
fmt.Println("received job", j)
} else {
fmt.Println("received all jobs")
done <- true
return
}
// fmt.Println("GoforEND", i)
}
}()
for j := 1; j <= 3; j++ {
// fmt.Println("OutFOR", j)
jobs <- j
fmt.Println("sent job", j)
}
close(jobs)
fmt.Println("sent all jobs")
<-done
}
sent job 1 received job 1 sent job 2 received job 2 sent job 3 sent all jobs received job 3 received all jobs这是有缓存的情况,输出跟大家所预想的是一样的,每次遇到
jobs <- j
j, more := <-jobs
jobs := make(chan int, 1)
jobs := make(chan int)并将代码中的注释部分全部还原执行。
OutFOR 1 GoStart GoforSTART 1 received job 1 GoforEND 1 GoforSTART 2 sent job 1 OutFOR 2 sent job 2 OutFOR 3 received job 2 GoforEND 2 GoforSTART 3 received job 3 GoforEND 3 GoforSTART 4 sent job 3 sent all jobs received all jobs
j, more := <-jobs处阻塞,回到外部main()的for循环。
sent job 2 OutFOR 3紧跟
OutFOR 2得到验证。
jobs <- j
示例3:
package main
import (
"fmt"
)
func main() {
jobs := make(chan int)
go func() {
fmt.Println("Goroutin Start")
for {
select {
case src := <-jobs:
fmt.Println("Get ", src)
}
}
}()
for j := 0; j < 5; j++ {
jobs <- j
fmt.Println("Sent job ", j)
}
close(jobs)
fmt.Println("finish.")
}其输出为:
Goroutin Start Get 0 Sent job 0 Sent job 1 Get 1 Get 2 Sent job 2 Sent job 3 Get 3 Get 4 Sent job 4 finish.
当遇到向channel写入值,是否往下执行是交替的,也就是说是随机的。
本文示例环境是go 1.4,本文所做的解释也是因为携程都是利用同一cpu在不同时间片上运行所做的输出分析。
goroutine利用阻塞同步携程之间的数据是非常重要的思想,但大家编程的时候要特别小心。