{"id":667,"date":"2025-05-17T12:52:51","date_gmt":"2025-05-17T12:52:51","guid":{"rendered":"https:\/\/boochlin.com\/?p=667"},"modified":"2026-09-19T17:04:03","modified_gmt":"2026-09-19T17:04:03","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":"<p><a href=\"https:\/\/impateljay.medium.com\/coroutine-vs-thread-understanding-the-differences-4b98a5a22a3a\">\u53c3\u8003\u6587\u7ae0<\/a><\/p>\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<h2><strong>\u6210\u672c<\/strong>\u548c<strong>\u9069\u7528\u5834\u666f<\/strong><\/h2>\n<p>Thread \u662f\u300e\u91cd\u91cf\u7d1a\u300f\u3002Preemptive multitasking managed by the OS.<\/p>\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<ul>\n<li>\n<p><strong>\u91cd\u91cf\u7d1a (Heavyweight)\uff1a<\/strong> \u5efa\u7acb\u548c\u7ba1\u7406Thread\u53ef\u80fd\u76f8\u7576\u8017\u8cbb\u8cc7\u6e90\u3002<\/p>\n<\/li>\n<li>\n<p><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<\/p>\n<\/li>\n<li>\n<p><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<\/p>\n<\/li>\n<li>\n<p><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<\/p>\n<\/li>\n<\/ul>\n<p>Coroutine \u662f\u8f15\u91cf\u7d1a Cooperative multitasking<\/p>\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<ul>\n<li>\n<p><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<\/p>\n<\/li>\n<li>\n<p><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<\/p>\n<\/li>\n<li>\n<p><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<\/p>\n<\/li>\n<li>\n<p><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<\/p>\n<\/li>\n<\/ul>\n<h2><strong>\u9069\u7528\u5834\u666f\uff1a\u5225\u518d\u70ba CPU \u5bc6\u96c6\u4efb\u52d9\u96a8\u610f new Thread<\/strong><\/h2>\n<p>\u5f88\u591a\u4eba\u5728\u65e9\u671f Java \u6642\u4ee3\u5f62\u6210\u4e86\u4e00\u7a2e\u76f4\u89ba\uff1a\u300cI\/O \u963b\u585e\u7528\u5354\u7a0b\uff0cCPU \u7b97\u529b\u69a8\u6c41\u7528 Thread\u300d\u3002<br \/>\u4f46\u5728\u73fe\u4ee3 Android ART \u8207 Linux \u6392\u7a0b\u5668\u8996\u89d2\u4e0b\uff0c\u9019\u500b\u8a8d\u77e5\u9700\u8981\u7cbe\u6e96\u62c6\u89e3\u70ba\u4e09\u500b\u968e\u68af\uff1a<\/p>\n<h3>1. \u7d14 Kotlin \/ Java \u6f14\u7b97\u6cd5\uff1a\u9996\u9078 <code>Dispatchers.Default<\/code><\/h3>\n<p>\u5982\u679c\u4f60\u5728\u7d14 Kotlin \u88e1\u8dd1\u5927\u77e9\u9663\u8a08\u7b97\u3001\u6df1\u5ea6 JSON \u53cd\u5e8f\u5217\u5316\uff0c\u5343\u842c\u4e0d\u8981\u81ea\u5df1\u624b\u52d5 <code>new Thread<\/code>\uff01<br \/>\u6bcf\u958b\u4e00\u689d Java Thread \u90fd\u6703\u541e\u566c 1MB \u865b\u64ec\u7dda\u7a0b\u68e7\uff1b\u7576\u7dda\u7a0b\u6578\u8d85\u904e CPU \u6838\u5fc3\u6578\uff0cLinux CFS \u6392\u7a0b\u5668\u6703\u767c\u52d5\u983b\u7e41\u7684\u6642\u9593\u7247\u6436\u4f54\uff0c<strong>\u6d17\u5237 L1\/L2 \u5feb\u53d6\uff08Cache Thrashing\uff09<\/strong>\uff0c\u7b97\u529b\u5168\u6d6a\u8cbb\u5728 Context Switch \u4e0a\u3002<br \/><code>Dispatchers.Default<\/code> \u5e95\u5c64\u662f Work-Stealing \u7dda\u7a0b\u6c60\uff0c\u628a\u6d3b\u8e8d\u7dda\u7a0b\u9396\u6b7b\u5728 CPU \u5be6\u9ad4\u6838\u5fc3\u6578\uff0c\u4fdd\u8b49 CPU \u6d41\u6c34\u7dda\u6c38\u9060\u5403\u98fd\u4e14 Context Switch \u8da8\u8fd1\u65bc\u96f6\uff1a<\/p>\n<pre><code class=\"hljs language-kotlin\"><span class=\"hljs-keyword\">suspend<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">fun<\/span> <span class=\"hljs-title\">parseMassiveDataset<\/span><span class=\"hljs-params\">(rawBytes: <span class=\"hljs-type\">ByteArray<\/span>)<\/span><\/span>: ProcessedResult =\n    withContext(Dispatchers.Default) {\n        <span class=\"hljs-comment\">\/\/ \u8ff4\u5708\u4e2d\u914d\u5408 ensureActive() \u652f\u63f4\u5408\u4f5c\u5f0f\u53d6\u6d88\uff0c\u907f\u514d\u5b64\u9b42\u7dda\u7a0b\u72c2\u71d2 CPU<\/span>\n        ensureActive()\n        heavyCalculationCore(rawBytes)\n    }<\/code><\/pre>\n<h3>2. JNI \u5bc6\u96c6\u8a08\u7b97\u8207 C++ \u786c\u9ad4\u7d81\u6838\uff1a\u5c08\u5c6c Dedicated Thread<\/h3>\n<p>\u4ec0\u9ebc\u6642\u5019\u624d\u8f2a\u5230 Thread \u51fa\u5834\uff1f<strong>\u7576\u4efb\u52d9\u727d\u6d89\u9577\u6642\u9593\u7684 C++ Native \u904b\u7b97\u6642<\/strong>\u3002<br \/>\u82e5\u4e00\u6bb5\u8017\u6642 500ms \u7684\u5f71\u50cf\u6f14\u7b97\u6cd5\u76f4\u63a5\u4e1f\u9032 <code>Dispatchers.Default<\/code>\uff0c\u5728 4 \u6838\u5fc3\uff08\u5982\u9ad8\u901a QM215\uff09\u5f31\u677f\u5b50\u4e0a\uff0c4 \u500b\u4efb\u52d9\u540c\u6642\u9032\u4f86\u5c31\u6703\u5f15\u767c\u300c\u5168\u57df\u5354\u7a0b\u6c60\u98e2\u9913\uff08Starvation\uff09\u300d\uff0c\u4e3b\u7dda\u7a0b\u8207\u5176\u4ed6\u8f15\u91cf\u5354\u7a0b\u5168\u90e8\u5361\u6b7b\u3002<br \/>\u6b64\u6642\u5fc5\u9808\u4f7f\u7528\u7368\u7acb\u5c08\u5c6c\u7684\u7dda\u7a0b\u6c60\uff0c\u4e26\u5728 Native \u7aef\u8abf\u7528 <code>pthread_setaffinity_np<\/code> \u5c07\u7dda\u7a0b\u6b7b\u6b7b\u91d8\u5728\u5be6\u9ad4\u5927\u6838\u4e0a\uff0c\u9632\u6b62\u8de8\u6838\u5fc3\u6f02\u79fb\u5c0e\u81f4\u5feb\u53d6\u5931\u6548\u3002<\/p>\n<h3>3. \u9ad8\u4f75\u767c I\/O \u5bc6\u96c6\u578b\u4efb\u52d9\uff1aCoroutine \u7684\u7d55\u5c0d\u4e3b\u5834<\/h3>\n<p>Web \u8acb\u6c42\u3001\u672c\u5730 SQLite \/ Room \u8b80\u5beb\u3001\u8cc7\u6599\u5eab\u9023\u7dda\uff0c99% \u7684\u6642\u9593\u90fd\u5728\u7b49\u7db2\u8def\u6216\u78c1\u789f I\/O\uff0c\u9019\u6642 Coroutine \u80fd\u7528\u5fae\u79d2\u7d1a\u7684\u4ee3\u50f9\u5728\u639b\u8d77\u9ede\u4e3b\u52d5\u8b93\u51fa CPU\uff0c\u4ee5\u6975\u4f4e\u958b\u92b7\u652f\u6490\u6210\u5343\u4e0a\u842c\u7684\u4efb\u52d9\u4e26\u767c\u3002<\/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  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1223,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[202,247,248],"_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":10,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":1546,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/667\/revisions\/1546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/1223"}],"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}]}}