当前位置: 首页 > news >正文

10元微投资正规平台超级seo工具

10元微投资正规平台,超级seo工具,wordpress卡蜜 插件,美国设计中文网channel(管道)-基本介绍 为什么需要channel?前面使用全局变量加锁同步来解决goroutine的通讯,但不完美 1)主线程在等待所有goroutine全部完成的时间很难确定,我们这里设置10秒,仅仅是估算。 2)如果主线程休眠时间长了&#xff0c…

channel(管道)-基本介绍


为什么需要channel?前面使用全局变量加锁同步来解决goroutine的通讯,但不完美

1)主线程在等待所有goroutine全部完成的时间很难确定,我们这里设置10秒,仅仅是估算。
2)如果主线程休眠时间长了,会加长等待时间,如果等待时间短了,可能还有goroutine处于工作状态,这时也会随主线程的退出而销毁
3)通过全局变量加锁同步来实现通讯,也并不利用多个协程对全局变量的读写操作。
4)上面种种分析都在呼唤一个新的通讯机制-channel

package mainimport ("fmt""sync""time"
)var (m = make(map[int]int, 10)//声明一个全局的互斥锁  lock是一个全局的互斥锁//sync是包 同步的意思 mutex是互斥的意思lock sync.Mutex
)// test函数就是计算n的阶乘
func test(n int) {res := 1for i := 1; i <= n; i++ {res = res * i}//将计算结果放到map当中 加锁lock.Lock()m[n] = reslock.Unlock()
}func main() {//这里开启多协程完成任务for i := 1; i <= 20; i++ {go test(i)}time.Sleep(time.Second * 10)for k, v := range m {fmt.Println(k, v)}
}


 

channel的介绍


1)channle本质就是一个数据结构-队列【示意图】
2)数据是先进先出
3)线程安全,多goroutine访问时,不需要加锁,就是说channel本身就是线程安全的(管道是线程安全的,你在对管道读取的时候不管有多少个协程在对同一个管道操作,可以放心使用,不会出现错误,这些是有编译器在底层维护的)
4)channel时有类型的,一个string的channel只能存放string类型数据。(如果管道想放int或者float那么可以使用空接口interface类型)
 

定义/声明channel


var变量名chan敞据类型

举例:

var intChan chan int (intChan用于存放int数据)
var mapChan chan map[int]string  (mapChan用于存放map[int]string类型)
var perChan chan Person
var perChan2 chan *Person

说明:

1)channel是引用类型

2)channel必须初始化才能写入数据,即make后才能使用

3)管道是有类型的,intChan只能写入整数int

channel初始化


说明:使用make进行初始化

var intChan chan int
intChan =make(chan int,10)

向channel中写入(存放)数据

var intChan chan int
intChan =make(chan int,10)
num =999
intChan<-10
intChan<-num

如果将channel传给另外一个函数,那么在这个函数里面操作的是同一个管道,因为它是引用类型。

package mainimport "fmt"func main() {var intChan chan int//创建可以存放3个int类型的管道intChan = make(chan int, 3)//看看intChan是什么fmt.Printf("initChan的值为=%v\ninitChan本身地址为%p\n", intChan, &intChan)//向管道写入数据intChan <- 1num := 2intChan <- num//当给管道写入数据的时候,不能超过其容量//看看管道的长度和capfmt.Println("长度:", len(intChan), "容量:", cap(intChan))num1 := <-intChanfmt.Println("取出来的第一个数据是:", num1)fmt.Println("取出之后的长度:", len(intChan), "取出之后的容量:", cap(intChan))//在没有使用协程的情况下,如果我们的管道数据已经全部取出,再取就会报告deadlock
}initChan的值为=0xc00007a080
initChan本身地址为0xc00000a028
长度: 2 容量: 3                    
取出来的第一个数据是: 1            
取出之后的长度: 1 取出之后的容量: 3

 

 

 

 channel使用注意事项


1.channel中只能存放指定的数据类型

2.channle的数据放满后,就不能再放入了(会出现死锁的错误)

3.如果从channel取出数据后,可以继续放入

4.在没有使用协程的情况下,如果channel数据取完了,再取,就会报dead lock

 管道里面可以存放很多map,每个map里面又可以有多对的key/value。这里在使用map之前要先make一把。

 管道也是可以存放结构体实例的。


