golang 的 buffered channel 及 unbuffered channel
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 main import (
"fmt"
"time"
) func main() {
ch := make(chan int) go func(ch chan int) {
fmt.Println("Func goroutine begins sending data")
ch <-
fmt.Println("Func goroutine ends sending data")
}(ch) fmt.Println("Main goroutine sleeps 2 seconds")
time.Sleep(time.Second * ) 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 seconds
Func goroutine begins sending data
Main goroutine begins receiving data
Main goroutine received data: 1
Func 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 main import (
"fmt"
"time"
) func main() {
ch := make(chan int, ) go func(ch chan int) {
for i := ; i <= ; i++ {
ch <- i
fmt.Println("Func goroutine sends data: ", i)
}
close(ch)
}(ch) fmt.Println("Main goroutine sleeps 2 seconds")
time.Sleep(time.Second * ) 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 seconds
Func goroutine sends data: 1
Func goroutine sends data: 2
Main goroutine begins receiving data
Main goroutine received data: 1
Main goroutine received data: 2
Main goroutine received data: 3
Func goroutine sends data: 3
Func goroutine sends data: 4
Func goroutine sends data: 5
Main goroutine received data: 4
Main 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.
golang 的 buffered channel 及 unbuffered channel的更多相关文章
- [GO]无缓冲通道(unbuffered channel)
无缓冲通道(unbuffered channel)是指在接收前没有能力保存任何值的通道,在之前的例子中使用的都是无缓冲通道,需要注意的是,对于无缓冲通道而言,不管是往通道里写数据还是从通道里读数据,都 ...
- Golang的goroutine协程和channel通道
一:简介 因为并发程序要考虑很多的细节,以保证对共享变量的正确访问,使得并发编程在很多情况下变得很复杂.但是Go语言在开发并发时,是比较简洁的.它通过channel来传递数据.数据竞争这个问题在gol ...
- go语言之行--golang核武器goroutine调度原理、channel详解
一.goroutine简介 goroutine是go语言中最为NB的设计,也是其魅力所在,goroutine的本质是协程,是实现并行计算的核心.goroutine使用方式非常的简单,只需使用go关键字 ...
- 实时事件统计项目:优化flume:用file channel代替mem channel
背景:利用kafka+flume+morphline+solr做实时统计. solr从12月23号开始一直没有数据.查看日志发现,因为有一个同事加了一条格式错误的埋点数据,导致大量error. 据推断 ...
- netty源码解解析(4.0)-12 Channel NIO实现:channel初始化
创建一个channel实例,并把它register到eventLoopGroup中之后,这个channel然后处于inactive状态,仍然是不可用的.只有在bind或connect方法调用成功之后才 ...
- Netty Tutorial Part 1.5: On Channel Handlers and Channel Options [z]
Intro: After some feedback on Part 1, and being prompted by some stackoverflow questions, I want to ...
- Fibre Channel和Fiber Channel
Fibre Channel也就是"网状通道"的意思,简称FC. 由于Fiber和Fibre只有一字之差,所以产生了很多流传的误解. FC只代表Fibre Channel,而不是 ...
- golang channel 用法转的
一.Golang并发基础理论 Golang在并发设计方面参考了C.A.R Hoare的CSP,即Communicating Sequential Processes并发模型理论.但就像John Gra ...
- 深入学习golang(2)—channel
Channel 1. 概述 “网络,并发”是Go语言的两大feature.Go语言号称“互联网的C语言”,与使用传统的C语言相比,写一个Server所使用的代码更少,也更简单.写一个Server除了网 ...
随机推荐
- DownloadProvider源码解析——与Volley对比
1.AndroidHttpClient的创建 DownloadManager: 在DownloadThread的run方法里 public void run() { Process.setThread ...
- C#设计模式--观察者模式(发布-订阅模式)
0.C#设计模式--简单工厂模式 1.C#设计模式--工厂方法模式 2.C#设计模式--抽象工厂模式 3.C#设计模式--单例模式 4.C#设计模式--建造者模式 5.C#设计模式--原型模式 6.C ...
- [转]复制、移动和删除:cp, rm, mv
转自:http://www.cnblogs.com/benio/archive/2010/07/27/1785929.html 要复制文件,请使用cp(copy)命令.不过,cp命令的用途很多.除了单 ...
- 使用 tabindex 改变Tab 键顺序
使用 tabindex原文 https://developers.google.cn/web/fundamentals/accessibility/focus/using-tabindex 在表单上使 ...
- 洛谷P1403 约数研究【思维】
题目:https://www.luogu.org/problemnew/show/P1403 题意: 定义$f(n)$为n的因子个数.给定一个数n,求$f(1)$到$f(n)$之和. 思路: 最直接的 ...
- 基础知识系列☞关键字→virtual
看到自己以前的一篇博客,感觉还差点什么,√,代码... using System; namespace Test { public class Program { private static voi ...
- 查看CUDA和cuDNN的版本号
1.查看cuda版本 cat /usr/local/cuda/version.txt2.查看cudnn版本 cat /usr/local/cuda/include/cudnn.h | grep CUD ...
- 为什么css定位雪碧图(合成图)都要以负号开头?
(1)正常来说 定位坐标是以 合成图片 左上角这个点作为原点(0px,0px)开始读取的, 而你的图片全都在坐标系的 第四象限 background-position: x y:(x,y为数值或百分比 ...
- 自动化运维工具-mussh工具安装配置及简单使用讲解
1.先决条件: 安装pssh工具的主机针对远程主机需要配置免秘钥认证: ssh-keygen -t rsa ssh-copy-id [remotehost] 2.下载mussh工具安装介质: http ...
- shell技巧之以逆序形式打印行
测试文本内容如下: # cat textfile hadoop hdfs yarn spark zookeeper mapreduce hive hbase scala kafka CHAVIN my ...