{"id":947,"date":"2024-11-07T15:17:01","date_gmt":"2024-11-07T07:17:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/947\/"},"modified":"2024-11-07T15:17:01","modified_gmt":"2024-11-07T07:17:01","slug":"%e4%bd%bf%e7%94%a8channel%e9%98%bb%e5%a1%9e%e6%89%a7%e8%a1%8c%e6%97%b6%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88goroutine%e7%9a%84%e8%be%93%e5%87%ba%e4%bc%9a%e7%bc%ba%e5%a4%b1%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/947\/","title":{"rendered":"\u4f7f\u7528channel\u963b\u585e\u6267\u884c\u65f6\uff0c\u4e3a\u4ec0\u4e48goroutine\u7684\u8f93\u51fa\u4f1a\u7f3a\u5931\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-964\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173088792439364.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173088792439364.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173088792439364-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173088792439364-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173088792439364-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"\u4f7f\u7528channel\u963b\u585e\u6267\u884c\u65f6\uff0c\u4e3a\u4ec0\u4e48goroutine\u7684\u8f93\u51fa\u4f1a\u7f3a\u5931\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528channel\u963b\u585e\u6267\u884c\u65f6\uff0c\u4e3a\u4ec0\u4e48goroutine\u7684\u8f93\u51fa\u4f1a\u7f3a\u5931\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>goroutine\u4f7f\u7528channel\u963b\u585e\u6267\u884c\u65f6\u8f93\u51fa\u7f3a\u5931<\/strong><\/p>\n<p>\u5728\u5e76\u53d1\u7684goroutine\u7f16\u7a0b\u4e2d\uff0c\u4f7f\u7528channel\u8fdb\u884c\u540c\u6b65\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8f93\u51fa\u7f3a\u5931\u7684\u60c5\u51b5\u3002\u8fd9\u662f\u56e0\u4e3agoroutine\u7684\u6267\u884c\u987a\u5e8f\u5e76\u4e0d\u53d7\u4fdd\u8bc1\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff1a<\/p>\n<pre>func main() {\n    num1 := runtime.GOMAXPROCS(1)\n    num2 := runtime.GOMAXPROCS(1)\n    fmt.Println(\"num1:\", num1, \"num2:\", num2)\n    c := make(chan bool)\n    for j := 0; j &lt; 5; j++ {\n        go Go(c, j)\n    }\n    &lt;-c\n}\n\nfunc Go(c chan bool, index int) {\n    a := 1\n    for i := 0; i &lt; 100000000; i++ {\n        a += i\n    }\n    fmt.Println(index, a)\n    if index == 4 {\n        c &lt;- true\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528channel c\u6765\u963b\u585e\u6267\u884c\u3002\u9884\u671f\u8f93\u51fa\u5e94\u8be5\u6309\u987a\u5e8f\u4e3a0-4\u3002\u4f46\u662f\uff0c\u5728\u5b9e\u9645\u6267\u884c\u4e2d\uff0c\u4e2d\u95f4\u53ef\u80fd\u4f1a\u5b58\u5728\u7f3a\u5931\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u8fd9\u662f\u4ec0\u4e48\u539f\u56e0\u9020\u6210\u7684\u5462\uff1f<\/p>\n<p>\u534f\u7a0b\u4e4b\u95f4\u7684\u6267\u884c\u4e0d\u662f\u4fdd\u8bc1\u6709\u5e8f\u7684\u3002\u5f53\u8f93\u51fa\u51fd\u6570 println \u88ab\u8c03\u7528\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u534f\u7a0b\u5207\u6362\u3002\u5728 c \u901a\u9053\u88ab\u63a5\u6536\u4e4b\u524d\uff0c\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u534f\u7a0b\u6267\u884c println\uff0c\u5bfc\u81f4\u8f93\u51fa\u987a\u5e8f\u6df7\u4e71\uff0c\u4ece\u800c\u9020\u6210\u7f3a\u5931\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u4f7f\u7528channel\u8fdb\u884c\u963b\u585e\u6267\u884c\u65f6\uff0c\u9700\u8981\u6ce8\u610fgoroutine\u7684\u6267\u884c\u987a\u5e8f\u5e76\u4e0d\u786e\u5b9a\u3002\u4e3a\u4e86\u4fdd\u8bc1\u8f93\u51fa\u7684\u6b63\u786e\u987a\u5e8f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u9501\u673a\u5236\u6216\u5176\u4ed6\u4fdd\u8bc1\u987a\u5e8f\u7684\u540c\u6b65\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528channel\u963b\u585e\u6267\u884c\u65f6\uff0c\u4e3a\u4ec0\u4e48goroutine\u7684\u8f93\u51fa\u4f1a\u7f3a\u5931\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>goroutine\u4f7f\u7528channel\u963b\u585e\u6267\u884c\u65f6\u8f93\u51fa\u7f3a\u5931 \u5728\u5e76\u53d1\u7684goroutine\u7f16\u7a0b\u4e2d\uff0c\u4f7f\u7528channel\u8fdb\u884c\u540c\u6b65\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8f93\u51fa\u7f3a\u5931\u7684\u60c5\u51b5\u3002\u8fd9\u662f\u56e0\u4e3agoroutine\u7684\u6267\u884c\u987a\u5e8f\u5e76\u4e0d\u53d7\u4fdd\u8bc1\u3002 \u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff1a func main() { num1 := runtime.GOMAXPROCS(1) num2 := runtime.GOMAXPROCS(1) fmt.Println(&#8220;num1:&#8221;, num1, &#8220;num2:&#8221;, num2) c := make(chan bool) for j := 0; j &lt; 5; j++ { go Go(c, j) } &lt;-c } func Go(c chan bool, index int) { a := 1 for i := 0; i &lt; 100000000; i++ { a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-947","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/947","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=947"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/947\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}