博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang 的 buffered channel 及 unbuffered channel
阅读量:6250 次
发布时间:2019-06-22

本文共 2943 字,大约阅读时间需要 9 分钟。

The channel is divided into two categories: unbuffered and buffered.

(1) Unbuffered channel

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. Check the following example:

package mainimport (        "fmt"        "time")func main() {        ch := make(chan int)        go func(ch chan int) {                fmt.Println("Func goroutine begins sending data")                ch <- 1                fmt.Println("Func goroutine ends sending data")        }(ch)        fmt.Println("Main goroutine sleeps 2 seconds")        time.Sleep(time.Second * 2)        fmt.Println("Main goroutine begins receiving data")        d := <-ch        fmt.Println("Main goroutine received data:", d)        time.Sleep(time.Second)}

The running result likes this:

Main goroutine sleeps 2 secondsFunc goroutine begins sending dataMain goroutine begins receiving dataMain goroutine received data: 1Func goroutine ends sending data

After the main goroutine is launched, it will sleep immediately("Main goroutine sleeps 2 seconds" is printed), and this will cause main goroutine relinquishes the CPU to the func goroutine("Func goroutine begins sending data" is printed). But since the main goroutine is sleeping and can't receive data from the channel, so ch <- 1 operation in func goroutine can't complete until d := <- ch in main goroutine is executed(The final 3 logs are printed).

 

(2) Buffered channel

Compared with unbuffered counterpart, the sender of buffered channel will block when there is no empty slot of the channel, while the receiver will block on the channel when it is empty. Modify the above example:

package mainimport (        "fmt"        "time")func main() {        ch := make(chan int, 2)        go func(ch chan int) {                for i := 1; i <= 5; i++ {                        ch <- i                        fmt.Println("Func goroutine sends data: ", i)                }                close(ch)        }(ch)        fmt.Println("Main goroutine sleeps 2 seconds")        time.Sleep(time.Second * 2)        fmt.Println("Main goroutine begins receiving data")        for d := range ch {                fmt.Println("Main goroutine received data:", d)        }}

The executing result is as follows:

Main goroutine sleeps 2 secondsFunc goroutine sends data:  1Func goroutine sends data:  2Main goroutine begins receiving dataMain goroutine received data: 1Main goroutine received data: 2Main goroutine received data: 3Func goroutine sends data:  3Func goroutine sends data:  4Func goroutine sends data:  5Main goroutine received data: 4Main goroutine received data: 5

In this sample, since the channel has 2 slots, so the func goroutine will not block until it sends the third element.

P.S., "make(chan int, 0)" is equal to "make(chan int)", and it will create an unbuffered int channel too.

 

转载地址:http://enysa.baihongyu.com/

你可能感兴趣的文章
C++ 与OpenCV 学习笔记
查看>>
【CV学习7】FAST算法详解
查看>>
11月20日学习内容整理:jquery插件
查看>>
预科班第四次考核总结
查看>>
【js】再谈移动端的模态框实现
查看>>
html
查看>>
Java变量类型
查看>>
[leetcode-89-Gray Code]
查看>>
mysql 存储过程的基本语法知识
查看>>
数据分析师到底在做什么?
查看>>
pt-heartbeat工具监控MySQL复制延迟
查看>>
指尖下的js —— 多触式web前端开发之三:处理复杂手势(转)
查看>>
spring boot项目配置文件集合
查看>>
cube-ui的用法
查看>>
2015.4.21 SetWindowPos函数用法
查看>>
2011-12-14 调用cmd并获得输入输出+网络访问
查看>>
解决nim db_mysql could not load: libmysql.dll的问题
查看>>
JavaScript之再谈回调与闭包
查看>>
优化PHP代码的一些建议
查看>>
android学习网站
查看>>