{"id":15453,"date":"2024-11-18T18:07:49","date_gmt":"2024-11-18T10:07:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=15453"},"modified":"2024-11-18T18:07:49","modified_gmt":"2024-11-18T10:07:49","slug":"goroutine%e5%92%8cchannel%e7%9a%84%e8%af%a6%e7%bb%86%e7%90%86%e8%a7%a3%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15453\/","title":{"rendered":"Goroutine\u548cchannel\u7684\u8be6\u7ec6\u7406\u89e3\uff08\u4e8c\uff09"},"content":{"rendered":"<ul>\n<li>\n<h2>Go\u8bed\u8a00\u7684\u5e76\u53d1\u548c\u5e76\u884c<\/h2>\n<\/li>\n<\/ul>\n<p>\u4e0d\u77e5\u9053\u4f60\u6709\u6ca1\u6709\u6ce8\u610f\u5230\u4e00\u4e2a\u73b0\u8c61\uff0c\u8fd8\u662f\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5982\u679c\u6211\u8dd1\u5728\u4e24\u4e2agoroutines\u91cc\u9762\u7684\u8bdd:<\/p>\n<pre>var quit chan int = make(chan int)\r\nfunc loop() {\r\n\u00a0\u00a0\u00a0 for i := 0; i &lt; 10; i++ {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fmt.Printf(\"%d \", i)\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 quit &lt;- 0\r\n}\r\nfunc main() {\r\n\u00a0\u00a0\u00a0 go loop()\u00a0  \/\/ \u5f00\u4e24\u4e2agoroutine\u8dd1\u51fd\u6570loop, loop\u51fd\u6570\u8d1f\u8d23\u6253\u537010\u4e2a\u6570\r\n\u00a0\u00a0\u00a0 go loop()\r\n\u00a0\u00a0\u00a0 for i := 0; i &lt; 2; i++ {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;- quit\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6211\u4eec\u89c2\u5bdf\u4e0b\u8f93\u51fa:<\/p>\n<pre>0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9<\/pre>\n<p>\u8fd9\u662f\u4e0d\u662f\u6709\u4ec0\u4e48\u95ee\u9898\uff1f?<\/p>\n<p>\u4ee5\u524d\u6211\u4eec\u7528\u7ebf\u7a0b\u53bb\u505a\u7c7b\u4f3c\u4efb\u52a1\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u7684\u7ebf\u7a0b\u4f1a\u62a2\u5360\u5f0f\u5730\u8f93\u51fa\uff0c \u8868\u73b0\u51fa\u6765\u7684\u662f\u4e71\u5e8f\u5730\u8f93\u51fa\u3002\u800cgoroutine\u4e3a\u4ec0\u4e48\u662f\u8fd9\u6837\u8f93\u51fa\u7684\u5462\uff1f<\/p>\n<ul>\n<li>\n<h2><strong>goroutine\u662f\u5728\u5e76\u884c\u5417\uff1f<\/strong><\/h2>\n<\/li>\n<\/ul>\n<p>\u6211\u4eec\u627e\u4e2a\u4f8b\u5b50\u6d4b\u8bd5\u4e0b:<\/p>\n<pre>package main\r\n\r\nimport \"fmt\"\r\nimport \"time\"\r\n\r\nvar quit chan int\r\n\r\nfunc foo(id int) {\r\n\u00a0\u00a0\u00a0 fmt.Println(id)\r\n\u00a0\u00a0\u00a0 time.Sleep(time.Second) \/\/ \u505c\u987f\u4e00\u79d2\r\n\u00a0\u00a0\u00a0 quit &lt;- 0 \/\/ \u53d1\u6d88\u606f\uff1a\u6211\u6267\u884c\u5b8c\u5566\uff01\r\n}\r\n\r\nfunc main() {\r\n\u00a0\u00a0\u00a0 count := 1000\r\n\u00a0\u00a0\u00a0 quit = make(chan int, count) \/\/ \u7f13\u51b21000\u4e2a\u6570\u636e\r\n\u00a0\u00a0\u00a0 for i := 0; i &lt; count; i++ { \/\/\u5f001000\u4e2agoroutine\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 go foo(i)\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 for i :=0 ; i &lt; count; i++ { \/\/ \u7b49\u5f85\u6240\u6709\u5b8c\u6210\u6d88\u606f\u53d1\u9001\u5b8c\u6bd5\u3002\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;- quit\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>\u8ba9\u6211\u4eec\u8dd1\u4e00\u4e0b\u8fd9\u4e2a\u7a0b\u5e8f(\u4e4b\u6240\u4ee5\u5148\u7f16\u8bd1\u518d\u8fd0\u884c\uff0c\u662f\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u8dd1\u7684\u5c3d\u91cf\u5feb,\u6d4b\u8bd5\u7ed3\u679c\u66f4\u597d):<\/p>\n<pre>go build test.go\r\ntime .\/test\r\n.\/test\u00a0 0.01s user 0.01s system 1% cpu 1.016 total<\/pre>\n<p>\u6211\u4eec\u770b\u5230\uff0c\u603b\u8ba1\u7528\u65f6\u63a5\u8fd1\u4e00\u79d2\u3002 \u8c8c\u4f3c\u5e76\u884c\u4e86\uff01<\/p>\n<p>\u6211\u4eec\u9700\u8981\u9996\u5148\u8003\u8651\u4e0b\u4ec0\u4e48\u662f\u5e76\u53d1, \u4ec0\u4e48\u662f\u5e76\u884c<\/p>\n<ul>\n<li>\n<h2><strong>\u5e76\u884c\u548c\u5e76\u53d1<\/strong><\/h2>\n<\/li>\n<\/ul>\n<p>\u4ece\u6982\u5ff5\u4e0a\u8bb2\uff0c\u5e76\u53d1\u548c\u5e76\u884c\u662f\u4e0d\u540c\u7684, \u7b80\u5355\u6765\u8bf4\u770b\u8fd9\u4e2a\u56fe\u7247(\u539f\u56fe\u6765\u81ea\u8fd9\u91cc)<\/p>\n<p><a href=\"http:\/\/fwq.ai\/wp-content\/uploads\/2017\/08\/con_and_par.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15454\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/con_and_par.jpg\" width=\"600\" height=\"451\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/con_and_par.jpg 600w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/con_and_par-300x226.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" title=\"Goroutine\u548cchannel\u7684\u8be6\u7ec6\u7406\u89e3\uff08\u4e8c\uff09\u63d2\u56fe\" alt=\"Goroutine\u548cchannel\u7684\u8be6\u7ec6\u7406\u89e3\uff08\u4e8c\uff09\u63d2\u56fe\" \/><\/a><\/p>\n<ul>\n<li>\u4e24\u4e2a\u961f\u5217\uff0c\u4e00\u4e2aCoffee\u673a\u5668\uff0c\u90a3\u662f\u5e76\u53d1<\/li>\n<li>\u4e24\u4e2a\u961f\u5217\uff0c\u4e24\u4e2aCoffee\u673a\u5668\uff0c\u90a3\u662f\u5e76\u884c<\/li>\n<\/ul>\n<p>\u66f4\u591a\u7684\u8d44\u6599\uff1a \u5e76\u53d1\u4e0d\u662f\u5e76\u884c, \u5f53\u7136Google\u4e0a\u6709\u66f4\u591a\u5173\u4e8e\u5e76\u884c\u548c\u5e76\u53d1\u7684\u533a\u522b\u3002<\/p>\n<p>\u90a3\u4e48\u56de\u5230\u4e00\u5f00\u59cb\u7684\u7591\u95ee\u4e0a\uff0c\u4ece\u4e0a\u9762\u7684\u4e24\u4e2a\u4f8b\u5b50\u6267\u884c\u540e\u7684\u8868\u73b0\u6765\u770b\uff0c\u591a\u4e2agoroutine\u8dd1loop\u51fd\u6570\u4f1a\u6328\u4e2agoroutine\u53bb\u8fdb\u884c\uff0c\u800csleep\u5219\u662f\u4e00\u8d77\u6267\u884c\u7684\u3002<\/p>\n<p>\u8fd9\u662f\u4e3a\u4ec0\u4e48\uff1f<\/p>\n<p>\u9ed8\u8ba4\u5730\uff0c Go\u6240\u6709\u7684goroutines\u53ea\u80fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u91cc\u8dd1 \u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c \u4ee5\u4e0a\u4e24\u4e2a\u4ee3\u7801\u90fd\u4e0d\u662f\u5e76\u884c\u7684\uff0c\u4f46\u662f\u90fd\u662f\u662f\u5e76\u53d1\u7684\u3002<\/p>\n<p>\u5982\u679c\u5f53\u524dgoroutine\u4e0d\u53d1\u751f\u963b\u585e\uff0c\u5b83\u662f\u4e0d\u4f1a\u8ba9\u51faCPU\u7ed9\u5176\u4ed6goroutine\u7684, \u6240\u4ee5\u4f8b\u5b50\u4e00\u4e2d\u7684\u8f93\u51fa\u4f1a\u662f\u4e00\u4e2a\u4e00\u4e2agoroutine\u8fdb\u884c\u7684\uff0c\u800csleep\u51fd\u6570\u5219\u963b\u585e\u6389\u4e86 \u5f53\u524dgoroutine, \u5f53\u524dgoroutine\u4e3b\u52a8\u8ba9\u5176\u4ed6goroutine\u6267\u884c, \u6240\u4ee5\u5f62\u6210\u4e86\u903b\u8f91\u4e0a\u7684\u5e76\u884c, \u4e5f\u5c31\u662f\u5e76\u53d1\u3002<\/p>\n<ul>\n<li>\n<h2><strong>\u771f\u6b63\u7684\u5e76\u884c<\/strong><\/h2>\n<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u8fbe\u5230\u771f\u6b63\u7684\u5e76\u884c\uff0c\u6211\u4eec\u9700\u8981\u544a\u8bc9Go\u6211\u4eec\u5141\u8bb8\u540c\u65f6\u6700\u591a\u4f7f\u7528\u591a\u4e2a\u6838\u3002<\/p>\n<p>\u56de\u5230\u8d77\u521d\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u8bbe\u7f6e\u6700\u5927\u5f002\u4e2a\u539f\u751f\u7ebf\u7a0b, \u6211\u4eec\u9700\u8981\u7528\u5230runtime\u5305(runtime\u5305\u662fgoroutine\u7684\u8c03\u5ea6\u5668):<\/p>\n<pre>import (\r\n    \"fmt\"\r\n    \"runtime\"\r\n)\r\nvar quit chan int = make(chan int)\r\n\r\nfunc loop() {\r\n    for i := 0; i &lt; 100; i++ { \/\/\u4e3a\u4e86\u89c2\u5bdf\uff0c\u8dd1\u591a\u4e9b\r\n        fmt.Printf(\"%d \", i)\r\n    }\r\n    quit &lt;- 0\r\n}\r\n\r\nfunc main() {\r\n    runtime.GOMAXPROCS(2) \/\/ \u6700\u591a\u4f7f\u75282\u4e2a\u6838\r\n    go loop()\r\n    go loop()\r\n    for i := 0; i &lt; 2; i++ {\r\n        &lt;- quit\r\n    }\r\n }<\/pre>\n<p>\u8fd9\u4e0b\u4f1a\u770b\u5230\u4e24\u4e2agoroutine\u4f1a\u62a2\u5360\u5f0f\u5730\u8f93\u51fa\u6570\u636e\u4e86\u3002<\/p>\n<pre>0 0 1 2 3 1 4 2 5 3 6 4 7 5 8 6 9 7 8 9<\/pre>\n<p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fd9\u6837\u663e\u5f0f\u5730\u8ba9\u51faCPU\u65f6\u95f4\uff1a<\/p>\n<pre>func loop() {\r\n    for i := 0; i &lt; 10; i++ {\r\n        runtime.Gosched() \/\/ \u663e\u5f0f\u5730\u8ba9\u51faCPU\u65f6\u95f4\u7ed9\u5176\u4ed6goroutine\r\n        fmt.Printf(\"%d \", i)\r\n    }\r\n    quit &lt;- 0\r\n }\r\n\r\nfunc main() {\r\n    go loop()\r\n    go loop()\r\n    for i := 0; i &lt; 2; i++ {\r\n        &lt;- quit\r\n    }\r\n }<\/pre>\n<p>\u89c2\u5bdf\u4e0b\u7ed3\u679c\u4f1a\u770b\u5230\u8fd9\u6837\u6709\u89c4\u5f8b\u7684\u8f93\u51fa:<\/p>\n<pre>0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9<\/pre>\n<p>\u5176\u5b9e\uff0c\u8fd9\u79cd\u4e3b\u52a8\u8ba9\u51faCPU\u65f6\u95f4\u7684\u65b9\u5f0f\u4ecd\u7136\u662f\u5728\u5355\u6838\u91cc\u8dd1\u3002\u4f46\u624b\u5de5\u5730\u5207\u6362goroutine\u5bfc\u81f4\u4e86\u770b\u4e0a\u53bb\u7684\u201c\u5e76\u884c\u201d\u3002<\/p>\n<p>\u5176\u5b9e\u4f5c\u4e3a\u4e00\u4e2aPython\u7a0b\u5e8f\u5458\uff0cgoroutine\u8ba9\u6211\u66f4\u591a\u5730\u60f3\u5230\u7684\u662fgevent\u7684\u534f\u7a0b\uff0c\u800c\u4e0d\u662f\u539f\u751f\u7ebf\u7a0b\u3002<\/p>\n<p>\u5173\u4e8eruntime\u5305\u5bf9goroutine\u7684\u8c03\u5ea6\uff0c\u5728stackoverflow\u4e0a\u6709\u4e00\u4e2a\u4e0d\u9519\u7684\u7b54\u6848:http:\/\/stackoverflow.com\/questions\/13107958\/what-exactly-does-runtime-gosched-do<\/p>\n<ul>\n<li>\n<h2><strong>\u4e00\u4e2a\u5c0f\u95ee\u9898<\/strong><\/h2>\n<\/li>\n<\/ul>\n<p>\u6211\u5728Segmentfault\u770b\u5230\u4e86\u8fd9\u4e2a\u95ee\u9898:\u00a0http:\/\/segmentfault.com\/q\/1010000000207474<\/p>\n<p>\u9898\u76ee\u8bf4\uff0c\u5982\u4e0b\u7684\u7a0b\u5e8f\uff0c\u6309\u7167\u7406\u89e3\u5e94\u8be5\u6253\u5370\u4e0b5\u6b21 \u201cworld\u201d\u5440\uff0c\u53ef\u662f\u4e3a\u4ec0\u4e48\u4ec0\u4e48\u4e5f\u6ca1\u6709\u6253\u5370<\/p>\n<pre>package main\r\n\r\nimport (\r\n    \"fmt\"\r\n )\r\n\r\nfunc say(s string) {\r\n    for i := 0; i &lt; 5; i++ {\r\n        fmt.Println(s)\r\n    }\r\n}\r\n\r\nfunc main() {\r\n    go say(\"world\") \/\/\u5f00\u4e00\u4e2a\u65b0\u7684Goroutines\u6267\u884c\r\n    for {\r\n    }\r\n}<\/pre>\n<p>\u697c\u4e0b\u7684\u7b54\u6848\u5df2\u7ecf\u5f88\u68d2\u4e86\uff0c\u8fd9\u91ccGo\u4ecd\u7136\u5728\u4f7f\u7528\u5355\u6838\uff0cfor\u6b7b\u5faa\u73af\u5360\u636e\u4e86\u5355\u6838CPU\u6240\u6709\u7684\u8d44\u6e90\uff0c\u800cmain\u7ebf\u548csay\u4e24\u4e2agoroutine\u90fd\u5728\u4e00\u4e2a\u7ebf\u7a0b\u91cc\u9762\uff0c \u6240\u4ee5say\u6ca1\u6709\u673a\u4f1a\u6267\u884c\u3002\u89e3\u51b3\u65b9\u6848\u8fd8\u662f\u4e24\u4e2a\uff1a<\/p>\n<ul>\n<li>\u5141\u8bb8Go\u4f7f\u7528\u591a\u6838(runtime.GOMAXPROCS)<\/li>\n<li>\u624b\u52a8\u663e\u5f0f\u8c03\u52a8(runtime.Gosched)<\/li>\n<\/ul>\n<ul>\n<li>\n<h2>runtime\u8c03\u5ea6\u5668<\/h2>\n<\/li>\n<\/ul>\n<p>runtime\u8c03\u5ea6\u5668\u662f\u4e2a\u5f88\u795e\u5947\u7684\u4e1c\u897f\uff0c\u4f46\u662f\u6211\u771f\u662f\u4f46\u613f\u5b83\u4e0d\u5b58\u5728\uff0c\u6211\u5e0c\u671b\u663e\u5f0f\u8c03\u5ea6\u80fd\u66f4\u4e3a\u81ea\u7136\u4e9b\uff0c\u591a\u6838\u5904\u7406\u9ed8\u8ba4\u5f00\u542f\u3002<\/p>\n<p>\u5173\u4e8eruntime\u5305\u51e0\u4e2a\u51fd\u6570:<\/p>\n<ul>\n<li>Gosched \u8ba9\u51facpu<\/li>\n<li>NumCPU \u8fd4\u56de\u5f53\u524d\u7cfb\u7edf\u7684CPU\u6838\u6570\u91cf<\/li>\n<li>GOMAXPROCS \u8bbe\u7f6e\u6700\u5927\u7684\u53ef\u540c\u65f6\u4f7f\u7528\u7684CPU\u6838\u6570<\/li>\n<li>Goexit \u9000\u51fa\u5f53\u524dgoroutine(\u4f46\u662fdefer\u8bed\u53e5\u4f1a\u7167\u5e38\u6267\u884c)<\/li>\n<\/ul>\n<ul>\n<li>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<\/li>\n<\/ul>\n<p>\u6211\u4eec\u4ece\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u9ed8\u8ba4\u7684, \u6240\u6709goroutine\u4f1a\u5728\u4e00\u4e2a\u539f\u751f\u7ebf\u7a0b\u91cc\u8dd1\uff0c\u4e5f\u5c31\u662f\u53ea\u4f7f\u7528\u4e86\u4e00\u4e2aCPU\u6838\u3002<\/p>\n<p>\u5728\u540c\u4e00\u4e2a\u539f\u751f\u7ebf\u7a0b\u91cc\uff0c\u5982\u679c\u5f53\u524dgoroutine\u4e0d\u53d1\u751f\u963b\u585e\uff0c\u5b83\u662f\u4e0d\u4f1a\u8ba9\u51faCPU\u65f6\u95f4\u7ed9\u5176\u4ed6\u540c\u7ebf\u7a0b\u7684goroutines\u7684\uff0c\u8fd9\u662fGo\u8fd0\u884c\u65f6\u5bf9goroutine\u7684\u8c03\u5ea6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528runtime\u5305\u6765\u624b\u5de5\u8c03\u5ea6\u3002<\/p>\n<p>\u672c\u6587\u5f00\u5934\u7684\u4e24\u4e2a\u4f8b\u5b50\u90fd\u662f\u9650\u5236\u5728\u5355\u6838CPU\u91cc\u6267\u884c\u7684\uff0c\u6240\u6709\u7684goroutines\u8dd1\u5728\u4e00\u4e2a\u7ebf\u7a0b\u91cc\u9762\uff0c\u5206\u6790\u5982\u4e0b:<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u4ee3\u7801\u4f8b\u5b50\u4e00\uff08loop\u51fd\u6570\u7684\u90a3\u4e2a\uff09\uff0c\u6bcf\u4e2agoroutine\u6ca1\u6709\u53d1\u751f\u5835\u585e(\u76f4\u5230quit\u6d41\u5165\u6570\u636e), \u6240\u4ee5\u5728quit\u4e4b\u524d\u6bcf\u4e2agoroutine\u4e0d\u4f1a\u4e3b\u52a8\u8ba9\u51faCPU\uff0c\u4e5f\u5c31\u53d1\u751f\u4e86\u4e32\u884c\u6253\u5370<\/li>\n<li>\u5bf9\u4e8e\u4ee3\u7801\u4f8b\u5b50\u4e8c\uff08time\u7684\u90a3\u4e2a\uff09\uff0c\u6bcf\u4e2agoroutine\u5728sleep\u88ab\u8c03\u7528\u7684\u65f6\u5019\u4f1a\u963b\u585e\uff0c\u8ba9\u51faCPU, \u6240\u4ee5\u4f8b\u5b50\u4e8c\u5e76\u53d1\u6267\u884c\u3002<\/li>\n<\/ul>\n<p>\u90a3\u4e48\u5173\u4e8e\u6211\u4eec\u5f00\u542f\u591a\u6838\u7684\u65f6\u5019\u5462\uff1fGo\u8bed\u8a00\u5bf9goroutine\u7684\u8c03\u5ea6\u884c\u4e3a\u53c8\u662f\u600e\u4e48\u6837\u7684\uff1f<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728Golang\u5b98\u65b9\u7f51\u7ad9\u7684\u8fd9\u91cc \u627e\u5230\u4e00\u53e5\u8bdd:<\/p>\n<p>When a coroutine blocks, such as by calling a blocking system call, the run-time automatically moves other coroutines on the same operating system thread to a different, runnable thread so they won\u2019t be blocked.<br \/>\n\u4e5f\u5c31\u662f\u8bf4:<\/p>\n<p>\u5f53\u4e00\u4e2agoroutine\u53d1\u751f\u963b\u585e\uff0cGo\u4f1a\u81ea\u52a8\u5730\u628a\u4e0e\u8be5goroutine\u5904\u4e8e\u540c\u4e00\u7cfb\u7edf\u7ebf\u7a0b\u7684\u5176\u4ed6goroutines\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2a\u7cfb\u7edf\u7ebf\u7a0b\u4e0a\u53bb\uff0c\u4ee5\u4f7f\u8fd9\u4e9bgoroutines\u4e0d\u963b\u585e<\/p>\n<ul>\n<li>\n<h2><strong>\u5f00\u542f\u591a\u6838\u7684\u5b9e\u9a8c<\/strong><\/h2>\n<\/li>\n<\/ul>\n<p>\u4ecd\u7136\u9700\u8981\u505a\u4e00\u4e2a\u5b9e\u9a8c\uff0c\u6765\u6d4b\u8bd5\u4e0b\u591a\u6838\u652f\u6301\u4e0bgoroutines\u7684\u5bf9\u539f\u751f\u7ebf\u7a0b\u7684\u5206\u914d, \u4e5f\u9a8c\u8bc1\u4e0b\u6211\u4eec\u6240\u5f97\u5230\u7684\u7ed3\u8bba\u201cgoroutine\u4e0d\u963b\u585e\u4e0d\u653e\u5f00CPU\u201d\u3002<\/p>\n<p>\u5b9e\u9a8c\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre>package main\r\n\r\nimport (\r\n   \"fmt\"\r\n   \"runtime\"\r\n)\r\n\r\nvar quit chan int = make(chan int)\r\n\r\nfunc loop(id int) { \/\/ id: \u8be5goroutine\u7684\u6807\u53f7\r\n    for i := 0; i &lt; 10; i++ { \/\/\u6253\u537010\u6b21\u8be5goroutine\u7684\u6807\u53f7\r\n        fmt.Printf(\"%d \", id)\r\n    }\r\n    quit &lt;- 0\r\n}\r\n\r\nfunc main() {\r\n    runtime.GOMAXPROCS(2) \/\/ \u6700\u591a\u540c\u65f6\u4f7f\u75282\u4e2a\u6838\r\n    for i := 0; i &lt; 3; i++ { \/\/\u5f00\u4e09\u4e2agoroutine\r\n       go loop(i)\r\n    }\r\n\r\n    for i := 0; i &lt; 3; i++ {\r\n       &lt;- quit\r\n    }\r\n}<\/pre>\n<p>\u591a\u8dd1\u51e0\u6b21\u4f1a\u770b\u5230\u7c7b\u4f3c\u8fd9\u4e9b\u8f93\u51fa(\u4e0d\u540c\u673a\u5668\u73af\u5883\u4e0d\u4e00\u6837):<\/p>\n<pre>0 0 0 0 0 1 1 0 0 1 0 0 1 0 1 2 1 2 1 2 1 2 1 2 1 2 2 2 2 2\r\n0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\r\n0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 1 0 1 2 1 2 1 2 2 2 2 2 2 2 2\r\n0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 2 0 2 0 2 2 2 2 2 2 2 2\r\n0 0 0 0 0 0 0 1 0 0 1 0 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 2 2<\/pre>\n<p>\u6267\u884c\u5b83\u6211\u4eec\u4f1a\u53d1\u73b0\u4ee5\u4e0b\u73b0\u8c61:<\/p>\n<p>\u6709\u65f6\u4f1a\u53d1\u751f\u62a2\u5360\u5f0f\u8f93\u51fa(\u8bf4\u660eGo\u5f00\u4e86\u4e0d\u6b62\u4e00\u4e2a\u539f\u751f\u7ebf\u7a0b\uff0c\u8fbe\u5230\u4e86\u771f\u6b63\u7684\u5e76\u884c)<br \/>\n\u6709\u65f6\u4f1a\u987a\u5e8f\u8f93\u51fa, \u6253\u5370\u5b8c0\u518d\u6253\u53701, \u518d\u6253\u53702(\u8bf4\u660eGo\u5f00\u4e00\u4e2a\u539f\u751f\u7ebf\u7a0b\uff0c\u5355\u7ebf\u7a0b\u4e0a\u7684goroutine\u4e0d\u963b\u585e\u4e0d\u677e\u5f00CPU)\uff0c\u90a3\u4e48\uff0c\u6211\u4eec\u8fd8\u4f1a\u89c2\u5bdf\u5230\u4e00\u4e2a\u73b0\u8c61\uff0c\u65e0\u8bba\u662f\u62a2\u5360\u5730\u8f93\u51fa\u8fd8\u662f\u987a\u5e8f\u7684\u8f93\u51fa\uff0c\u90fd\u4f1a\u6709\u90a3\u4e48\u4e24\u4e2a\u6570\u5b57\u8868\u73b0\u51fa\u8fd9\u6837\u7684\u73b0\u8c61:<\/p>\n<p>\u4e00\u4e2a\u6570\u5b57\u7684\u6240\u6709\u8f93\u51fa\u90fd\u4f1a\u5728\u53e6\u4e00\u4e2a\u6570\u5b57\u7684\u6240\u6709\u8f93\u51fa\u4e4b\u524d<\/p>\n<p>\u539f\u56e0\u662f\uff0c 3\u4e2agoroutine\u5206\u914d\u5230\u81f3\u591a2\u4e2a\u7ebf\u7a0b\u4e0a\uff0c\u5c31\u4f1a\u81f3\u5c11\u4e24\u4e2agoroutine\u5206\u914d\u5230\u540c\u4e00\u4e2a\u7ebf\u7a0b\u91cc\uff0c\u5355\u7ebf\u7a0b\u91cc\u7684goroutine \u4e0d\u963b\u585e\u4e0d\u653e\u5f00CPU, \u4e5f\u5c31\u53d1\u751f\u4e86\u987a\u5e8f\u8f93\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go\u8bed\u8a00\u7684\u5e76\u53d1\u548c\u5e76\u884c \u4e0d\u77e5\u9053\u4f60\u6709\u6ca1\u6709\u6ce8\u610f\u5230\u4e00\u4e2a\u73b0\u8c61\uff0c\u8fd8\u662f\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5982\u679c\u6211\u8dd1\u5728\u4e24\u4e2agoroutines\u91cc\u9762\u7684\u8bdd: var quit chan int = make(chan int) func loop() { \u00a0\u00a0\u00a0 for i := 0; i &lt; 10; i++ { \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fmt.Printf(&#8220;%d &#8220;, i) \u00a0\u00a0\u00a0 } \u00a0\u00a0\u00a0 quit &lt;- 0 } func main() { \u00a0\u00a0\u00a0 go loop()\u00a0 \/\/ \u5f00\u4e24\u4e2agoroutine\u8dd1\u51fd\u6570loop, loop\u51fd\u6570\u8d1f\u8d23\u6253\u537010\u4e2a\u6570 \u00a0\u00a0\u00a0 go loop() \u00a0\u00a0\u00a0 for i := 0; i &lt; 2; i++ { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-15453","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=15453"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15453\/revisions"}],"predecessor-version":[{"id":15455,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15453\/revisions\/15455"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}