go语言高级并发模型

所需积分/C币:10 2018-06-20 10:46:24 1.71MB PDF
13
收藏 收藏
举报

Go语言官方高级并发模型的演示和讨论,展示go语言的高级并发运用技术和特点能力。
Go supports concurrency In the language and runtime, not a library This changes how you structure your programs Goroutines and channels Goroutines are independently executing functions in the same address space gog(1,2) Channels are typed values that allow goroutines to synchronize and exchange information C: make( chan int) go func(IC<-3JO For more on the basics, watch Go Concurrency Patterns(Pike, 2012) (htrp //talks golang org/2012/concurency slde#1) EXample: ping-pong type Ball structi hits int j func minot table make(chan *Ball go player ping", table) go player("pong",table) table < new( Ball)// game on; toss the ball time. Sleep(1 time. Second) <-table / game over grab the ball func player(name string, table chan *Ball)i for i ball .=<-table ballhits++ fmt Printin(name, ball hits) time. Sleep(100 time Millisecond) table < ball Deadlock detection type Ball structi hits int j func minot table make(chan *Ball go player ping", table) go player ("pong",table) // table < new(Ball)// game on; toss the ball time. Sleep(1 time. Second) <-table / game over grab the ball func player(name string, table chan *Ball)i for i ball .=<-table ballhits++ fmt Printin(name, ball hits) time. Sleep(100 time Millisecond) table < ball Panic dumps the stacks type Ball struct hits int y func minot table make(chan *Ball go player ping", table) go player ("pong",table) table < new(Ball)// game on the ball time. Sleep(1 time. Second) <-table / game over grab the ball panic("show me the stacks") func player (name string, table chan *Ball)[ for i ball <-table fmt Println(name, ballhits) time. Sleep (100 time. Millisecond) table < ball It's easy to go, but how to stop? Long-lived programs need to clean up et' s look at how to write programs that handle communication, periodic events, and cancellation The core is go's select statement like a switch but the decision is made based on the ability to communicate select f case xc < x / sent x on xc case y : <-yo / received y from yc EXample: feed reader My favorite feed reader disappeared. I need a new one Why not write one? Where do we start?

...展开详情
试读 43P go语言高级并发模型
立即下载
限时抽奖 低至0.43元/次
身份认证后 购VIP低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
关注 私信
上传资源赚钱or赚积分
最新推荐
go语言高级并发模型 10积分/C币 立即下载
1/43
go语言高级并发模型第1页
go语言高级并发模型第2页
go语言高级并发模型第3页
go语言高级并发模型第4页
go语言高级并发模型第5页
go语言高级并发模型第6页
go语言高级并发模型第7页
go语言高级并发模型第8页
go语言高级并发模型第9页

试读结束, 可继续读4页

10积分/C币 立即下载