博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
结构体、通道、并发实现生产者消费者
阅读量:4101 次
发布时间:2019-05-25

本文共 1652 字,大约阅读时间需要 5 分钟。

要求:利用生产者消费者实现对一个随机数所有位求和(体现结构体与通道)

实现:

item结构体存储id与一个随机数,itemChan作为通道存储item结构体

result结构体存储item指针与对item的num随机数所有位求和的结果,resultChan作为通道存储result结构体

启动两个生产者,利用函数实现自定义启动n个消费者,关键是最后的printResult函数,与通道交流时同时阻塞了main,防止main的结束迫使所有goroutine结束。

package mainimport (	"fmt"	"math/rand")var itemChan chan *item     //存放待求和结构体的通道var resultChan chan *result //存放求和结果结构体的通道//待求和结构体type item struct {	id  int64	num int64}//求和结果结构体type result struct {	item *item	sum  int64}//生产者,初始化待求和结构体,存入*item类型通道func producer(ch chan *item) {	//1.生成随机数	var id int64	for {		id++		number := rand.Int63() //int64正整数		tmp := &item{			id:  id,			num: number,		}		//2.把随机数所在结构体发送到通道		ch <- tmp	}}//计算一个数字所有位之和func calc(num int64) int64 {	//将num模10后再除以10	// 123%10=12...3	// 12%10=1...2	// 1%10=0...1	var sum int64	for num > 0 {		sum += num % 10		num = num / 10	}	return sum}//消费者,从*item类型通道取值处理,利用结果来初始化求和结果结构体,存入*result类型通道func consumer(itemCh chan *item, resultChan chan *result) {	for tmp := range itemCh {		sum := calc(tmp.num)		//构造结果结构体result		rstObj := &result{			item: tmp,			sum:  sum,		}		resultChan <- rstObj	}}//go运行多个消费者func startMultiConsumer(n int, itemCh chan *item, rstChan chan *result) {	for i := 0; i < n; i++ {		go consumer(itemCh, rstChan)	}}//打印求和结果通道里的数据func printResult(rstChan chan *result) {	for rst := range rstChan {		fmt.Printf("id:%v, num:%v, sum:%v\n", rst.item.id, rst.item.num, rst.sum)	}}func main() {	itemChan = make(chan *item, 1000)	resultChan = make(chan *result, 10)	go producer(itemChan)	go producer(itemChan)	//开启50个消费者	startMultiConsumer(50, itemChan, resultChan)	//打印结果,顺便阻塞main,让其他goroutine完成工作。否则main结束,其他goroutine就被强行结束	printResult(resultChan)}

 

转载地址:http://szwsi.baihongyu.com/

你可能感兴趣的文章
程序设计方法概述:从面相对象到面向功能到面向对象
查看>>
SQL join
查看>>
JavaScript实现页面无刷新让时间走动
查看>>
CSS实例:Tab选项卡效果
查看>>
前端设计之特效表单
查看>>
前端设计之CSS布局:上中下三栏自适应高度CSS布局
查看>>
Java的时间操作玩法实例若干
查看>>
JavaScript:时间日期格式验证大全
查看>>
责任链模式 Chain of Responsibility
查看>>
高并发与大数据解决方案概述
查看>>
解决SimpleDateFormat线程安全问题NumberFormatException: multiple points
查看>>
MySQL数据库存储引擎简介
查看>>
处理Maven本地仓库.lastUpdated文件
查看>>
CentOS7,玩转samba服务,基于身份验证的共享
查看>>
计算机网络-网络协议模型
查看>>
计算机网络-OSI各层概述
查看>>
Java--String/StringBuffer/StringBuilder区别
查看>>
分布式之redis复习精讲
查看>>
数据结构与算法7-栈
查看>>
Java并发编程 | 一不小心就死锁了,怎么办?
查看>>