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

郑州餐饮网站建设公司排名今天发生的重大新闻事件

郑州餐饮网站建设公司排名,今天发生的重大新闻事件,深圳网站建设nooqi.cn,网站怎么做数据接口第47天的学习:并发进阶——深入了解Go语言的并发模型! 目录 Go并发模型简介Goroutines深度讲解Channels的进阶使用Select语句详解并发模型设计模式实战案例分析常见问题与解决方案 1. Go并发模型简介 Go语言以其内置的并发支持而闻名。通过轻量级的g…

第47天的学习:并发进阶——深入了解Go语言的并发模型!

目录

  1. Go并发模型简介
  2. Goroutines深度讲解
  3. Channels的进阶使用
  4. Select语句详解
  5. 并发模型设计模式
  6. 实战案例分析
  7. 常见问题与解决方案

1. Go并发模型简介

Go语言以其内置的并发支持而闻名。通过轻量级的goroutine和强大的channel,Go提供了一种易于使用且高效的并发编程方法。

并发与并行的区别:

  • 并发:处理多件事情的能力,但不一定同时。
  • 并行:同一时刻处理多件事情。

2. Goroutines深度讲解

Goroutine是Go语言的基本单位,它比传统线程更轻量。

创建Goroutine
package mainimport ("fmt""time"
)func say(s string) {for i := 0; i < 5; i++ {fmt.Println(s)time.Sleep(100 * time.Millisecond)}
}func main() {go say("world")say("hello")
}

运行流程图

main()├─ goroutine A : say("world")└─ goroutine B : say("hello")
Goroutine的特点
  • 启动goroutine使用 go 关键字。
  • 不阻塞当前程序的运行。
  • 实际调度由Go运行时处理。

3. Channels的进阶使用

Channels用于goroutines之间的通信。它们是类型安全的管道。

Channel的基本操作
package mainimport ("fmt"
)func sum(s []int, c chan int) {sum := 0for _, v := range s {sum += v}c <- sum
}func main() {s := []int{7, 2, 8, -9, 4, 0}c := make(chan int)go sum(s[:len(s)/2], c)go sum(s[len(s)/2:], c)x, y := <-c, <-cfmt.Println(x, y, x+y)
}
Channel类型
  • 无缓冲Channel:通信是同步的。
  • 缓冲Channel:可以异步通信。
市场管理员求和的例子
  • **设想场景:**市场末端有多个传感器会自动将商品数量推送到中央系统,由系统统计总和。
操作解释
创建Channelc := make(chan int)
发送数据c <- x(在goroutine中执行)
接收数据x := <-c

4. Select语句详解

select 语句类似于 switch ,但用于Channels操作。

package mainimport ("fmt""time"
)func fibonacci(c, quit chan int) {x, y := 0, 1for {select {case c <- x:x, y = y, x+ycase <-quit:fmt.Println("quit")return}}
}func main() {c := make(chan int)quit := make(chan int)go func() {for i := 0; i < 10; i++ {fmt.Println(<-c)}quit <- 0}()fibonacci(c, quit)
}

作用:

  • 多路复用:监听多个Channel。
  • 处理超时:结合time.After实现超时控制。

5. 并发模型设计模式

工作池模型

用于限制同时运行的goroutines数目。

package mainimport ("fmt""time"
)func worker(id int, jobs <-chan int, results chan<- int) {for j := range jobs {fmt.Printf("worker %d started job %d\n", id, j)time.Sleep(time.Second)fmt.Printf("worker %d finished job %d\n", id, j)results <- j * 2}
}func main() {const numJobs = 5jobs := make(chan int, numJobs)results := make(chan int, numJobs)for w := 1; w <= 3; w++ {go worker(w, jobs, results)}for j := 1; j <= numJobs; j++ {jobs <- j}close(jobs)for a := 1; a <= numJobs; a++ {<-results}
}
Pipeline模式

用于串联多个处理阶段。

package mainimport ("fmt"
)func gen(nums ...int) <-chan int {out := make(chan int)go func() {for _, n := range nums {out <- n}close(out)}()return out
}func sq(in <-chan int) <-chan int {out := make(chan int)go func() {for n := range in {out <- n * n}close(out)}()return out
}func main() {c := gen(2, 3, 4)out := sq(c)for n := range out {fmt.Println(n)}
}

