{"id":667,"date":"2025-05-17T12:52:51","date_gmt":"2025-05-17T12:52:51","guid":{"rendered":"https:\/\/boochlin.com\/?p=667"},"modified":"2025-11-12T12:18:05","modified_gmt":"2025-11-12T12:18:05","slug":"thread-%e5%92%8c-coroutine-%e7%9a%84%e7%90%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=667","title":{"rendered":"Thread \u548c Coroutine \u7684\u7406\u89e3"},"content":{"rendered":"\n<p><a href=\"https:\/\/impateljay.medium.com\/coroutine-vs-thread-understanding-the-differences-4b98a5a22a3a\">\u53c3\u8003\u6587\u7ae0<\/a><\/p>\n\n\n\n<p>\u7c21\u55ae\u4f86\u8aaa\uff0c<strong>Thread (\u57f7\u884c\u7dd2) \u662f\u7531\u4f5c\u696d\u7cfb\u7d71 (OS) \u4f86\u7ba1\u7406\u548c\u8abf\u5ea6\u7684<\/strong>\uff0c\u800c <strong>Coroutine (\u5354\u7a0b) \u7684\u63a7\u5236\u6b0a\u5247\u5728\u6211\u5011\u958b\u767c\u8005\u624b\u4e0a<\/strong>\uff0c\u7531\u7a0b\u5f0f\u8a9e\u8a00\u7684\u57f7\u884c\u74b0\u5883 (Runtime) \u5728\u4f7f\u7528\u8005\u5c64\u7d1a\u9032\u884c\u8abf\u5ea6\u3002\u9019\u500b\u6839\u672c\u6027\u7684\u4e0d\u540c\uff0c\u5c0e\u81f4\u4e86\u5b83\u5011\u5728\u8cc7\u6e90\u6d88\u8017\u548c\u6548\u80fd\u4e0a\u6709\u8457\u5929\u58e4\u4e4b\u5225\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6210\u672c<\/strong>\u548c<strong>\u9069\u7528\u5834\u666f<\/strong><\/h2>\n\n\n\n<p>Thread \u662f\u300e\u91cd\u91cf\u7d1a\u300f\u3002Preemptive multitasking managed by the OS.<\/p>\n\n\n\n<p>\u6bcf\u5efa\u7acb\u4e00\u500b Thread\uff0cOS \u90fd\u9700\u8981\u70ba\u5b83\u5206\u914d\u7368\u7acb\u7684\u3001MB \u7b49\u7d1a\u7684\u8a18\u61b6\u9ad4\u5806\u758a\u3002\u66f4\u81f4\u547d\u7684\u662f\u5b83\u7684\u300e\u4e0a\u4e0b\u6587\u5207\u63db\u300f\u6210\u672c\u3002\u7576 OS \u6c7a\u5b9a\u5f9e Thread A \u5207\u63db\u5230 Thread B \u6642\uff0c\u9700\u8981\u6df1\u5165\u6838\u5fc3\u6a21\u5f0f (Kernel Mode)\uff0c\u4fdd\u5b58 A \u7684\u6240\u6709\u73fe\u5834\uff08CPU \u66ab\u5b58\u5668\u3001\u5b8c\u6574\u5806\u758a\u7b49\uff09\uff0c\u518d\u8f09\u5165 B \u7684\u73fe\u5834\u3002\u9019\u6574\u500b\u904e\u7a0b\u975e\u5e38\u8017\u6642\uff0c\u662f\u5fae\u79d2 (\u00b5s) \u7b49\u7d1a\u7684\u3002<\/p>\n\n\n\n<ul>\n<li><strong>\u91cd\u91cf\u7d1a (Heavyweight)\uff1a<\/strong> \u5efa\u7acb\u548c\u7ba1\u7406Thread\u53ef\u80fd\u76f8\u7576\u8017\u8cbb\u8cc7\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u963b\u585e (Blocking)\uff1a<\/strong> Thread\u53ef\u80fd\u6703\u4e92\u76f8\u963b\u585e\uff0c\u7279\u5225\u662f\u7576\u5b83\u5011\u5728\u7b49\u5f85\u8cc7\u6e90\u6216 I\/O \u64cd\u4f5c\u6642\u3002<\/li>\n\n\n\n<li><strong>\u4e0a\u4e0b\u6587\u5207\u63db (Context Switching)\uff1a<\/strong> \u5728Thread\u4e4b\u9593\u5207\u63db\u9700\u8981\u5132\u5b58\u548c\u8f09\u5165\u5b83\u5011\u7684\u72c0\u614b\uff0c\u9019\u53ef\u80fd\u6703\u5f88\u8017\u8cbb\u6210\u672c\u3002<\/li>\n\n\n\n<li><strong>\u7531\u4f5c\u696d\u7cfb\u7d71\u7ba1\u7406 (OS Managed)\uff1a<\/strong> Thread \u7531\u4f5c\u696d\u7cfb\u7d71\u7ba1\u7406\uff0c\u9019\u53ef\u80fd\u6703\u589e\u52a0\u984d\u5916\u958b\u92b7\u3002<\/li>\n<\/ul>\n\n\n\n<p>Coroutine \u662f\u8f15\u91cf\u7d1a Cooperative multitasking<\/p>\n\n\n\n<p>\u5b83\u6d3b\u5728 Thread \u5167\u90e8\uff0c\u50cf\u662f\u4e00\u7a2e\u5408\u4f5c\u95dc\u4fc2\u3002\u5efa\u7acb\u4e00\u500b Coroutine \u7684\u6210\u672c\u6975\u4f4e\uff0c\u53ea\u4f54\u7528 KB \u7b49\u7d1a\u7684\u8a18\u61b6\u9ad4\u3002\u5b83\u7684\u5207\u63db\uff0c\u662f\u7a0b\u5f0f\u81ea\u5df1\u6c7a\u5b9a\u7684\uff0c\u901a\u5e38\u5728await\u9019\u7a2e\u7b49\u5f85 I\/O \u7684\u5730\u65b9\uff0c\u4e3b\u52d5\u8b93\u51fa\u57f7\u884c\u6b0a\u3002\u9019\u500b\u5207\u63db\u5b8c\u5168\u5728\u4f7f\u7528\u8005\u6a21\u5f0f (User Mode) \u5167\u90e8\u5b8c\u6210\uff0c\u5c31\u50cf\u4e00\u6b21\u51fd\u5f0f\u547c\u53eb\uff0c\u6210\u672c\u662f\u5948\u79d2 (ns) \u7b49\u7d1a\u7684\uff0c\u6bd4 Thread \u5207\u63db\u5feb\u4e86\u4e0a\u5343\u500d\u3002<\/p>\n\n\n\n<ul>\n<li><strong>\u8f15\u91cf\u7d1a (Lightweight)\uff1a<\/strong> Coroutine\u6bd4Thread\u8f15\u5f97\u591a\uff1b\u4f60\u53ef\u4ee5\u5efa\u7acb\u6578\u5343\u500bCoroutine\u800c\u4e0d\u6703\u6709\u986f\u8457\u7684\u958b\u92b7\u3002<\/li>\n\n\n\n<li><strong>\u975e\u963b\u585e (Non-Blocking)\uff1a<\/strong> Coroutine\u4f7f\u7528\u66ab\u505c\u51fd\u5f0f (suspending functions) \u4f86\u8b93\u51fa\u63a7\u5236\u6b0a\u800c\u4e0d\u963b\u585e\u57f7\u884c\u7dd2\uff0c\u5f9e\u800c\u6709\u6548\u5229\u7528\u8cc7\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u8f03\u5c11\u958b\u92b7 (Less Overhead)\uff1a<\/strong> \u8207Thread\u76f8\u6bd4\uff0cCoroutine\u4e4b\u9593\u7684\u4e0a\u4e0b\u6587\u5207\u63db\u66f4\u5feb\u4e14\u8cc7\u6e90\u6d88\u8017\u66f4\u5c11\u3002<\/li>\n\n\n\n<li><strong>\u7531\u958b\u767c\u8005\u63a7\u5236 (Controlled by Developer)\uff1a<\/strong> Coroutine\u63d0\u4f9b\u4e86\u5c0d\u4efb\u52d9\u57f7\u884c\u7684\u66f4\u7cbe\u7d30\u63a7\u5236\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u9069\u7528\u5834\u666f<\/strong>\u3002\u57fa\u65bc\u4e0a\u9762\u7684\u6210\u672c\u5dee\u7570\uff0c\u6211\u7684\u9078\u64c7\u6703\u975e\u5e38\u770b\u91cd\u61c9\u7528\u5834\u666f\u3002<\/p>\n\n\n\n<ul>\n<li>\u300c\u5c0d\u65bc <strong>CPU \u5bc6\u96c6\u578b<\/strong>\u4efb\u52d9\uff0c\u6bd4\u5982\u5f71\u7247\u8f49\u6a94\u3001\u8907\u96dc\u904b\u7b97\uff0c\u6211\u6703\u6beb\u4e0d\u7336\u8c6b\u5730\u4f7f\u7528 Thread\u3002\u56e0\u70ba\u6211\u9700\u8981\u771f\u6b63\u5229\u7528\u597d\u591a\u6838\u5fc3 CPU \u7684\u5e73\u884c\u8655\u7406\u80fd\u529b\uff0c\u628a\u904b\u7b97\u80fd\u529b\u69a8\u4e7e\u3002\u9019\u6642\u5019\u5207\u63db\u6210\u672c\u76f8\u5c0d\u6b21\u8981\u3002\u300d<\/li>\n\n\n\n<li>\u300c\u4f46\u5c0d\u65bc<strong>\u9ad8\u4f75\u767c I\/O \u5bc6\u96c6\u578b<\/strong>\u4efb\u52d9\uff0c\u6bd4\u5982 Web \u4f3a\u670d\u5668\u3001\u8cc7\u6599\u5eab\u9023\u63a5\u6c60\uff0c\u6211\u4e00\u5b9a\u6703\u9078\u64c7 Coroutine\u3002\u5728\u9019\u7a2e\u5834\u666f\u4e0b\uff0c\u7a0b\u5f0f\u6709\u6210\u5343\u4e0a\u842c\u7684\u4efb\u52d9\uff0c\u4f46\u7d55\u5927\u90e8\u5206\u90fd\u5728\u7b49\u5f85\u7db2\u8def\u6216\u78c1\u789f\u56de\u61c9\u3002\u5982\u679c\u7528 Thread\uff0c\u90a3\u5149\u662f\u5207\u63db\u7684\u6210\u672c\u5c31\u6703\u628a\u7cfb\u7d71\u62d6\u57ae\u3002\u800c Coroutine \u80fd\u7528\u6975\u4f4e\u7684\u4ee3\u50f9\u5728\u9019\u4e9b\u7b49\u5f85\u7684\u4efb\u52d9\u9593\u5feb\u901f\u5207\u63db\uff0c\u6700\u5927\u5316 CPU \u7684\u5229\u7528\u7387\u3002\u300d<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c3\u8003\u6587\u7ae0 \u7c21\u55ae\u4f86\u8aaa\uff0cThread (\u57f7\u884c\u7dd2) \u662f\u7531\u4f5c\u696d\u7cfb\u7d71 (OS) \u4f86\u7ba1\u7406\u548c\u8abf\u5ea6\u7684\uff0c\u800c Coroutine (\u5354\u7a0b) \u7684\u63a7\u5236\u6b0a\u5247\u5728\u6211\u5011\u958b\u767c\u8005\u624b\u4e0a\uff0c\u7531\u7a0b\u5f0f\u8a9e\u8a00\u7684\u57f7\u884c\u74b0\u5883 (Runtime) \u5728\u4f7f\u7528\u8005\u5c64&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/667"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=667"}],"version-history":[{"count":5,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/667\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}