单向channel:

单向通道可分为发送通道和接收通道。但是无论哪一种单向通道,都不应该出现在变量的声明中,假如初始化了这样一个变量

var uselessChan chan <- int =make(chan <- int,10)

这样一个变量该如何使用呢,这样一个只进不出的通道没有什么实际意义。那么这种单向通道的应用场景在什么地方呢。我们可以用这种变换来约束对通道的使用方式。比如下面的这种声明

func Notify(c chan <- os.Signal, sig… os.Signal)

该函数的第一个参数的类型是发送通道类型。从参数的声明来看,调用它的程序应该传入一个只能发送而不能接收的通道。但是实际上应该传入的双向通道,Go会依据该参数的声明,自动把它转换成一个单向通道。Notify函数中的代码只能向通道c发送数据,而不能从它那里接收数据,在该函数中从通道c接收数据会导致编译错误。但是在函数之外不存在这个约束。

现在对SignalNotifier接口的声明稍作改变,如下:

type SignalNotifier interface{

Notify(sig…os.Signal) <- chan os.Signal

}

现在这个声明放在了函数外面,这种实现方法说明Notify方法的调用只能从作为结果的通道中接收元素值,而不能向其发送元素值。来看一个单向通道的例子

var strChan=make(chan string,3)

var mapChan=make(chan map[string]int,1)

func receive(strChan <- chan string,syncChan1 <- chan struct{},syncChan2 chan <- struct{}){

<-syncChan1

fmt.Println("Received a sync signal and wait a second...[receiver]")

time.Sleep(time.Second)

for{

if elem,ok:=<-strChan;ok{

fmt.Println("Received:",elem,"[receiver]")

}else{

break

}

}

fmt.Println("stopped.[receiver]")

syncChan2 <- struct{}{}

}

func send(strChan chan <- string,syncChan1 chan <- struct{},syncChan2 chan <- struct{}){

for _,elem:=range[]string{"a","b","c","d","e"}{

strChan <- elem

fmt.Println("sent:",elem,"[sender]")

if elem == "c"{

syncChan1 <- struct{}{}

fmt.Println("sent a sync signal.[sender]")

}

}

fmt.Println("wait 2 seconds...[sender]")

time.Sleep(time.Second*2)

close(strChan)

syncChan2 <- struct{}{}

}

func main(){

syncChan1:=make(chan struct{},1)

syncChan2:=make(chan struct{},2)

go receive(strChan,syncChan1,syncChan2)

go send(strChan,syncChan1,syncChan2)

<-syncChan2

<-syncChan2

}

receive函数只能对strChan和syncChan1通道进行接收操作。而send函数只能对这2个通道进行发送操作。区别点在于chan 和 <-的位置。chan <- 表明是接收通道。<- chan表明是发送通道。运行结果如下:

sent: a [sender]

sent: b [sender]

sent: c [sender]

sent a sync signal.[sender]

Received a sync signal and wait a second...[receiver]

sent: d [sender]

Received: a [receiver]

Received: b [receiver]

Received: c [receiver]

Received: d [receiver]

Received: e [receiver]

sent: e [sender]

wait 2 seconds...[sender]

stopped.[receiver]

非缓冲channel

如果在初始化一个通道时将其容量设置为0或者直接忽略对容量的设置。就会使该通道变成一个非缓冲通道。和异步的方式不同,非缓冲通道只能同步的传递元素值

1 向此类通道发送元素值的操作会被阻塞。直到至少有一个针对通道的接收操作进行为止。该接收操作会首先得到元素的副本,然后再唤醒发送方所在的goroutine之后返回。也就是说,这是的接收操作会在对应的发送操作完成之前完成。

2 从此类通道接收元素值的操作会被阻塞,直到至少有一个针对该通道的发送操作进行为止。发送操作会直接把元素值赋值给接收方,然后再唤醒接收方所在的goroutine之后返回。这时的发送操作会在对应的接收操作之前完成。

func main(){

sendingInterval:=time.Second

receptionInterval:=time.Second*2

intChan:=make(chan int,0)

go func(){

var ts0,ts1 int64

for i:=1;i<=5;i++{

intChan <- i

ts1=time.Now().Unix()

if ts0 == 0{

fmt.Println("sent:",i)

}else{

fmt.Println("Sent:",i,"[interval:",ts1-ts0,"]\n")

}

ts0=time.Now().Unix()

time.Sleep(sendingInterval)

}

close(intChan)

}()

var ts0,ts1 int64

Loop:

for{

select{

case v,ok:=<-intChan:

if !ok{

break Loop

}

ts1=time.Now().Unix()

if ts0 == 0{

fmt.Println("receive:",v)

}else{

fmt.Println("receive:",v,"[interval:",ts1-ts0,"]\n")

}

}

ts0=time.Now().Unix()

time.Sleep(receptionInterval)

}

fmt.Println("End.")

}

运行结果:

sent: 1

receive: 1

receive: 2 [interval: 2 ]

Sent: 2 [interval: 2 ]

Sent: 3 [interval: 2 ]

receive: 3 [interval: 2 ]

receive: 4 [interval: 2 ]

Sent: 4 [interval: 2 ]

