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的更多相关文章

  1. [GO]无缓冲通道(unbuffered channel)

    无缓冲通道(unbuffered channel)是指在接收前没有能力保存任何值的通道,在之前的例子中使用的都是无缓冲通道,需要注意的是,对于无缓冲通道而言,不管是往通道里写数据还是从通道里读数据,都 ...

  2. Golang的goroutine协程和channel通道

    一:简介 因为并发程序要考虑很多的细节,以保证对共享变量的正确访问,使得并发编程在很多情况下变得很复杂.但是Go语言在开发并发时,是比较简洁的.它通过channel来传递数据.数据竞争这个问题在gol ...

  3. go语言之行--golang核武器goroutine调度原理、channel详解

    一.goroutine简介 goroutine是go语言中最为NB的设计,也是其魅力所在,goroutine的本质是协程,是实现并行计算的核心.goroutine使用方式非常的简单,只需使用go关键字 ...

  4. 实时事件统计项目:优化flume:用file channel代替mem channel

    背景:利用kafka+flume+morphline+solr做实时统计. solr从12月23号开始一直没有数据.查看日志发现,因为有一个同事加了一条格式错误的埋点数据,导致大量error. 据推断 ...

  5. netty源码解解析(4.0)-12 Channel NIO实现:channel初始化

    创建一个channel实例,并把它register到eventLoopGroup中之后,这个channel然后处于inactive状态,仍然是不可用的.只有在bind或connect方法调用成功之后才 ...

  6. 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 ...

  7. Fibre Channel和Fiber Channel

    Fibre Channel也就是"网状通道"的意思,简称FC.   由于Fiber和Fibre只有一字之差,所以产生了很多流传的误解. FC只代表Fibre Channel,而不是 ...

  8. golang channel 用法转的

    一.Golang并发基础理论 Golang在并发设计方面参考了C.A.R Hoare的CSP,即Communicating Sequential Processes并发模型理论.但就像John Gra ...

  9. 深入学习golang(2)—channel

    Channel 1. 概述 “网络,并发”是Go语言的两大feature.Go语言号称“互联网的C语言”,与使用传统的C语言相比,写一个Server所使用的代码更少,也更简单.写一个Server除了网 ...

随机推荐

  1. Linux-C实现GPRS模块发送短信

    “GSM模块,是将GSM射频芯片.基带处理芯片.存储器.功放器件等集成在一块线路板上,具有独立的操作系统.GSM射频处理.基带处理并提供标准接口的功能模块.GSM模块根据其提供的数据传输速率又可以分为 ...

  2. ios 耳机插入拔出检测

    [AVAudioSession sharedInstance]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@se ...

  3. 4、css盒模型和文本溢出

    4:css盒模型和文本溢出 学习目标 1.认识盒子模型 2.盒子模型的组成部分 3.学习盒子模型的相关元素 margin padding 4.文本溢出相关的属性 一.css属性和属性值的定义 盒模型是 ...

  4. 怎么给button设置背景颜色?【Android】

    怎么给button设置背景颜色?[Android] 怎么给button设置背景颜色?[Android] 现在我想给按钮添加背景颜色,怎么做 1.android:background="@an ...

  5. Luogu P3919【模板】可持久化数组(可持久化线段树/平衡树)

    题面:[模板]可持久化数组(可持久化线段树/平衡树) 不知道说啥,总之我挺喜欢自己打的板子的! #include<cstdio> #include<cstring> #incl ...

  6. 对类型“Func<,>”的引用声称该类型是在“mscorlib”中定义的,但未能找到

    报错 右击→属性

  7. 区块链共识机制:POW、POS、DPOS、PBFT、POOL

    共识机制作为区块链的关键技术之一,在业务吞吐量.交易速度.不可篡改性.准入门槛等等方面发挥重要的作用. 区块链是去中心化的,没有中心记账节点,所以需要全网对账本达成共识.目前有POW.POS.DPOS ...

  8. mysq数据库基本操作

    MySQL的数据库名称,表名称是区分大小写,MySQL 的SQL keywords不区分大小写: if when you attempt to log in, you get an error mes ...

  9. Java中抽象类和抽象方法的区别

    抽象方法:在类中没有方法体的方法,就是抽象方法. 抽象类:含有抽象方法的类就叫抽象类. 抽象类中的抽象方法必须被实现! 如果一个子类没有实现父类中的抽象方法,则子类也成为了一个抽象类! 抽象类中的普通 ...

  10. linux学习:【第1篇】初识Linux及安装

    狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! linux学习:[第1篇]初识Linux及安装 写在前面 学习之初看了一段文章,很有感触,所以也 ...