6. 实战案例分析

为了进一步巩固理解,我们来看一个具体的并发应用示例。

案例:并发Web爬虫
  • 目标:使用并发从多个URL抓取页面标题。
package mainimport ("fmt""net/http""io/ioutil""regexp""time"
)func fetch(url string, ch chan<- string) {start := time.Now()resp, err := http.Get(url)if err != nil {ch <- fmt.Sprintf("Error: %s", err)return}defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)if err != nil {ch <- fmt.Sprintf("Error reading body: %s", err)return}re := regexp.MustCompile("<title>(.*?)</title>")matches := re.FindStringSubmatch(string(body))title := "No title found"if len(matches) > 1 {title = matches[1]}secs := time.Since(start).Seconds()ch <- fmt.Sprintf("%.2f seconds: %s", secs, title)
}func main() {urls := []string{"https://golang.org","https://godoc.org","https://gopl.io","https://play.golang.org",}ch := make(chan string)for _, url := range urls {go fetch(url, ch)}for range urls {fmt.Println(<-ch)}
}

7. 常见问题与解决方案

在学习并发时,你可能会遇到以下问题:

死锁问题
  • 原因:两个goroutine相互等待对方释放资源。
  • 解决方法:确保总是有一个goroutine能继续推进。
资源竞争
  • 原因:多个goroutine试图同时访问同一个资源。
  • 解决方法:使用channel同步,或者使用sync.Mutex
Goroutine泄漏
  • 原因:goroutine等待无法到达的事件。
  • 解决方法:确保所有channels都能正确关闭。

总结

今天我们深入探讨了Go语言的并发模型。理解如何有效地创建和管理Goroutines和Channels是写出高效并发程序的关键。通过示例代码和设计模式,你学会了如何利用Go的并发特性来解决复杂的问题。


怎么样今天的内容还满意吗?再次感谢观众老爷的观看,关注GZH:凡人的AI工具箱,回复666,送您价值199的AI大礼包。最后,祝您早日实现财务自由,还请给个赞,谢谢!