receive: 5 [interval: 2 ]

Sent: 5 [interval: 2 ]

End.

可以看到发送操作和接收操作都与receptioninterval的间隔一致。如果把sendingInterval改成time.Second*4. 则结果如下:发送操作和接收操作都与sendingInterval的间隔一致

sent: 1

receive: 1

Sent: 2 [interval: 4 ]

receive: 2 [interval: 4 ]

Sent: 3 [interval: 4 ]

receive: 3 [interval: 4 ]

Sent: 4 [interval: 4 ]

receive: 4 [interval: 4 ]

Sent: 5 [interval: 4 ]

receive: 5 [interval: 4 ]

End.

go语言之并发编程 channel(1)的更多相关文章

  1. go语言之并发编程 channel

    前面介绍了goroutine的用法,如果有多个goroutine的话相互之间是如何传递数据和通信的呢.在C语言或者JAVA中,传输的方法包括共享内存,管道,信号.而在Go语言中,有了更方便的方法,就是 ...

  2. Go语言 7 并发编程

    文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 今天我们学习Go语言编程的第七章,并发编程.语言级别 ...

  3. 十.Go并发编程--channel使用

    一.设计原理 Go 语言中最常见的.也是经常被人提及的设计模式就是: "不要通过共享内存来通信,我们应该使用通信来共享内存" 通过共享内存来通信是直接读取内存的数据,而通过通信来共 ...

  4. Go语言之并发编程(二)

    通道(channel) 单纯地将函数并发执行是没有意义的.函数与函数间需要交换数据才能体现并发执行函数的意义.虽然可以使用共享内存进行数据交换,但是共享内存在不同的goroutine中容易发生竞态问题 ...

  5. Go语言之并发编程(一)

    轻量级线程(goroutine) 在编写socket网络程序时,需要提前准备一个线程池为每一个socket的收发包分配一个线程.开发人员需要在线程数量和CPU数量间建立一个对应关系,以保证每个任务能及 ...

  6. Go语言之并发编程(四)

    同步 Go 程序可以使用通道进行多个 goroutine 间的数据交换,但这仅仅是数据同步中的一种方法.通道内部的实现依然使用了各种锁,因此优雅代码的代价是性能.在某些轻量级的场合,原子访问(atom ...

  7. Go语言之并发编程(三)

    Telnet回音服务器 Telnet协议是TCP/IP协议族中的一种.它允许用户(Telnet客户端)通过一个协商过程与一个远程设备进行通信.本例将使用一部分Telnet协议与服务器进行通信. 服务器 ...

  8. go语言之并发编程同步一

    前面介绍了采用go语法的并行操作以及channel.既然是并行操作,那么就涉及到数据原子性以及同步的问题.所以在Go里面也需要采用同步的机制. 互斥锁: 由标准库代码包sync中的Mutex结构体类型 ...

  9. go语言入门(10)并发编程

    1,概述 1.1并发和并行 并行(parallel):指在同一时刻,有多条指令在多个处理器上同时执行. 并发(concurrency):指在同一时刻只能有一条指令执行,但多个进程指令被快速的轮换执行, ...

随机推荐

  1. MockServer的测试思想与实现

    转载:http://blog.csdn.net/shen1936/article/details/50298901 背景 什么是MOCK Mock的定义 Mock框架简介 Mock在单测中的应用 De ...

  2. mac os x+paralles使用source insight

    将Mac OS X下的目录共享到Paralles后,source insight创建工程.但是当再次打开时却打开失败.提示:there was an error opening project 网上对 ...

  3. iPhone手机解锁效果&&自定义滚动条&&拖拽--Clone&&窗口拖拽(改变大小/最小化/最大化/还原/关闭)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. How to Configure an SSIS Package to Access a Web Service using WCF

    This information is from:http://blogs.msdn.com/b/dbrowne/archive/2010/07/08/how-to-configure-an-ssis ...

  5. 标准库Allocator(三)uninitialized_fill等函数的实现

    前面我们使用了uninitialized_fill,来批量初始化某一段内存. 下面提供三个函数的实现代码,这三个代码的共同点是: 1.遇到错误,抛出异常 2.出现异常时,把之前构造的对象全部销毁 所以 ...

  6. JavaScript对象this指向(普通键this指向 非指向函数的键)

    1.结论 JavaScript对象普通键(非指向函数的键)this指向是window. 2.示例 <!DOCTYPE html> <html lang="zh"& ...

  7. 【BIEE】06_UNION /UNION ALL集合中分类汇总求和占比字段特殊处理

    环境准备 基于[BIEE]04..中建立的事实表 通过UNION ALL后得到如下报表: 优秀员工薪水公式:CASE WHEN "EMP_FACT"."级别"= ...

  8. 命令行添加pod示例

    1.创建AlamFireDemo 工程,关闭工程 2.进入到工程目录 执行 pod init 命令 生成 PodFile文件 3.vi PodFile编辑该文件 启用:platform :ios, ' ...

  9. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  10. 用unity3d实现简单的主server连接

    用unity3d实现简单的主server连接 參考自鹰大的网络实例 -------------------------------------------------华丽的切割线----------- ...