1、未初始化的channel读,阻塞

package main

import (
"fmt"
"time"
) func main() {
var ch chan int
go check(ch) fmt.Println("main runtime end")
time.Sleep(time.Second * 1000)
} func check(ch chan int) {
select {
case i := <-ch:
fmt.Println("read ch data=", i)
}
fmt.Println("check runtime exit")
}

2、未初始化的channel写,阻塞

package main

import (
"fmt"
"time"
) func main() {
go func() {
for {
time.Sleep(time.Second * 1)
}
}()
var ch chan int
go check(ch)
time.Sleep(time.Second * 1)
fmt.Println("xxxxxxx")
<-ch
fmt.Println("main runtime end")
time.Sleep(time.Second * 1000)
} func check(ch chan int) {
defer func() {
if r := recover(); r != nil {
fmt.Println("recover ", r)
}
}()
ch <- 1
fmt.Println("check runtime exit")
}

3、向已关闭的channel读,返回默认值和false

package main

import (
"fmt"
"time"
) func main() {
var ch chan int
ch = make(chan int)
go check(ch) fmt.Println("main runtime end")
close(ch)
time.Sleep(time.Second * 1000)
} func check(ch chan int) {
select {
case i, k := <-ch:
fmt.Println("read ch data=", i, " k=", k) //k=false i=0
}
fmt.Println("check runtime exit")
}

4、向已关闭的channel写,panic

package main

import (
"fmt"
"time"
) func main() {
var ch chan int
ch = make(chan int)
close(ch)
ch <- 1
fmt.Println("end")
time.Sleep(time.Second * 1000)
}

5、time.Timer Stop后,time.Timer.C将阻塞

package main

import (
"fmt"
"time"
) func main() {
tm := time.NewTimer(time.Second * 3)
go check(tm)
time.Sleep(time.Second * 1)
tm.Stop()
fmt.Println("main runtime end")
time.Sleep(time.Second * 1000)
} func check(tm *time.Timer) {
select {
case i, k := <-tm.C: //阻塞
fmt.Println("read ch data=", i, " k=", k)
}
fmt.Println("check runtime exit")
}

6、无缓冲与有缓冲channel的重要区别,无缓冲的channel在写时必须有读携程,否则会阻塞。如下例子,超时后向exit发数据会阻塞,因为只有一个携程,此时没有其他携程对exit进行读。【踩了坑才理解深刻】

package main

import (
"fmt"
"time"
) func main() {
exit := make(chan int)
go check(exit)
time.Sleep(time.Second * 100)
} func check(exit chan int) {
tm := time.NewTimer(time.Second * 3)
select {
case <-exit:
fmt.Println("exit")
case <-tm.C:
fmt.Println("time out")
exit <- 1
fmt.Println("exit <- 1 ok")
}
fmt.Println("check runtime exit")
}

例子2:

package main

import (
"fmt"
"time"
) func main() {
go func() {
for {
time.Sleep(time.Second * 1)
} }()
exit := make(chan int, 1)
exit <- 1
fmt.Println("end")
}

这里会直接END,如果exit:=make(chan int),会阻塞在exit<-1

golang channel 总结的更多相关文章

  1. golang channel的使用以及调度原理

    golang channel的使用以及调度原理 为了并发的goroutines之间的通讯,golang使用了管道channel. 可以通过一个goroutines向channel发送数据,然后从另一个 ...

  2. golang channel关闭后,是否可以读取剩余的数据

    golang channel关闭后,其中剩余的数据,是可以继续读取的. 请看下面的测试例子. 创建一个带有缓冲的channel,向channel中发送数据,然后关闭channel,最后,从channe ...

  3. golang channel原理

    channel介绍 channel一个类型管道,通过它可以在goroutine之间发送和接收消息.它是Golang在语言层面提供的goroutine间的通信方式. 众所周知,Go依赖于称为CSP(Co ...

  4. golang channel 用法转的

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

  5. golang channel初次接触

    goroutine之间的同步 goroutine是golang中在语言级别实现的轻量级线程,仅仅利用go就能立刻起一个新线程.多线程会引入线程之间的同步问题,经典的同步问题如生产者-消费者问题,在c, ...

  6. 如何优雅的关闭Golang Channel?

    Channel关闭原则 不要在消费端关闭channel,不要在有多个并行的生产者时对channel执行关闭操作. 也就是说应该只在[唯一的或者最后唯一剩下]的生产者协程中关闭channel,来通知消费 ...

  7. golang channel几点总结

    golang提倡使用通讯来共享数据,而不是通过共享数据来通讯.channel就是golang这种方式的体现. Channel 在golang中有两种channel:带缓存的和不带缓存. 带缓存的cha ...

  8. golang channel 源码剖析

    channel 在 golang 中是一个非常重要的特性,它为我们提供了一个并发模型.对比锁,通过 chan 在多个 goroutine 之间完成数据交互,可以让代码更简洁.更容易实现.更不容易出错. ...

  9. golang channel本质——共享内存

    channel是golang中很重要的概念,配合goroutine是golang能够方便实现并发编程的关键.channel其实就是传统语言的阻塞消息队列,可以用来做不同goroutine之间的消息传递 ...

  10. Golang channel 用法简介

    channel 是 golang 里相当有趣的一个功能,大部分时候 channel 都是和 goroutine 一起配合使用.本文主要介绍 channel 的一些有趣的用法. 通道(channel), ...

随机推荐

  1. Eclipse错误:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    该报错是由于缺少servlet-api.jar造成的,将servlet-api.jar复制到项目下的WEB-INF/lib目录下即可 servlet-api.jar在tomcat的lib目录下有,可以 ...

  2. 把旧系统迁移到.Net Core 2.0 日记(7) Tag Helpers /ResponseCache

    Tag Helpers是Html Helpers的一种替换 比如,原来的视图模型定义是这样的: @using (Html.BeginForm("Register", "A ...

  3. 苹果手机 disabled 的背景颜色没有

    解决方案 .class disabled{ background-color: rgb(235, 235, 228); opacity:1}

  4. kiss word memory post poly peri out ~p 4

    1● post p əust 在后面,邮件     2● peri 多   3● poly 周围,靠近  

  5. securecrt远程管理工具连接VM虚拟机

    对桥接,net,host_only网络不理解的,请点击:桥接,net,host_only的区别 我这里以net连接为例: 我们使用securecrt实现net的连接,前提是保证虚拟机的ip和虚拟网卡V ...

  6. 熔断监控面板(Hystrix Dashboard)

    Hystrix Dashboard Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Comma ...

  7. Docker安装websphere(四)

    在Docker容器里安装webshpere <!--前提:已经安装好了docker,能够正常使用.--> (1)docker安装websphere(需要账号和密码登录,不挂载数据卷) 获取 ...

  8. C/C++知识补充(2) C/C++操作符/运算符的优先级 & 结合性

    , 逗号操作符 for( i = 0, j = 0; i < 10; i++, j++ ) ... 从左到右   Precedence Operator Description Example ...

  9. μC/OS-II在Microblaze上的移植与使用专题--“安富利杯”赛灵思FPGA设计技巧与应用创新博文大赛参赛作品

    reference:http://xilinx.eetrend.com/d6-xilinx/blog/2010-05/682.html   随着集成电路设计与制造技术的发展,FPGA芯片的容量越来越大 ...

  10. SpringMVC学习四(@ModelMap @RequestBody等等的说明)

    参考如下 http://www.cnblogs.com/HD/p/4107674.html http://www.cnblogs.com/qiankun-site/p/5774325.html @re ...