{"id":545,"date":"2021-03-11T17:20:48","date_gmt":"2021-03-11T09:20:48","guid":{"rendered":"\/?p=545"},"modified":"2021-03-11T17:20:49","modified_gmt":"2021-03-11T09:20:49","slug":"2-3%e9%80%9a%e9%81%93","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=545","title":{"rendered":"2.3\u901a\u9053"},"content":{"rendered":"<p>\u5728Go\u4e2d,\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u539f\u5b50\u51fd\u6570\u548c\u4e92\u65a5\u9501\u6765\u4fdd\u8bc1\u5bf9\u5171\u4eab\u8d44\u6e90\u548c\u5b89\u5168\u8bbf\u95ee\u4ee5\u53ca\u6d88\u9664\u7ade\u4e89\u72b6\u6001,\u8fd8\u53ef\u4ee5\u5229\u7528\u901a\u9053,\u901a\u8fc7\u53d1\u9001\u548c\u63a5\u6536\u5171\u4eab\u7684\u8d44\u6e90,\u5728goroutine\u8fdb\u884c\u540c\u6b65<\/p>\n<p>\u5f53\u4e00\u4e2a\u8d44\u6e90\u9700\u8981\u5728goroutine\u4e4b\u95f4\u8fdb\u884c\u5171\u4eab\u7684\u65f6\u5019,\u901a\u9053\u5728goroutine\u8fdb\u884c\u67b6\u8d77\u4e86\u4e00\u4e2a\u7ba1\u9053,\u901a\u9053\u5728goroutine\u4e4b\u95f4\u67b6\u8d77\u4e86\u7ba1\u9053,\u786e\u4fdd\u4e86\u4ea4\u6362\u6570\u636e\u7684\u673a\u5236,\u58f0\u660e\u901a\u9053\u7684\u65f6\u5019,\u6211\u4eec\u9700\u8981\u5728\u58f0\u660e\u7684\u4f7f\u7528\u6307\u5b9a\u5171\u4eab\u7684\u6570\u636e\u7c7b\u578b,\u901a\u8fc7\u901a\u9053\u5171\u4eab\u5185\u7f6e\u7c7b\u578b,\u547d\u4ee4\u7c7b\u578b,\u7ed3\u6784\u7c7b\u578b,\u5f15\u7528\u7c7b\u578b\u7684\u503c\u6216\u8005\u7c7b\u578b<\/p>\n<p>\u521b\u5efa\u901a\u9053\u7684\u65b9\u5f0f\u5982\u4e0b<\/p>\n<p>\/\/\u65e0\u7f13\u51b2<\/p>\n<p>unbuffered := make(chan int)<\/p>\n<p>\/\/\u6709\u7f13\u51b2<\/p>\n<p>buffered := make(chan string, 10)<\/p>\n<p>\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u5185\u7f6e\u51fd\u6570,\u4e00\u4e2a\u65e0\u7f13\u51b2\u7684\u901a\u9053,\u4e00\u4e2a\u6709\u7f13\u51b2\u7684\u901a\u9053<\/p>\n<p>\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u6709\u7f13\u51b2\u7684\u901a\u9053,\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u8fd9\u4e2a\u6307\u5b9a\u901a\u9053\u7684\u7f13\u51b2\u533a\u7684\u5927\u5c0f<\/p>\n<p>\u7136\u540e\u5f80\u901a\u9053\u5982\u677e\u503c\u7684\u4ee3\u7801\u5982\u4e0b,\u9700\u8981\u7528\u5230 &lt;- \u64cd\u4f5c\u7b26<\/p>\n<p>buffered := make(chan string,10)<\/p>\n<p>\/\/\u53d1\u9001\u4e00\u4e2a\u6570\u636e<\/p>\n<p>buffered &lt;- &#8220;Gopher&#8221;<\/p>\n<p>\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a10\u4e2a\u503c\u7684\u7f13\u51b2\u533a,\u4e4b\u540e\u6211\u4eec\u901a\u8fc7\u901a\u9053\u53d1\u9001\u5b57\u7b26\u4e32 &#8220;Gopher&#8221;<\/p>\n<p>\u4e3a\u4e86\u53e6\u4e00\u4e2agoroutine\u4e4b\u540e\u80fd\u591f\u63a5\u53d7\u5230\u8fd9\u4e2a\u5b57\u7b26\u4e32,\u6211\u4eec\u4ecd\u7136\u9700\u8981\u4f7f\u7528 &lt;- \u64cd\u4f5c\u7b26<\/p>\n<p>\u63a5\u6536\u4e00\u4e2a\u503c,\u6211\u4eec\u53ef\u4ee5\u5982\u4e0b\u7684\u5b57\u7b26\u4e32<\/p>\n<p>value := &lt;- buffered<\/p>\n<p>\u4e3a\u4e86\u63a5\u6536\u4e00\u4e2a\u503c, &lt;- \u8fd0\u7b97\u7b26\u8981\u5728\u64cd\u4f5c\u7684\u901a\u9053\u53d8\u91cf\u7684\u5de6\u4fa7<\/p>\n<p>\u901a\u9053\u4e2d,\u6709\u7f13\u51b2\u7684\u548c\u65e0\u7f13\u51b2\u7684,\u662f\u5177\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u7684<\/p>\n<p>\u5bf9\u4e8e\u65e0\u7f13\u51b2\u7684\u901a.\u5728\u63a5\u6536\u524d\u6ca1\u6709\u80fd\u529b\u4fdd\u5b58\u4efb\u4f55\u503c\u7684\u901a\u9053<\/p>\n<p>\u8981\u6c42\u7684\u662f\u53d1\u9001\u7684goroutine\u63a5\u6536\u7684goroutne\u540c\u65f6\u51c6\u5907\u597d,\u4e5f\u5c31\u662f\u961f\u5217\u4e2d0\u5b58\u50a8\u7684\u90a3\u4e00\u7c7b\u578b<\/p>\n<p>\u6211\u4eec\u770b\u4e00\u4e0bgoroutine\u5982\u4f55\u901a\u8fc7\u65e0\u7f13\u51b2\u7684\u901a\u9053\u5171\u4eab\u7684\u4e00\u4e2a\u503c,\u5982\u679c\u6ca1\u6709\u4eba\u4ea4\u6362,\u90a3\u4e48\u4f1a\u4e00\u76f4\u9501\u4f4f<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"573\" height=\"490\" class=\"wp-image-546\" src=\"\/wp-content\/uploads\/2021\/03\/unnamed-file-283.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-283.png 573w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-283-300x257.png 300w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/><\/p>\n<p>\u6211\u4eec\u770b\u4e00\u4e32\u4ee3\u7801<\/p>\n<p>package main<\/p>\n<p>import (<\/p>\n<p>&#8220;fmt&#8221;<\/p>\n<p>&#8220;math\/rand&#8221;<\/p>\n<p>&#8220;sync&#8221;<\/p>\n<p>&#8220;time&#8221;<\/p>\n<p>)<\/p>\n<p>\/\/ wg is used to wait for the program to finish.<\/p>\n<p>var wg sync.WaitGroup<\/p>\n<p>func init() {<\/p>\n<p>rand.Seed(time.Now().UnixNano())<\/p>\n<p>}<\/p>\n<p>\/\/ main is the entry point for all Go programs.<\/p>\n<p>func main() {<\/p>\n<p>\/\/ Create an unbuffered channel.<\/p>\n<p>court := make(chan int)<\/p>\n<p>\/\/ Add a count of two, one for each goroutine.<\/p>\n<p>wg.Add(2)<\/p>\n<p>\/\/ Launch two players.<\/p>\n<p>go player(&#8220;Nadal&#8221;, court)<\/p>\n<p>go player(&#8220;Djokovic&#8221;, court)<\/p>\n<p>\/\/ Start the set.<\/p>\n<p>court &lt;- 1<\/p>\n<p>\/\/ Wait for the game to finish.<\/p>\n<p>wg.Wait()<\/p>\n<p>}<\/p>\n<p>\/\/ player simulates a person playing the game of tennis.<\/p>\n<p>func player(name string, court chan int) {<\/p>\n<p>\/\/ Schedule the call to Done to tell main we are done.<\/p>\n<p>defer wg.Done()<\/p>\n<p>for {<\/p>\n<p>\/\/ Wait for the ball to be hit back to us.<\/p>\n<p>ball, ok := &lt;-court<\/p>\n<p>if !ok {<\/p>\n<p>\/\/ If the channel was closed we won.<\/p>\n<p>fmt.Printf(&#8220;Player %s Won\\n&#8221;, name)<\/p>\n<p>return<\/p>\n<p>}<\/p>\n<p>\/\/ Pick a random number and see if we miss the ball.<\/p>\n<p>n := rand.Intn(100)<\/p>\n<p>if n%13 == 0 {<\/p>\n<p>fmt.Printf(&#8220;Player %s Missed\\n&#8221;, name)<\/p>\n<p>\/\/ Close the channel to signal we lost.<\/p>\n<p>close(court)<\/p>\n<p>return<\/p>\n<p>}<\/p>\n<p>\/\/ Display and then increment the hit count by one.<\/p>\n<p>fmt.Printf(&#8220;Player %s Hit %d\\n&#8221;, name, ball)<\/p>\n<p>ball++<\/p>\n<p>\/\/ Hit the ball back to the opposing player.<\/p>\n<p>court &lt;- ball<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u8fd9\u7aef\u4ee3\u7801\u4e2d,\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aint\u7c7b\u578b\u7684\u65e0\u7f13\u51b2\u7684\u901a\u9053,\u8ba9\u4e24\u4e2agoroutine\u5728\u51fb\u7403\u7684\u65f6\u5019\u4e92\u76f8\u540c\u6b65,\u7136\u540e\u572828\u548c29\u884c,\u521b\u5efa\u4e86\u4e24\u4e2agoroutine<\/p>\n<p>\u7136\u540e\u5206\u522b\u7684\u4f20\u9012\u6570\u503c\u7ed9\u8fd9\u4e24\u4e2agoroutine<\/p>\n<p>\u7136\u540e\u7528\u4e0d\u540c\u7684\u6a21\u578b,\u4f7f\u7528\u65e0\u7f13\u51b2\u7684\u901a\u9053,\u5728goroutine\u4e4b\u95f4\u540c\u6b65\u6570\u636e,\u5728main\u51fd\u6570\u768417\u884c,\u521b\u5efa\u4e86\u65e0\u7f13\u51b2\u7684\u901a\u9053,\u7528\u6765\u6765\u56de\u7684\u4f20\u9012,\u6211\u4eec\u8bbe\u7f6e\u4e86WaitGroup\u4e3a1,\u7136\u540e\u6211\u4eec\u4e0d\u65ad\u7684\u4f20\u9012,\u76f4\u5230\u51fd\u6570\u8fd4\u56de\u4e4b\u524d,\u963b\u585e\u8fd9\u4e2aWaitGroup\u7684,\u7b49\u5f85\u4e00\u4f4d\u8dd1\u6b65\u8005\u5b8c\u6210\u6bd4\u8d5b<\/p>\n<p>\u5728Runner goroutine\u91cc\u9762,\u53ef\u4ee5\u770b\u5230\u63a5\u529b\u68d2\u5982\u4f55\u5728\u8dd1\u6b65\u8005\u4e2d\u4f20\u9012,\u4e00\u65e6\u63a5\u529b\u68d2\u4f20\u4e86,\u5c31\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u8dd1\u6b65\u8005,\u7136\u540e\u63a5\u529b\u4e0b\u4e00\u68d2,\u76f4\u5230\u56db\u4e2a\u8dd1\u8005\u90fd\u5b8c\u6210\u4e86,\u624d\u4f1a\u9501\u4f4fgoroutine,\u7b49\u5f85\u4ea4\u63a5\u5b8c\u6210<\/p>\n<p>\u7136\u540e\u662f\u6709\u7f13\u51b2\u7684\u901a\u9053,\u5728\u6d88\u8d39\u4e4b\u524d\u80fd\u591f\u5b58\u50a8\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u503c\u7684\u901a\u9053,\u8fd9\u79cd\u7c7b\u578b\u7684\u901a\u9053\u5e76\u4e0d\u4f1a\u5f3a\u5236\u8981\u6c42goroutine\u4e4b\u95f4\u540c\u65f6\u53d1\u9001\u548c\u63a5\u6536,\u6574\u4f53\u7684\u63a5\u6536\u6d41\u7a0b\u5982\u4e0b<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"510\" height=\"310\" class=\"wp-image-547\" src=\"\/wp-content\/uploads\/2021\/03\/unnamed-file-284.png\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-284.png 510w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-284-300x182.png 300w\" sizes=\"(max-width: 510px) 100vw, 510px\" \/><\/p>\n<p>\u63a5\u4e0b\u6765\u662f\u4e00\u4e2a\u4f7f\u7528\u7f13\u51b2\u7684\u901a\u9053\u7684\u4f8b\u5b50<\/p>\n<p>package main<\/p>\n<p>import (<\/p>\n<p>&#8220;fmt&#8221;<\/p>\n<p>&#8220;math\/rand&#8221;<\/p>\n<p>&#8220;sync&#8221;<\/p>\n<p>&#8220;time&#8221;<\/p>\n<p>)<\/p>\n<p>const (<\/p>\n<p>numberGoroutines = 4 \/\/ Number of goroutines to use.<\/p>\n<p>taskLoad = 10 \/\/ Amount of work to process.<\/p>\n<p>)<\/p>\n<p>\/\/ wg is used to wait for the program to finish.<\/p>\n<p>var wg sync.WaitGroup<\/p>\n<p>\/\/ init is called to initialize the package by the<\/p>\n<p>\/\/ Go runtime prior to any other code being executed.<\/p>\n<p>func init() {<\/p>\n<p>\/\/ Seed the random number generator.<\/p>\n<p>rand.Seed(time.Now().Unix())<\/p>\n<p>}<\/p>\n<p>\/\/ main is the entry point for all Go programs.<\/p>\n<p>func main() {<\/p>\n<p>\/\/ Create a buffered channel to manage the task load.<\/p>\n<p>tasks := make(chan string, taskLoad)<\/p>\n<p>\/\/ Launch goroutines to handle the work.<\/p>\n<p>wg.Add(numberGoroutines)<\/p>\n<p>for gr := 1; gr &lt;= numberGoroutines; gr++ {<\/p>\n<p>go worker(tasks, gr)<\/p>\n<p>}<\/p>\n<p>\/\/ Add a bunch of work to get done.<\/p>\n<p>for post := 1; post &lt;= taskLoad; post++ {<\/p>\n<p>tasks &lt;- fmt.Sprintf(&#8220;Task : %d&#8221;, post)<\/p>\n<p>}<\/p>\n<p>\/\/ Close the channel so the goroutines will quit<\/p>\n<p>\/\/ when all the work is done.<\/p>\n<p>close(tasks)<\/p>\n<p>\/\/ Wait for all the work to get done.<\/p>\n<p>wg.Wait()<\/p>\n<p>}<\/p>\n<p>\/\/ worker is launched as a goroutine to process work from<\/p>\n<p>\/\/ the buffered channel.<\/p>\n<p>func worker(tasks chan string, worker int) {<\/p>\n<p>\/\/ Report that we just returned.<\/p>\n<p>defer wg.Done()<\/p>\n<p>for {<\/p>\n<p>\/\/ Wait for work to be assigned.<\/p>\n<p>task, ok := &lt;-tasks<\/p>\n<p>if !ok {<\/p>\n<p>\/\/ This means the channel is empty and closed.<\/p>\n<p>fmt.Printf(&#8220;Worker: %d : Shutting Down\\n&#8221;, worker)<\/p>\n<p>return<\/p>\n<p>}<\/p>\n<p>\/\/ Display we are starting the work.<\/p>\n<p>fmt.Printf(&#8220;Worker: %d : Started %s\\n&#8221;, worker, task)<\/p>\n<p>\/\/ Randomly wait to simulate work time.<\/p>\n<p>sleep := rand.Int63n(100)<\/p>\n<p>time.Sleep(time.Duration(sleep) * time.Millisecond)<\/p>\n<p>\/\/ Display we finished the work.<\/p>\n<p>fmt.Printf(&#8220;Worker: %d : Completed %s\\n&#8221;, worker, task)<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u521b\u5efa\u4e86\u4e00\u4e2a\u7f13\u51b2\u5bb9\u91cf\u4e3a10 \u7684string\u7c7b\u578b\u7684\u901a\u9053,\u7136\u540e\u8bbe\u7f6e\u4e86\u4e00\u4e2aWaitGroup\u4e3a4<\/p>\n<p>\u7136\u540e\u5728\u4ee3\u7801\u4e2d,\u6211\u4eec\u5173\u95ed\u4e86\u901a\u9053,\u4f46\u662f\u6ca1\u6709\u7ed3\u675f\u4ee3\u7801\u8fd9\u662f\u56e0\u4e3ago\u8bed\u8a00\u4e2d,\u901a\u9053\u5173\u95ed\u4e86,\u53ea\u662f\u4e0d\u80fd\u5b58\u5165\u6570\u636e,\u4f46\u662f\u53ef\u4ee5\u4ece\u4e2d\u53d6\u51fa\u7f13\u51b2\u7684\u5168\u90e8\u503c,\u8fd9\u662f\u56e0\u4e3a\u901a\u9053\u4e2d\u4fdd\u5b58\u4e86\u6240\u6709\u7684\u6570\u636e<\/p>\n<p>\u5728worker\u51fd\u6570\u4e2d,\u6211\u4eec\u5229\u7528\u4e86\u4e00\u4e2a\u65e0\u9650\u7684for\u5faa\u73af,\u5904\u7406\u6240\u6709\u63a5\u6536\u5230\u7684\u5de5\u4f5c,\u76f4\u5230\u5229\u7528ok\u6807\u5fd7,\u6765\u68c0\u67e5\u901a\u9053\u662f\u5426\u6e05\u7a7a\u800c\u4e14\u5df2\u7ecf\u5173\u95ed,\u76f4\u5230ok\u6807\u5fd7\u4f4dfalse,\u8bf4\u660e\u63a5\u6536\u7684\u503c\u662f\u65e0\u6548\u7684<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Go\u4e2d,\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u539f\u5b50\u51fd\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/545"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=545"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/545\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}