1、

fatal error: all goroutines are asleep - deadlock!

所有的协程都休眠了 - 死锁!

package main
import("fmt")
func f1(in chan int){
fmt.Println(<-in)
}
func main(){
out :=make(chan int)
out<-2
go f1(out)
}

package main
import("fmt")
func f1(in chan int){
fmt.Println(<-in)
}
func main(){
out :=make(chan int)
// out<-2
go f1(out)
out<-2
}

Understanding the context package in golang - Parikshit Agnihotry http://p.agnihotry.com/post/understanding_the_context_package_in_golang/

  package main

  import "fmt"

  //prints to stdout and puts an int on channel
func printHello(ch chan int) {
fmt.Println("Hello from printHello")
//send a value on channel
ch <- 2
} func main() {
//make a channel. You need to use the make function to create channels.
//channels can also be buffered where you can specify size. eg: ch := make(chan int, 2)
//that is out of the scope of this post.
ch := make(chan int)
//inline goroutine. Define a function and then call it.
//write on a channel when done
go func(){
fmt.Println("Hello inline")
//send a value on channel
ch <- 1
}()
//call a function as goroutine
go printHello(ch)
fmt.Println("Hello from main") //get first value from channel.
//and assign to a variable to use this value later
//here that is to print it
i := <- ch
fmt.Println("Recieved ",i)
//get the second value from channel
//do not assign it to a variable because we dont want to use that
<- ch
}

  

Hello from main
Hello from printHello
Recieved 2
Hello inline

golang channel 使用总结 - litang.me http://litang.me/post/golang-channel/

package main

import (
"fmt"
"time"
)

func fA(ch chan bool) {
defer func() {
fmt.Println("fA-defer")
}()
fmt.Println("fA-doing")
ch <- true
time.Sleep(time.Duration(123) * time.Millisecond)

}

func fB(ch chan bool) {
defer func() {
fmt.Println("fB-defer")
}()
fmt.Println("fB-doing")
ch <- true

}

func main() {
defer func() {
fmt.Println("main-defer")
}()
ch := make(chan bool)
go fA(ch)
go fB(ch)
<-ch
<-ch
}

目前打印结果为:
fB-doing
fB-defer
fA-doing
main-defer
修改代码打印结果为:
fB-doing
fB-defer
fA-doing
fA-defer
main-defer

交换

ch <- true
time.Sleep(time.Duration(123) * time.Millisecond)

位置

【练习】goroutine chan 通道 总结的更多相关文章

  1. go实例之轻量级线程goroutine、通道channel与select

    1.goroutine线程 goroutine是一个轻量级的执行线程.假设有一个函数调用f(s),要在goroutine中调用此函数,请使用go f(s). 这个新的goroutine将与调用同时执行 ...

  2. [系列] Go - chan 通道

    目录 概述 声明 chan 写入 chan 读取 chan 关闭 chan 示例 推荐阅读 概述 原来分享基础语法的时候,还未分享过 chan 通道,这次把它补上. chan 可以理解为队列,遵循先进 ...

  3. Go - chan 通道

    概述 原来分享的基础语法的时候,还未分享过 chan 通道,这次把它补上. chan 可以理解为队列,遵循先进先出的规则. 在说 chan 之前,咱们先说一下 go 关键字. 在 go 关键字后面加一 ...

  4. golang中为何在同一个goroutine中使用无缓冲通道会导致死锁

    package main import "fmt" func main() { /* 以下程序会导致死锁 c := make(chan int) c <- 10 n1 := ...

  5. [Go] golang无缓冲通道实现工作池控制并发

    展示如何使用无缓冲的通道创建一个goroutine池,控制并发频率1.无缓冲通道保证了两个goroutine之间的数据交换2.当所有的goroutine都忙的时候,能够及时通过通道告知调用者3.无缓冲 ...

  6. Golang教程:goroutine信道

    在上一篇教程中,我们讨论了如何使用协程实现并发.在这篇教程中,我们将讨论信道以及如何使用信道实现协程间通信. 什么是信道 信道(Channel)可以被认为是协程之间通信的管道.与水流从管道的一端流向另 ...

  7. go——通道

    相比Erlang,go并未实现严格的并发安全.允许全局变量.指针.引用类型这些非安全内存共享操作,就需要开发人员自行维护数据一致和完整性.Go鼓励使用CSP通道,以通信来代替内存共享,实现并发安全.作 ...

  8. go中的数据结构通道-channel

    1. channel的使用 很多文章介绍channel的时候都和并发揉在一起,这里我想把它当做一种数据结构来单独介绍它的实现原理. channel,通道.golang中用于数据传递的一种数据结构.是g ...

  9. Golang 入门 : channel(通道)

    笔者在<Golang 入门 : 竞争条件>一文中介绍了 Golang 并发编程中需要面对的竞争条件.本文我们就介绍如何使用 Golang 提供的 channel(通道) 消除竞争条件. C ...

随机推荐

  1. Vue2+Koa2+Typescript前后端框架教程--03后端路由和三层模式配置

    昨天将Koa2的基础框架和自动编译调试重启服务完成,今天开始配置路由和搭建基础的三层架构模式. 路由中间件:koa-router,即路由导航,就是我们平时使用最广泛的get/post方法执行的URL路 ...

  2. ESP8266系列图片外观 Wi-Fi模块一共有01~14十多款模块

  3. 「译」 .NET 5 新增的Http, Sockets, DNS 和 TLS 遥测

    .NET 一直在稳定的增加和改善对应用程序进行跨平台的诊断分析,在.NET Core 3.0, 我们看到了 EventCounters 的介绍,用于观察和分析指标测量. 我最近在几个 .NET Cor ...

  4. JVM--理解介绍

    JVM?JDK?JRE?关系? JDK(Java Development Kit),它是实际上存在的,它包含JRE+编译.运行等开发工具. JRE(Java Runtime Environment), ...

  5. 地图开发笔记(一):百度地图介绍、使用和Qt内嵌地图Demo

    前言   Qt在地图方面的研发.   百度地图 介绍   百度的地图分为多个开发,都是在线的(离线的需要自己提取,本篇解说在线地图).  百度地图JavaScript API支持HTTP和HTTPS, ...

  6. 在md里画流程图

    可以使用名为mermaid的代码块,即 ```mermaid``` 需要md解析器能解析mermaid mermaid使用详情参见

  7. 细说 js 的7种继承方式

    在这之前,先搞清楚下面这个问题: function Father(){} Father.prototype.name = 'father'; Father.prototype.children = [ ...

  8. 【C++】《C++ Primer 》第五章

    第五章 语句 一.简单语句 表达式语句:一个表达式末尾加上分号,就变成了表达式语句. 空语句:只有一个单独的分号,记得注释说明提高代码可读性. 复合语句(块):用花括号 {}包裹起来的语句和声明的序列 ...

  9. TypeScript接口与类的使用

    一.TypeScript接口 Interfaces 可以约定一个对象的结构 一个对象去实现一个接口 就必须拥有这个接口中所有的成员用interface定义接口, 并且定义接口中成员的类型 编译之后会发 ...

  10. git文件操作

    git下载地址: https://git-scm.com/download mac 直接使用brew下载brew install git 1Git一般工作流程: 1.在工作目录创建版本库 2.在工作目 ...