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. JavaのEclipse安装Tomcat插件

    由于我装的eclipse是SE版,所以没有Servlet项目,需要自己安装插件. 一:首先到Tomcat的官网下载对应Tomcat版本. http://tomcat.apache.org/ 现在tom ...

  2. rar压缩find查找到的文件

    find . -name 'CMakeLists.txt' | xargs /d/Program\ Files/WinRAR/rar.exe a -r ./out.rar $ ------------ ...

  3. Fastjson 爆出远程代码执行高危漏洞,更新版本已修复

    fastjson近日曝出代码执行漏洞,恶意用户可利用此漏洞进行远程代码执行,入侵服务器,漏洞评级为“高危”. 基本介绍fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器 ...

  4. C++判断是否连接服务器

    BOOL CheckServerStatus::isConnectServer(CString serverName, int serverPort) { CString strURL; strURL ...

  5. maven项目启动报错:SLF4J: Class path contains multiple SLF4J bindings.

    SringBoot的Application启动报错: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding ...

  6. Could not write file: C:\......\.classpath

    最近因为换操作系统,把项目从Mac系统copy到了win10下,出现了不少项目部署启动上的问题.最开始的一个问题是:Could not write file: C:\......\.classpath ...

  7. 所生成项目的处理器架构“MSIL”与引用“***”的处理器架构“x86”不匹配。这种不匹配可能会导致运行时失败。请考虑通过配置管理器...

    警告:所生成项目的处理器架构“MSIL”与引用“***”的处理器架构“x86”不匹配.这种不匹配可能会导致运行时失败.请考虑通过配置管理器更改您的项目的目标处理器架构,以使您的项目与引用间的处理器架构 ...

  8. python中的os

    import sys, os print(__file__) # 绝对路径,实际是文件名 /Users/majianyu/Desktop/test/bin/bin.py print(os.path.a ...

  9. 线程同步-使用CountDownEvent类

    CountDownEvent类:信号类,等待直到一定数量的操作完成. 代码Demo: using System; using System.Threading; Main方法下面加入以下代码片段: p ...

  10. pycharm 下的djiango使用

    创建工程可以在虚拟环境下运行,创建工程后使用命令 在python 下的命令窗口(Terminal) python3 manage.py startapp django_web (或者 python3替 ...