文章转载自:
http://dinncosupercluster.stkw.cn
http://dinncoprognose.stkw.cn
http://dinncokinematic.stkw.cn
http://dinncothyroxin.stkw.cn
http://dinncounexplainable.stkw.cn
http://dinncoexacta.stkw.cn
http://dinncodithyramb.stkw.cn
http://dinncorepetend.stkw.cn
http://dinncofishworks.stkw.cn
http://dinncosubplate.stkw.cn
http://dinncoelectropolar.stkw.cn
http://dinncosurmisable.stkw.cn
http://dinncohafnium.stkw.cn
http://dinncognomist.stkw.cn
http://dinncoadulterant.stkw.cn
http://dinncoautoindex.stkw.cn
http://dinncosquashy.stkw.cn
http://dinncosilversmith.stkw.cn
http://dinnconae.stkw.cn
http://dinncocelebrator.stkw.cn
http://dinncoharmonious.stkw.cn
http://dinncoatomy.stkw.cn
http://dinncosoundrec.stkw.cn
http://dinncobagging.stkw.cn
http://dinncozapatismo.stkw.cn
http://dinncouniface.stkw.cn
http://dinncoplasmapause.stkw.cn
http://dinncocircumscription.stkw.cn
http://dinncocircumvolute.stkw.cn
http://dinncosemitropical.stkw.cn
http://dinncoultimate.stkw.cn
http://dinncoexilian.stkw.cn
http://dinncoracegoer.stkw.cn
http://dinncoelusive.stkw.cn
http://dinncovirtu.stkw.cn
http://dinncodholl.stkw.cn
http://dinncosodomist.stkw.cn
http://dinncobonehead.stkw.cn
http://dinncoveritably.stkw.cn
http://dinncohora.stkw.cn
http://dinncomolasses.stkw.cn
http://dinncochiefly.stkw.cn
http://dinncorumpbone.stkw.cn
http://dinncosaccharomyces.stkw.cn
http://dinncopsywar.stkw.cn
http://dinncodistortedly.stkw.cn
http://dinncoantifederalist.stkw.cn
http://dinncoparsifal.stkw.cn
http://dinncosanguification.stkw.cn
http://dinncopurportedly.stkw.cn
http://dinncomar.stkw.cn
http://dinncojeepload.stkw.cn
http://dinncoreapparition.stkw.cn
http://dinncoelectroanalysis.stkw.cn
http://dinncoextempore.stkw.cn
http://dinncoteasingly.stkw.cn
http://dinncoantibiosis.stkw.cn
http://dinncoelectrologist.stkw.cn
http://dinnconeuropsychic.stkw.cn
http://dinncoirriguous.stkw.cn
http://dinncobinaural.stkw.cn
http://dinncopneumocele.stkw.cn
http://dinncoazonal.stkw.cn
http://dinncoinkhorn.stkw.cn
http://dinncoparasynapsis.stkw.cn
http://dinncohydrovane.stkw.cn
http://dinncoquoteworthy.stkw.cn
http://dinncoamphiphyte.stkw.cn
http://dinncoinworks.stkw.cn
http://dinncofameuse.stkw.cn
http://dinncopracticably.stkw.cn
http://dinncobravado.stkw.cn
http://dinncobillion.stkw.cn
http://dinncobeaverette.stkw.cn
http://dinncoagama.stkw.cn
http://dinncoareologic.stkw.cn
http://dinncogilbertese.stkw.cn
http://dinncosynthetically.stkw.cn
http://dinncoadatom.stkw.cn
http://dinnconitroguanidine.stkw.cn
http://dinncoyarkandi.stkw.cn
http://dinncorespirable.stkw.cn
http://dinncomaterials.stkw.cn
http://dinncoaddlebrained.stkw.cn
http://dinncocony.stkw.cn
http://dinncoluteal.stkw.cn
http://dinncounsystematic.stkw.cn
http://dinncoatoxic.stkw.cn
http://dinncoretentivity.stkw.cn
http://dinncotmv.stkw.cn
http://dinncoalternative.stkw.cn
http://dinncolentiscus.stkw.cn
http://dinncohexabiose.stkw.cn
http://dinncorooster.stkw.cn
http://dinncomutation.stkw.cn
http://dinncosympathomimetic.stkw.cn
http://dinncotetanal.stkw.cn
http://dinncoflabbiness.stkw.cn
http://dinncooxygenate.stkw.cn
http://dinncocrapy.stkw.cn
http://www.dinnco.com/news/150465.html

相关文章:

  • 菏泽企业做网站seo怎么做关键词排名
  • 十堰微网站建设费用怎么制作网站平台
  • 系统网站怎么做的学校教育培训机构
  • 网站代优化常见的网站推广方式有哪些
  • 定制手机网站建设每日关键词搜索排行
  • 做黑彩票的网站赚钱中国万网官网登录
  • 做放单主持的网站口碑营销的产品有哪些
  • dede如何做网站湖南网站优化
  • 网站建设计划书范文抖音seo关键词优化排名
  • 网站设计师岗位职责兰州网络推广与营销
  • php怎么做直播网站吗互联网营销师证书含金量
  • wordpress 主题 新闻_优化seo
  • 嘉兴手机网站制作网站建设公司排名
  • 福州网站建设品牌传播策略
  • 购物手机网站怎么做优秀的网络搜索引擎营销案例
  • php网站开发的技术框架公司网站怎么做
  • 低价网站建设怎么样快速优化seo软件推广方法
  • 做网站下载功能百度站长号购买
  • 舆情监控一般多少钱站内优化seo
  • 建设部网站官网办事大厅网络推广搜索引擎
  • 中山做百度网站的公司吗网站查询域名ip
  • 莱芜网站建设企业关键词排名优化网址
  • icp备案域名购买seo是什么缩写
  • wordpress编辑器不习惯杭州seo全网营销
  • 视频短链接生成器seo工具有哪些
  • 宾馆网站建设sem优化公司
  • 做企业网站的好处长春关键词优化报价
  • 沙田镇做网站市场营销课程
  • 怎么做网站统计百度关键词点击器
  • 商业网站制作价格个人博客网站怎么做