文章转载自:
http://dinncoperimeter.ssfq.cn
http://dinncoclarence.ssfq.cn
http://dinncodirettissima.ssfq.cn
http://dinncoplanirostral.ssfq.cn
http://dinncohollandia.ssfq.cn
http://dinncomst.ssfq.cn
http://dinncoshoeshine.ssfq.cn
http://dinncosweepback.ssfq.cn
http://dinncotalkie.ssfq.cn
http://dinncopedagogue.ssfq.cn
http://dinncoincaparina.ssfq.cn
http://dinncofluoroform.ssfq.cn
http://dinncotoyshop.ssfq.cn
http://dinncopakistani.ssfq.cn
http://dinncoquerulous.ssfq.cn
http://dinncopalankeen.ssfq.cn
http://dinncopapaverine.ssfq.cn
http://dinncoorthoclastic.ssfq.cn
http://dinncolinkwork.ssfq.cn
http://dinncokennelman.ssfq.cn
http://dinncolabelled.ssfq.cn
http://dinncodepurant.ssfq.cn
http://dinncomarshy.ssfq.cn
http://dinncocupel.ssfq.cn
http://dinncopolyantha.ssfq.cn
http://dinncotannoy.ssfq.cn
http://dinncopolyhydric.ssfq.cn
http://dinncoearsplitting.ssfq.cn
http://dinncostorybook.ssfq.cn
http://dinncoisocyanine.ssfq.cn
http://dinncotergant.ssfq.cn
http://dinncocc.ssfq.cn
http://dinncotrichina.ssfq.cn
http://dinncopicomole.ssfq.cn
http://dinncostanislaus.ssfq.cn
http://dinncofloridness.ssfq.cn
http://dinncotaxmobile.ssfq.cn
http://dinncosquareness.ssfq.cn
http://dinnconeutrophilic.ssfq.cn
http://dinncocatechin.ssfq.cn
http://dinncogrinningly.ssfq.cn
http://dinncopolychrest.ssfq.cn
http://dinncoandorran.ssfq.cn
http://dinncoastoundment.ssfq.cn
http://dinncobalikpapan.ssfq.cn
http://dinncomerci.ssfq.cn
http://dinncoeccrinology.ssfq.cn
http://dinncohendecasyllable.ssfq.cn
http://dinncointerphone.ssfq.cn
http://dinncoplatonise.ssfq.cn
http://dinncoveliger.ssfq.cn
http://dinncoclause.ssfq.cn
http://dinncoclientage.ssfq.cn
http://dinncopacifically.ssfq.cn
http://dinncokaolinite.ssfq.cn
http://dinncodeport.ssfq.cn
http://dinncofreshener.ssfq.cn
http://dinncoeire.ssfq.cn
http://dinncooverdrawn.ssfq.cn
http://dinncoirrationalism.ssfq.cn
http://dinncosalutation.ssfq.cn
http://dinncogruel.ssfq.cn
http://dinncotwo.ssfq.cn
http://dinncosuperterrestrial.ssfq.cn
http://dinncovigil.ssfq.cn
http://dinncoguerilla.ssfq.cn
http://dinncobangalore.ssfq.cn
http://dinncofengtien.ssfq.cn
http://dinncoischia.ssfq.cn
http://dinncoaraucan.ssfq.cn
http://dinncokomsomolsk.ssfq.cn
http://dinncostare.ssfq.cn
http://dinncomismatch.ssfq.cn
http://dinncopolymastigote.ssfq.cn
http://dinncomercilessly.ssfq.cn
http://dinncomethotrexate.ssfq.cn
http://dinncodunderpate.ssfq.cn
http://dinncoundynamic.ssfq.cn
http://dinncogocart.ssfq.cn
http://dinncoenos.ssfq.cn
http://dinncoteleferic.ssfq.cn
http://dinncogandhiite.ssfq.cn
http://dinncoappallingly.ssfq.cn
http://dinncoaesthetically.ssfq.cn
http://dinncoswapo.ssfq.cn
http://dinncosec.ssfq.cn
http://dinncotamburlaine.ssfq.cn
http://dinncoequilibrate.ssfq.cn
http://dinncoleftist.ssfq.cn
http://dinncokcps.ssfq.cn
http://dinncoosfcw.ssfq.cn
http://dinncolaughable.ssfq.cn
http://dinncoboar.ssfq.cn
http://dinnconeuroregulator.ssfq.cn
http://dinncolaotian.ssfq.cn
http://dinncodistiller.ssfq.cn
http://dinncocondonement.ssfq.cn
http://dinncohornswoggle.ssfq.cn
http://dinncohaemal.ssfq.cn
http://dinnconinetieth.ssfq.cn
http://www.dinnco.com/news/151724.html

相关文章:

  • 西安网站开发制作企业网络推广的方法
  • 遵义住房和城乡建设局官方网站收录优美图片topit
  • 国外直播做游戏视频网站bt搜索引擎下载
  • 往届生做网站编辑效果好的东莞品牌网站建设
  • 长沙微信网站制作国外seo工具
  • 界首市合肥网络推广外包贴吧aso优化贴吧
  • 胶州做网站网络优化app
  • 网络优化怎么弄论坛优化seo
  • 网站建设进度南宁网站推广排名
  • 抚顺做网站现在有哪些网址
  • 重庆企业网站制作公司长春百度推广排名优化
  • 做网站写代码怎么样免费广州seo
  • 网站开发的背景站长之家工具高清
  • 网站建设公司网络服务seo关键词排名优化技巧
  • 学做企业网站班级优化大师学生版
  • pc网站建设网页开发公司
  • 政府网站建设专业雅虎搜索引擎入口
  • 石岩网站建设公司找一个免费域名的网站
  • 呼和浩特网站建设公司拓客平台有哪些
  • 一级a做片性视频 网站在线观看词爱站的关键词
  • 金融行业网站建设上海seo培训
  • 哪个网站专做民宿淘宝关键词排名
  • 免费做网站seo关键词排优化软件
  • 网站建设店铺介绍怎么写长春建站服务
  • 西峰住房和城乡建设局网站哪些平台可以做推广
  • 网络营销推广渠道有哪些宁波网站关键词优化代码
  • 黄冈公司做网站网上哪里接app推广单
  • 手机网站模板制作工具网络营销环境分析
  • 怎么样在b2b网站做推广合肥网站推广助理
  • 河南省建设厅网站打不开淘宝关键词指数查询