{"id":517,"date":"2025-06-18T19:13:55","date_gmt":"2025-06-18T11:13:55","guid":{"rendered":"https:\/\/trantor.ink\/?p=517"},"modified":"2025-06-24T16:50:00","modified_gmt":"2025-06-24T08:50:00","slug":"517","status":"publish","type":"post","link":"https:\/\/trantor.ink\/?p=517","title":{"rendered":"Kotlin \u534f\u7a0b\u539f\u7406\u6df1\u5ea6\u89e3\u6790\uff1a\u4ece\u7f16\u8bd1\u5668\u9b54\u6cd5\u5230\u7ed3\u6784\u5316\u5e76\u53d1"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/trantor.ink\/?p=517\/#%E5%BC%95%E8%A8%80\" >\u5f15\u8a00<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/trantor.ink\/?p=517\/#%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E7%9A%84%E6%8C%81%E4%B9%85%E6%8C%91%E6%88%98\" >\u5f02\u6b65\u7f16\u7a0b\u7684\u6301\u4e45\u6311\u6218<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/trantor.ink\/?p=517\/#Kotlin_%E5%8D%8F%E7%A8%8B%EF%BC%9A%E4%B8%80%E6%AC%A1%E8%8C%83%E5%BC%8F%E8%BD%AC%E7%A7%BB\" >Kotlin \u534f\u7a0b\uff1a\u4e00\u6b21\u8303\u5f0f\u8f6c\u79fb<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/trantor.ink\/?p=517\/#%E7%AC%AC%E4%B8%80%E8%8A%82%EF%BC%9A%E5%9F%BA%E6%9C%AC%E6%8A%BD%E8%B1%A1%EF%BC%9A%E6%8C%82%E8%B5%B7%EF%BC%88Suspension%EF%BC%89\" >\u7b2c\u4e00\u8282\uff1a\u57fa\u672c\u62bd\u8c61\uff1a\u6302\u8d77\uff08Suspension\uff09<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/trantor.ink\/?p=517\/#11_%E8%B6%85%E8%B6%8A%E7%BA%BF%E7%A8%8B%EF%BC%9A%E5%8D%8F%E7%A8%8B%E7%9A%84%E8%BD%BB%E9%87%8F%E7%BA%A7%E6%9C%AC%E8%B4%A8\" >1.1 \u8d85\u8d8a\u7ebf\u7a0b\uff1a\u534f\u7a0b\u7684\u8f7b\u91cf\u7ea7\u672c\u8d28<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/trantor.ink\/?p=517\/#12_%E6%8C%82%E8%B5%B7%E6%9C%BA%E5%88%B6_vs_%E9%98%BB%E5%A1%9E%E7%9A%84%E4%BD%8E%E6%95%88\" >1.2 \u6302\u8d77\u673a\u5236 vs. \u963b\u585e\u7684\u4f4e\u6548<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/trantor.ink\/?p=517\/#13_%E4%BB%8E%E5%9B%9E%E8%B0%83%E5%9C%B0%E7%8B%B1%E5%88%B0%E9%A1%BA%E5%BA%8F%E6%B8%85%E6%99%B0%EF%BC%9A%E4%BB%A3%E7%A0%81%E5%8F%AF%E8%AF%BB%E6%80%A7%E7%9A%84%E8%8C%83%E5%BC%8F%E8%BD%AC%E7%A7%BB\" >1.3 \u4ece\u56de\u8c03\u5730\u72f1\u5230\u987a\u5e8f\u6e05\u6670\uff1a\u4ee3\u7801\u53ef\u8bfb\u6027\u7684\u8303\u5f0f\u8f6c\u79fb<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/trantor.ink\/?p=517\/#%E7%AC%AC%E4%BA%8C%E8%8A%82%EF%BC%9A%E6%B7%B1%E5%85%A5%E5%BA%95%E5%B1%82%EF%BC%9Asuspend_%E5%87%BD%E6%95%B0%E7%9A%84%E7%BC%96%E8%AF%91%E5%99%A8%E8%BD%AC%E6%8D%A2\" >\u7b2c\u4e8c\u8282\uff1a\u6df1\u5165\u5e95\u5c42\uff1asuspend \u51fd\u6570\u7684\u7f16\u8bd1\u5668\u8f6c\u6362<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/trantor.ink\/?p=517\/#21_%E7%BB%AD%E4%BD%93%E4%BC%A0%E9%80%92%E9%A3%8E%E6%A0%BC_Continuation-Passing_Style_CPS%EF%BC%9A%E7%A7%98%E5%AF%86%E6%AD%A6%E5%99%A8\" >2.1 \u7eed\u4f53\u4f20\u9012\u98ce\u683c (Continuation-Passing Style, CPS)\uff1a\u79d8\u5bc6\u6b66\u5668<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/trantor.ink\/?p=517\/#22_Continuation_%E6%8E%A5%E5%8F%A3%EF%BC%9A%E4%B8%80%E4%BB%BD%E6%81%A2%E5%A4%8D%E6%89%A7%E8%A1%8C%E7%9A%84%E5%A5%91%E7%BA%A6\" >2.2 Continuation \u63a5\u53e3\uff1a\u4e00\u4efd\u6062\u590d\u6267\u884c\u7684\u5951\u7ea6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/trantor.ink\/?p=517\/#23_%E7%94%9F%E6%88%90%E7%9A%84%E7%8A%B6%E6%80%81%E6%9C%BA%EF%BC%9Asuspend_%E5%87%BD%E6%95%B0%E7%9A%84%E7%9C%9F%E5%AE%9E%E5%BD%A2%E6%80%81\" >2.3 \u751f\u6210\u7684\u72b6\u6001\u673a\uff1asuspend \u51fd\u6570\u7684\u771f\u5b9e\u5f62\u6001<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/trantor.ink\/?p=517\/#24_COROUTINE_SUSPENDED_%E6%9A%82%E5%81%9C%E7%9A%84%E4%BF%A1%E5%8F%B7\" >2.4 COROUTINE_SUSPENDED: \u6682\u505c\u7684\u4fe1\u53f7<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/trantor.ink\/?p=517\/#25_%E4%B8%80%E6%AC%A1%E6%8C%82%E8%B5%B7%E8%B0%83%E7%94%A8%E7%9A%84%E5%AE%8C%E6%95%B4%E8%BF%BD%E8%B8%AA\" >2.5 \u4e00\u6b21\u6302\u8d77\u8c03\u7528\u7684\u5b8c\u6574\u8ffd\u8e2a<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/trantor.ink\/?p=517\/#%E7%AC%AC%E4%B8%89%E8%8A%82%EF%BC%9A%E6%9E%B6%E6%9E%84%E6%94%AF%E6%9F%B1%EF%BC%9A%E7%BB%93%E6%9E%84%E5%8C%96%E5%B9%B6%E5%8F%91\" >\u7b2c\u4e09\u8282\uff1a\u67b6\u6784\u652f\u67f1\uff1a\u7ed3\u6784\u5316\u5e76\u53d1<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/trantor.ink\/?p=517\/#31_%E7%BB%93%E6%9E%84%E5%8C%96%E5%B9%B6%E5%8F%91%E7%9A%84%E8%B5%B7%E6%BA%90\" >3.1 \u7ed3\u6784\u5316\u5e76\u53d1\u7684\u8d77\u6e90<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/trantor.ink\/?p=517\/#32_CoroutineScope_%E4%B8%8E_Job_%E5%B1%82%E7%BA%A7%E7%BB%93%E6%9E%84\" >3.2 CoroutineScope \u4e0e Job \u5c42\u7ea7\u7ed3\u6784<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/trantor.ink\/?p=517\/#33_%E5%8F%AF%E9%A2%84%E6%B5%8B%E7%9A%84%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%EF%BC%9A%E5%BC%82%E5%B8%B8%E4%BC%A0%E6%92%AD\" >3.3 \u53ef\u9884\u6d4b\u7684\u9519\u8bef\u5904\u7406\uff1a\u5f02\u5e38\u4f20\u64ad<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/trantor.ink\/?p=517\/#34_%E4%BC%98%E9%9B%85%E7%9A%84%E7%BB%88%E6%AD%A2%EF%BC%9A%E5%8F%96%E6%B6%88%E4%BC%A0%E6%92%AD\" >3.4 \u4f18\u96c5\u7684\u7ec8\u6b62\uff1a\u53d6\u6d88\u4f20\u64ad<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/trantor.ink\/?p=517\/#%E7%AC%AC%E5%9B%9B%E8%8A%82%EF%BC%9A%E6%89%A7%E8%A1%8C%E6%A1%86%E6%9E%B6%EF%BC%9ACoroutineContext_%E4%B8%8E_Dispatcher\" >\u7b2c\u56db\u8282\uff1a\u6267\u884c\u6846\u67b6\uff1aCoroutineContext \u4e0e Dispatcher<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/trantor.ink\/?p=517\/#41_CoroutineContext%EF%BC%9A%E5%8D%8F%E7%A8%8B%E7%9A%84%E2%80%9C%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E2%80%9D\" >4.1 CoroutineContext\uff1a\u534f\u7a0b\u7684\u201c\u73af\u5883\u53d8\u91cf\u201d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/trantor.ink\/?p=517\/#42_CoroutineDispatcher%EF%BC%9A%E6%89%A7%E8%A1%8C%E7%9A%84%E5%BC%95%E6%93%8E\" >4.2 CoroutineDispatcher\uff1a\u6267\u884c\u7684\u5f15\u64ce<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/trantor.ink\/?p=517\/#43_%E6%8E%8C%E6%8F%A1%E7%BA%BF%E7%A8%8B%E8%B0%83%E5%BA%A6%EF%BC%9ADefault_IO_Main_%E4%B8%8E_Unconfined\" >4.3 \u638c\u63e1\u7ebf\u7a0b\u8c03\u5ea6\uff1aDefault, IO, Main \u4e0e Unconfined<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/trantor.ink\/?p=517\/#44_withContext_%E5%87%BD%E6%95%B0%EF%BC%9A%E7%B2%BE%E7%BB%86%E5%8C%96%E7%9A%84%E7%BA%BF%E7%A8%8B%E6%8E%A7%E5%88%B6\" >4.4 withContext \u51fd\u6570\uff1a\u7cbe\u7ec6\u5316\u7684\u7ebf\u7a0b\u63a7\u5236<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/trantor.ink\/?p=517\/#%E7%AC%AC%E4%BA%94%E8%8A%82%EF%BC%9A%E5%B9%B6%E5%8F%91%E5%9B%BE%E6%99%AF%E4%B8%AD%E7%9A%84%E6%AF%94%E8%BE%83%E5%88%86%E6%9E%90\" >\u7b2c\u4e94\u8282\uff1a\u5e76\u53d1\u56fe\u666f\u4e2d\u7684\u6bd4\u8f83\u5206\u6790<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/trantor.ink\/?p=517\/#51_%E5%8D%8F%E7%A8%8B_vs_%E5%93%8D%E5%BA%94%E5%BC%8F%E6%B5%81_RxJava%EF%BC%9A%E8%8C%83%E5%BC%8F%E4%B9%8B%E4%BA%89\" >5.1 \u534f\u7a0b vs. \u54cd\u5e94\u5f0f\u6d41 (RxJava)\uff1a\u8303\u5f0f\u4e4b\u4e89<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/trantor.ink\/?p=517\/#52_%E5%8D%8F%E7%A8%8B_vs_CompletableFuture%EF%BC%9A%E7%8E%B0%E4%BB%A3%E7%AE%80%E6%B4%81%E6%80%A7%E4%B8%8E%E4%BC%A0%E7%BB%9F%E8%83%BD%E5%8A%9B%E7%9A%84%E5%AF%B9%E6%AF%94\" >5.2 \u534f\u7a0b vs. CompletableFuture\uff1a\u73b0\u4ee3\u7b80\u6d01\u6027\u4e0e\u4f20\u7edf\u80fd\u529b\u7684\u5bf9\u6bd4<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/trantor.ink\/?p=517\/#%E7%BB%93%E8%AE%BA\" >\u7ed3\u8bba<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/trantor.ink\/?p=517\/#%E5%BC%95%E7%94%A8%E7%9A%84%E8%91%97%E4%BD%9C\" >\u5f15\u7528\u7684\u8457\u4f5c<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BC%95%E8%A8%80\"><\/span><strong>\u5f15\u8a00<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E7%9A%84%E6%8C%81%E4%B9%85%E6%8C%91%E6%88%98\"><\/span><strong>\u5f02\u6b65\u7f16\u7a0b\u7684\u6301\u4e45\u6311\u6218<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u65e0\u8bba\u662f\u6784\u5efa\u670d\u52a1\u5668\u7aef\u5e94\u7528\u3001\u684c\u9762\u8f6f\u4ef6\u8fd8\u662f\u79fb\u52a8\u5e94\u7528\uff0c\u5f02\u6b65\u975e\u963b\u585e\u7f16\u7a0b\u90fd\u5df2\u6210\u4e3a\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206 <sup>1<\/sup>\u3002\u5176\u6838\u5fc3\u76ee\u6807\u5728\u4e8e\u89e3\u51b3\u4e00\u4e2a\u6839\u672c\u6027\u7684\u77db\u76fe\uff1a\u5982\u4f55\u5728\u6267\u884c\u53ef\u80fd\u8017\u65f6\u7684\u64cd\u4f5c\uff08\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6 I\/O \u6216\u590d\u6742\u7684\u8ba1\u7b97\uff09\u65f6\uff0c\u65e2\u80fd\u4fdd\u8bc1\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u754c\u9762\u6d41\u7545\u54cd\u5e94\uff0c\u53c8\u80fd\u5b9e\u73b0\u7cfb\u7edf\u8d44\u6e90\u7684\u9ad8\u6548\u5229\u7528\u4e0e\u53ef\u4f38\u7f29\u6027 <sup>3<\/sup>\u3002\u5386\u53f2\u4e0a\uff0c\u5f00\u53d1\u8005\u4eec\u91c7\u7528\u4e86\u591a\u79cd\u6280\u672f\u6765\u5e94\u5bf9\u8fd9\u4e00\u6311\u6218\uff0c\u5305\u62ec\u76f4\u63a5\u4f7f\u7528\u7ebf\u7a0b\uff08Threading\uff09\u3001\u56de\u8c03\u51fd\u6570\uff08Callbacks\uff09\u4ee5\u53ca\u8bf8\u5982<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Future \u6216 Promise \u4e4b\u7c7b\u7684\u62bd\u8c61\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u4f20\u7edf\u65b9\u6cd5\u65e0\u4e00\u4f8b\u5916\u5730\u5e26\u6765\u4e86\u81ea\u8eab\u7684\u590d\u6742\u6027\uff1a\u539f\u751f\u7ebf\u7a0b\u662f\u91cd\u91cf\u7ea7\u8d44\u6e90\uff0c\u5176\u521b\u5efa\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u6210\u672c\u9ad8\u6602\uff0c\u4e14\u96be\u4ee5\u7ba1\u7406 <sup>4<\/sup>\uff1b\u56de\u8c03\u51fd\u6570\u5219\u6781\u6613\u5bfc\u81f4\u201c\u56de\u8c03\u5730\u72f1\u201d\uff08Callback Hell\uff09\uff0c\u4f7f\u5f97\u4ee3\u7801\u903b\u8f91\u96be\u4ee5\u8ffd\u8e2a\u548c\u7ef4\u62a4 <sup>4<\/sup>\uff1b\u800c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Future\/Promise \u6a21\u578b\u867d\u7136\u6709\u6240\u6539\u8fdb\uff0c\u4f46\u4ecd\u5c06\u5f00\u53d1\u8005\u5f15\u5165\u4e86\u4e00\u79cd\u94fe\u5f0f\u8c03\u7528\u7684\u7f16\u7a0b\u8303\u5f0f\uff0c\u4f7f\u5f97\u9519\u8bef\u5904\u7406\u548c\u63a7\u5236\u6d41\u53d8\u5f97\u590d\u6742 <sup>4<\/sup>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Kotlin_%E5%8D%8F%E7%A8%8B%EF%BC%9A%E4%B8%80%E6%AC%A1%E8%8C%83%E5%BC%8F%E8%BD%AC%E7%A7%BB\"><\/span><strong>Kotlin \u534f\u7a0b\uff1a\u4e00\u6b21\u8303\u5f0f\u8f6c\u79fb<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6b64\u80cc\u666f\u4e0b\uff0cKotlin \u534f\u7a0b\u7684\u51fa\u73b0\u5e76\u975e\u4ec5\u4ec5\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u5e93\uff0c\u800c\u662f\u5e26\u6765\u4e86\u4e00\u573a\u6839\u672c\u6027\u7684\u8303\u5f0f\u8f6c\u79fb\u3002\u5b83\u662f\u4e00\u79cd\u5728\u8bed\u8a00\u5c42\u9762\u548c\u7f16\u8bd1\u5668\u5c42\u9762\u6df1\u5ea6\u652f\u6301\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u65e8\u5728\u4ece\u6839\u672c\u4e0a\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b <sup>1<\/sup>\u3002\u534f\u7a0b\u7684\u6838\u5fc3\u627f\u8bfa\u662f\uff1a\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u4ee5\u7f16\u5199\u540c\u6b65\u3001\u987a\u5e8f\u4ee3\u7801\u7684\u76f4\u89c2\u65b9\u5f0f\uff0c\u6765\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u3001\u975e\u963b\u585e\u7a0b\u5e8f <sup>4<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u62a5\u544a\u65e8\u5728\u5bf9 Kotlin \u534f\u7a0b\u8fdb\u884c\u4e00\u6b21\u5168\u9762\u800c\u6df1\u5165\u7684\u539f\u7406\u5256\u6790\uff0c\u63ed\u793a\u5176\u201c\u9b54\u6cd5\u201d\u80cc\u540e\u7684\u6280\u672f\u5b9e\u8d28\u3002\u6211\u4eec\u5c06\u63a2\u8ba8\u534f\u7a0b\u7684\u529b\u91cf\u6e90\u6cc9\u2014\u2014\u5b83\u5e76\u975e\u6765\u81ea\u5355\u4e00\u7684\u7279\u6027\uff0c\u800c\u662f\u4e24\u5927\u521b\u65b0\u7684\u534f\u540c\u4f5c\u7528\uff1a\u5176\u4e00\uff0c\u662f<strong>\u7531\u7f16\u8bd1\u5668\u9a71\u52a8\u7684\u6302\u8d77\uff08Suspension\uff09\u673a\u5236<\/strong>\uff0c\u5b83\u901a\u8fc7\u7cbe\u5de7\u7684\u4ee3\u7801\u8f6c\u6362\uff08Continuation-Passing Style\uff09\u5b9e\u73b0\u4e86\u975e\u963b\u585e\u7b49\u5f85\u7684\u5e95\u5c42\u6548\u7387\uff1b\u5176\u4e8c\uff0c\u662f<strong>\u7531\u5e93\u9a71\u52a8\u7684\u67b6\u6784\u6a21\u5f0f<\/strong>\uff0c\u5373<strong>\u7ed3\u6784\u5316\u5e76\u53d1\uff08Structured Concurrency\uff09<\/strong>\uff0c\u5b83\u4e3a\u5e76\u53d1\u4efb\u52a1\u63d0\u4f9b\u4e86\u5065\u58ee\u3001\u53ef\u9884\u6d4b\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u548c\u9519\u8bef\u5904\u7406\u6846\u67b6\u3002\u901a\u8fc7\u672c\u6b21\u6df1\u5ea6\u89e3\u6790\uff0c\u6211\u4eec\u5c06\u7cfb\u7edf\u6027\u5730\u9610\u660e Kotlin \u534f\u7a0b\u662f\u5982\u4f55\u4ece\u6839\u672c\u4e0a\u6539\u53d8\u5f02\u6b65\u7f16\u7a0b\u7684\u6e38\u620f\u89c4\u5219\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%B8%80%E8%8A%82%EF%BC%9A%E5%9F%BA%E6%9C%AC%E6%8A%BD%E8%B1%A1%EF%BC%9A%E6%8C%82%E8%B5%B7%EF%BC%88Suspension%EF%BC%89\"><\/span><strong>\u7b2c\u4e00\u8282\uff1a\u57fa\u672c\u62bd\u8c61\uff1a\u6302\u8d77\uff08Suspension\uff09<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u7406\u89e3\u534f\u7a0b\uff0c\u9996\u5148\u5fc5\u987b\u638c\u63e1\u5176\u6700\u6838\u5fc3\u3001\u6700\u6839\u672c\u7684\u62bd\u8c61\u6982\u5ff5\u2014\u2014\u6302\u8d77\uff08Suspension\uff09\u3002\u6b63\u662f\u8fd9\u4e00\u673a\u5236\u5c06\u534f\u7a0b\u4e0e\u4f20\u7edf\u7684\u5e76\u53d1\u6a21\u578b\uff08\u5982\u7ebf\u7a0b\uff09\u533a\u5206\u5f00\u6765\uff0c\u5e76\u7531\u6b64\u884d\u751f\u51fa\u534f\u7a0b\u7684\u6240\u6709\u5176\u4ed6\u4f18\u52bf\u3002\u672c\u8282\u5c06\u4ece\u534f\u7a0b\u7684\u201c\u8f7b\u91cf\u7ea7\u201d\u7279\u6027\u51fa\u53d1\uff0c\u6df1\u5165\u5256\u6790\u201c\u6302\u8d77\u201d\u4e0e\u201c\u963b\u585e\u201d\u7684\u672c\u8d28\u533a\u522b\uff0c\u5e76\u6700\u7ec8\u9610\u660e\u8fd9\u4e00\u673a\u5236\u5982\u4f55\u5e26\u6765\u4e86\u4ee3\u7801\u6e05\u6670\u5ea6\u7684\u9769\u547d\u6027\u63d0\u5347\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"11_%E8%B6%85%E8%B6%8A%E7%BA%BF%E7%A8%8B%EF%BC%9A%E5%8D%8F%E7%A8%8B%E7%9A%84%E8%BD%BB%E9%87%8F%E7%BA%A7%E6%9C%AC%E8%B4%A8\"><\/span><strong>1.1 \u8d85\u8d8a\u7ebf\u7a0b\uff1a\u534f\u7a0b\u7684\u8f7b\u91cf\u7ea7\u672c\u8d28<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f20\u7edf\u5e76\u53d1\u7f16\u7a0b\u7684\u57fa\u672c\u5355\u5143\u662f\u7ebf\u7a0b\u3002\u7136\u800c\uff0c\u7ebf\u7a0b\u662f\u4e00\u79cd\u91cd\u91cf\u7ea7\u7684\u3001\u7531\u64cd\u4f5c\u7cfb\u7edf\uff08OS\uff09\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u4e0d\u4ec5\u610f\u5473\u7740\u5185\u5b58\u7684\u6d88\u8017\uff08\u9700\u8981\u4e3a\u5176\u5206\u914d\u72ec\u7acb\u7684\u6808\u7a7a\u95f4\uff09\uff0c\u8fd8\u4f34\u968f\u7740\u6602\u8d35\u7684 CPU \u4e0a\u4e0b\u6587\u5207\u6362\u6210\u672c <sup>9<\/sup>\u3002\u8fd9\u4f7f\u5f97\u5927\u89c4\u6a21\u5e76\u53d1\u6210\u4e3a\u4e00\u4e2a\u68d8\u624b\u7684\u95ee\u9898\uff1a\u5f53\u9700\u8981\u540c\u65f6\u5904\u7406\u6210\u5343\u4e0a\u4e07\u4e2a\u5e76\u53d1\u4efb\u52a1\u65f6\uff0c\u521b\u5efa\u540c\u7b49\u6570\u91cf\u7684\u7ebf\u7a0b\u51e0\u4e4e\u5fc5\u7136\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u8d44\u6e90\u8017\u5c3d\uff0c\u751a\u81f3\u5f15\u53d1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">OutOfMemoryError <sup>4<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin \u534f\u7a0b\u4e3a\u6b64\u63d0\u4f9b\u4e86\u622a\u7136\u4e0d\u540c\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4e00\u4e2a\u534f\u7a0b\uff0c\u5176\u672c\u8d28\u662f<strong>\u4e00\u4e2a\u53ef\u6302\u8d77\u8ba1\u7b97\u7684\u5b9e\u4f8b\uff08an instance of a suspendable computation\uff09<\/strong>\uff0c\u800c\u975e\u4e00\u4e2a\u7ebf\u7a0b <sup>11<\/sup>\u3002\u5b83\u5e38\u88ab\u6bd4\u4f5c\u201c\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u201d\uff0c\u56e0\u4e3a\u534f\u7a0b\u5e76\u4e0d\u4e0e\u4efb\u4f55\u7279\u5b9a\u7684\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u4e00\u5bf9\u4e00\u7ed1\u5b9a <sup>7<\/sup>\u3002\u534f\u7a0b\u662f\u5b58\u5728\u4e8e\u7528\u6237\u7a7a\u95f4\uff08user-level\uff09\u7684\u62bd\u8c61\uff0c\u5b83\u5728 JVM \u5806\u4e0a\u4ec5\u8868\u73b0\u4e3a\u4e00\u4e2a\u5c0f\u5bf9\u8c61\uff08\u5373\u7a0d\u540e\u5c06\u8be6\u8ff0\u7684\u201cContinuation\u201d\u5bf9\u8c61\uff09\uff0c\u800c\u975e\u4e00\u4e2a\u62e5\u6709\u5e9e\u5927\u539f\u751f\u6808\u7684\u7ebf\u7a0b <sup>3<\/sup>\u3002\u8fd9\u79cd\u8bbe\u8ba1\u5e26\u6765\u4e86\u60ca\u4eba\u7684\u8d44\u6e90\u6548\u7387\uff0c\u4f7f\u5f97\u5728\u5355\u4e2a JVM \u5b9e\u4f8b\u4e2d\u521b\u5efa\u6570\u5341\u4e07\u751a\u81f3\u6570\u767e\u4e07\u4e2a\u534f\u7a0b\u6210\u4e3a\u53ef\u80fd\uff0c\u800c\u4e0d\u4f1a\u89e6\u53ca\u8d44\u6e90\u74f6\u9888 <sup>9<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u800c\uff0c\u5bf9\u201c\u534f\u7a0b\u6bd4\u7ebf\u7a0b\u5feb\u201d\u8fd9\u4e00\u6d41\u884c\u8bf4\u6cd5\u9700\u8981\u6709\u66f4\u7cbe\u786e\u7684\u7406\u89e3\u3002\u534f\u7a0b\u7684\u6027\u80fd\u4f18\u52bf\u4e3b\u8981\u4f53\u73b0\u5728\u8d44\u6e90\u5229\u7528\u7387\u548c\u9ad8\u5e76\u53d1\u4e0b\u7684\u541e\u5410\u91cf\uff0c\u5c24\u5176\u662f\u5728 I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u3002\u5728\u5355\u4e2a\u4efb\u52a1\u7684**\u542f\u52a8\u5ef6\u8fdf\uff08startup latency\uff09**\u65b9\u9762\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5df2\u7ecf\u9884\u70ed\u7684\u7ebf\u7a0b\u6c60\uff0c\u5176\u542f\u52a8\u4efb\u52a1\u7684\u901f\u5ea6\u53ef\u80fd\u5fae\u5feb\u4e8e\u542f\u52a8\u4e00\u4e2a\u65b0\u534f\u7a0b <sup>14<\/sup>\u3002\u56e0\u6b64\uff0c\u534f\u7a0b\u771f\u6b63\u7684\u6027\u80fd\u589e\u76ca\u6765\u6e90\u4e8e\u5176\u5728\u89c4\u6a21\u5316\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u9ad8\u6548\u7387\uff0c\u800c\u975e\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u7684\u539f\u59cb\u542f\u52a8\u901f\u5ea6 <sup>14<\/sup>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"12_%E6%8C%82%E8%B5%B7%E6%9C%BA%E5%88%B6_vs_%E9%98%BB%E5%A1%9E%E7%9A%84%E4%BD%8E%E6%95%88\"><\/span><strong>1.2 \u6302\u8d77\u673a\u5236 vs. \u963b\u585e\u7684\u4f4e\u6548<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u8fdb\u4e00\u6b65\u7406\u89e3\u534f\u7a0b\u7684\u8f7b\u91cf\u7ea7\u672c\u8d28\uff0c\u5fc5\u987b\u8fa8\u6790\u201c\u6302\u8d77\u201d\u4e0e\u201c\u963b\u585e\u201d\u8fd9\u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u963b\u585e\uff08Blocking\uff09<\/strong>\uff1a\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u4e00\u4e2a\u963b\u585e\u64cd\u4f5c\u65f6\uff0c\u4f8b\u5982\u8c03\u7528 Thread.sleep() \u6216\u7b49\u5f85\u4e00\u4e2a\u540c\u6b65\u7684 I\/O \u64cd\u4f5c\u8fd4\u56de\uff0c\u8be5\u7ebf\u7a0b\u7684\u6267\u884c\u6d41\u4f1a\u88ab\u64cd\u4f5c\u7cfb\u7edf\u6682\u505c\u3002\u5728\u6b64\u671f\u95f4\uff0c\u7ebf\u7a0b\u867d\u7136\u88ab\u201c\u5361\u4f4f\u201d\uff0c\u4e0d\u80fd\u6267\u884c\u4efb\u4f55\u5176\u4ed6\u4ee3\u7801\uff0c\u4f46\u5b83\u4ecd\u7136\u5360\u6709\u7740\u5b9d\u8d35\u7684\u7cfb\u7edf\u8d44\u6e90\uff0c\u5c24\u5176\u662f\u5176\u5b8c\u6574\u7684\u6808\u5185\u5b58\u3002\u8fd9\u662f\u4e00\u79cd\u6781\u5176\u4f4e\u6548\u7684\u8d44\u6e90\u4f7f\u7528\u65b9\u5f0f\uff0c\u56e0\u4e3a\u4e00\u4e2a\u6602\u8d35\u7684\u8d44\u6e90\u5728\u5927\u90e8\u5206\u65f6\u95f4\u91cc\u53ef\u80fd\u53ea\u662f\u5728\u7a7a\u95f2\u7b49\u5f85 <sup>4<\/sup>\u3002\u5728 UI \u5e94\u7528\u4e2d\uff0c\u963b\u585e\u4e3b\u7ebf\u7a0b\u66f4\u662f\u4f1a\u5bfc\u81f4\u754c\u9762\u51bb\u7ed3\u548c\u201c\u5e94\u7528\u65e0\u54cd\u5e94\u201d\uff08ANR\uff09\u7684\u4e25\u91cd\u95ee\u9898 <sup>3<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u6302\u8d77\uff08Suspending\uff09<\/strong>\uff1a\u6302\u8d77\u662f\u534f\u7a0b\u7684\u6838\u5fc3\u673a\u5236\u3002\u5f53\u4e00\u4e2a\u534f\u7a0b\u6267\u884c\u5230<strong>\u6302\u8d77\u70b9\uff08suspension point\uff09<\/strong>\u2014\u2014\u5373\u4e00\u4e2a\u5bf9\u5176\u4ed6 suspend \u51fd\u6570\u7684\u8c03\u7528\u65f6\uff0c\u5b83\u53ef\u4ee5\u6682\u505c\u81ea\u8eab\u7684\u6267\u884c\uff0c\u4f46<strong>\u5b8c\u5168\u4e0d\u4f1a\u963b\u585e\u5176\u6240\u5728\u7684\u7ebf\u7a0b<\/strong> <sup>3<\/sup>\u3002\u7ebf\u7a0b\u4f1a\u7acb\u5373\u88ab\u91ca\u653e\uff0c\u53ef\u4ee5\u53bb\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u4f8b\u5982\u8fd0\u884c\u53e6\u4e00\u4e2a\u534f\u7a0b\u6216\u5904\u7406 UI \u4e8b\u4ef6 <sup>11<\/sup>\u3002\u5f53\u534f\u7a0b\u7b49\u5f85\u7684\u64cd\u4f5c\uff08\u5982\u7f51\u7edc\u54cd\u5e94\uff09\u5b8c\u6210\u540e\uff0c\u5b83\u53ef\u4ee5\u88ab\u6062\u590d\uff08resume\uff09\uff0c\u5e76\u4ece\u5b83\u79bb\u5f00\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\uff0c\u751a\u81f3\u53ef\u80fd\u662f\u5728\u4e00\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e0a <sup>9<\/sup>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u6267\u884c\u6a21\u5f0f\u88ab\u79f0\u4e3a<strong>\u534f\u4f5c\u5f0f\u591a\u4efb\u52a1\uff08Cooperative Multitasking\uff09<\/strong>\u3002\u4e0e\u7ebf\u7a0b\u7531\u64cd\u4f5c\u7cfb\u7edf\u5728\u4efb\u610f\u65f6\u523b\u5f3a\u5236\u5207\u6362\u7684\u62a2\u5360\u5f0f\u591a\u4efb\u52a1\uff08Preemptive Multitasking\uff09\u4e0d\u540c\uff0c\u534f\u7a0b\u53ea\u5728\u4ee3\u7801\u4e2d\u660e\u786e\u6807\u8bb0\u7684\u6302\u8d77\u70b9\u201c\u81ea\u613f\u5730\u201d\u4ea4\u51fa\u63a7\u5236\u6743 <sup>9<\/sup>\u3002\u8fd9\u79cd\u534f\u4f5c\u6027\u8d28\u4f7f\u5f97\u534f\u7a0b\u7684\u884c\u4e3a\u66f4\u52a0\u786e\u5b9a\uff0c\u56e0\u4e3a\u72b6\u6001\u7684\u53d8\u66f4\u53ea\u53ef\u80fd\u53d1\u751f\u5728\u8fd9\u4e9b\u5df2\u77e5\u7684\u6302\u8d77\u70b9\uff0c\u4ece\u800c\u7b80\u5316\u4e86\u5e76\u53d1\u4ee3\u7801\u7684\u8c03\u8bd5\u548c\u63a8\u7406 <sup>19<\/sup>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"13_%E4%BB%8E%E5%9B%9E%E8%B0%83%E5%9C%B0%E7%8B%B1%E5%88%B0%E9%A1%BA%E5%BA%8F%E6%B8%85%E6%99%B0%EF%BC%9A%E4%BB%A3%E7%A0%81%E5%8F%AF%E8%AF%BB%E6%80%A7%E7%9A%84%E8%8C%83%E5%BC%8F%E8%BD%AC%E7%A7%BB\"><\/span><strong>1.3 \u4ece\u56de\u8c03\u5730\u72f1\u5230\u987a\u5e8f\u6e05\u6670\uff1a\u4ee3\u7801\u53ef\u8bfb\u6027\u7684\u8303\u5f0f\u8f6c\u79fb<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f02\u6b65\u7f16\u7a0b\u7684\u957f\u671f\u75db\u70b9\u5728\u4e8e\u5176\u5bf9\u4ee3\u7801\u7ed3\u6784\u548c\u53ef\u8bfb\u6027\u7684\u7834\u574f\u3002\u4f20\u7edf\u4e0a\uff0c\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u56de\u8c03\u5730\u72f1\u7684\u56f0\u5883<\/strong>\uff1a\u5f53\u4e1a\u52a1\u903b\u8f91\u9700\u8981\u4e32\u884c\u6267\u884c\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\u65f6\uff08\u4f8b\u5982\uff0c\u5148\u83b7\u53d6\u7528\u6237\u4ee4\u724c\uff0c\u518d\u7528\u4ee4\u724c\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u6700\u540e\u66f4\u65b0 UI\uff09\uff0c\u4ee3\u7801\u4f1a\u8fc5\u901f\u9000\u5316\u4e3a\u5c42\u5c42\u5d4c\u5957\u7684\u56de\u8c03\u3002\u8fd9\u79cd\u91d1\u5b57\u5854\u5f62\u72b6\u7684\u7f29\u8fdb\u7ed3\u6784\uff0c\u88ab\u5f62\u8c61\u5730\u79f0\u4e3a\u201c\u56de\u8c03\u5730\u72f1\u201d\uff08Callback Hell\uff09\u6216\u201c\u6bc1\u706d\u91d1\u5b57\u5854\u201d\uff08Pyramid of Doom\uff09<sup>4<\/sup>\u3002\u5728\u8fd9\u6837\u7684\u4ee3\u7801\u4e2d\uff0c\u6b63\u5e38\u7684\u63a7\u5236\u6d41\u88ab\u6253\u65ad\uff0c\u9519\u8bef\u5904\u7406\u53d8\u5f97\u5f02\u5e38\u7e41\u7410\u548c\u5206\u6563\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u6025\u5267\u4e0b\u964d <sup>4<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u534f\u7a0b\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\uff1aKotlin \u534f\u7a0b\u901a\u8fc7\u5f15\u5165 suspend \u5173\u952e\u5b57\uff0c\u4ece\u6839\u672c\u4e0a\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u7528\u5b8c\u5168\u540c\u6b65\u3001\u987a\u5e8f\u7684\u65b9\u5f0f\u6765\u7f16\u5199\u5f02\u6b65\u903b\u8f91 <sup>4<\/sup>\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u50cf\u7f16\u5199\u666e\u901a\u963b\u585e\u4ee3\u7801\u4e00\u6837\uff0c\u4f7f\u7528\u6807\u51c6\u7684<br>for \u5faa\u73af\u3001if-else \u5206\u652f\u4ee5\u53ca try-catch \u5f02\u5e38\u5904\u7406\u5757\uff0c\u800c\u4ee3\u7801\u7684\u5f02\u6b65\u3001\u975e\u963b\u585e\u7279\u6027\u5219\u7531\u7f16\u8bd1\u5668\u548c\u534f\u7a0b\u5e93\u5728\u5e95\u5c42\u4fdd\u8bc1 <sup>4<\/sup>\u3002\u8fd9\u662f\u534f\u7a0b\u5e26\u7ed9\u5f00\u53d1\u8005\u7684\u6700\u76f4\u89c2\u3001\u6700\u5f3a\u5927\u7684\u4f53\u9a8c\u63d0\u5347\u3002<\/li>\n\n\n\n<li><strong>\u642d\u5efa\u6865\u6881<\/strong>\uff1a\u4e3a\u4e86\u4e0e\u5e9e\u5927\u7684\u73b0\u6709\u5f02\u6b65\u751f\u6001\u517c\u5bb9\uff0c\u534f\u7a0b\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5173\u952e\u7684\u5e95\u5c42\u51fd\u6570 suspendCoroutine\u3002\u5b83\u5145\u5f53\u4e86\u4e00\u5ea7\u6865\u6881\uff0c\u53ef\u4ee5\u5c06\u4efb\u4f55\u57fa\u4e8e\u56de\u8c03\u7684\u5f02\u6b65 API\uff0c\u4f18\u96c5\u5730\u5c01\u88c5\u6210\u4e00\u4e2a\u5e72\u51c0\u7684\u3001\u53ef\u6302\u8d77\u7684\u51fd\u6570\uff0c\u4ece\u800c\u5c06\u201c\u56de\u8c03\u5730\u72f1\u201d\u5f7b\u5e95\u57cb\u846c\u5728\u5386\u53f2\u4e2d <sup>5<\/sup>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u7cfb\u7edf\u5730\u603b\u7ed3\u534f\u7a0b\u4e0e\u7ebf\u7a0b\u7684\u6839\u672c\u5dee\u5f02\uff0c\u4e0b\u8868\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8be6\u7ec6\u7684\u5bf9\u6bd4\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u7279\u6027<\/td><td>\u7ebf\u7a0b (Thread)<\/td><td>\u534f\u7a0b (Coroutine)<\/td><\/tr><tr><td><strong>\u5e95\u5c42\u5355\u5143<\/strong><\/td><td>\u64cd\u4f5c\u7cfb\u7edf\uff08OS\uff09\u7ba1\u7406\u7684\u539f\u751f\u5b9e\u4f53<\/td><td>\u8bed\u8a00\/\u5e93\u5c42\u9762\u7684\u62bd\u8c61<\/td><\/tr><tr><td><strong>\u8d44\u6e90\u6210\u672c (\u5185\u5b58)<\/strong><\/td><td>\u9ad8\uff08\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u3001\u8f83\u5927\u7684\u6808\uff09<\/td><td>\u4f4e\uff08\u6bcf\u4e2a\u534f\u7a0b\u4ec5\u4e3a\u5806\u4e0a\u7684\u4e00\u4e2a\u5c0f\u5bf9\u8c61\uff09<\/td><\/tr><tr><td><strong>\u521b\u5efa\u6210\u672c (CPU)<\/strong><\/td><td>\u9ad8\uff08\u6d89\u53ca\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\uff09<\/td><td>\u4f4e\uff08\u666e\u901a\u7684\u5bf9\u8c61\u5b9e\u4f8b\u5316\uff09<\/td><\/tr><tr><td><strong>\u4e0a\u4e0b\u6587\u5207\u6362<\/strong><\/td><td>\u6602\u8d35\uff08\u9700\u8981\u8fdb\u5165\u5185\u6838\u6a21\u5f0f\uff09<\/td><td>\u5ec9\u4ef7\uff08\u7528\u6237\u7a7a\u95f4\u7684\u5bf9\u8c61\u5f15\u7528\u5207\u6362\uff09<\/td><\/tr><tr><td><strong>\u5e76\u53d1\u6a21\u578b<\/strong><\/td><td>\u62a2\u5360\u5f0f\u591a\u4efb\u52a1 (Preemptive)<\/td><td>\u534f\u4f5c\u5f0f\u591a\u4efb\u52a1 (Cooperative)<\/td><\/tr><tr><td><strong>\u6267\u884c\u6a21\u578b<\/strong><\/td><td>\u963b\u585e (Blocking)<\/td><td>\u6302\u8d77 (Suspending)<\/td><\/tr><tr><td><strong>\u53ef\u4f38\u7f29\u6027<\/strong><\/td><td>\u53d7\u9650\u4e8e OS\/\u786c\u4ef6 (\u901a\u5e38\u4e3a\u6570\u767e\u81f3\u6570\u5343)<\/td><td>\u975e\u5e38\u9ad8 (\u53ef\u8fbe\u6570\u767e\u4e07)<\/td><\/tr><tr><td><strong>\u7ba1\u7406\u6a21\u578b<\/strong><\/td><td>\u624b\u52a8\u7ba1\u7406\u751f\u547d\u5468\u671f<\/td><td>\u7ed3\u6784\u5316\u5e76\u53d1 (Structured Concurrency)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6570\u636e\u6765\u6e90: <sup>9<\/sup><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u534f\u7a0b\u7684\u201c\u8f7b\u91cf\u7ea7\u201d\u7279\u6027\u4e0d\u4ec5\u4ec5\u662f\u4e00\u79cd\u6027\u80fd\u4f18\u5316\uff0c\u5b83\u66f4\u662f\u4e00\u79cd\u5168\u65b0\u7f16\u7a0b\u6a21\u578b\u7684\u50ac\u5316\u5242\u3002\u6b63\u662f\u56e0\u4e3a\u534f\u7a0b\u7684\u521b\u5efa\u548c\u5207\u6362\u6210\u672c\u6781\u4f4e\uff0c\u50cf Actor \u6a21\u578b\u6216\u5904\u7406\u6d77\u91cf\u5e76\u53d1\u4efb\u52a1\u8fd9\u7c7b\u5bf9\u5e76\u53d1\u7c92\u5ea6\u8981\u6c42\u6781\u9ad8\u7684\u8303\u5f0f\uff0c\u624d\u5728 JVM \u5e73\u53f0\u4e0a\u53d8\u5f97\u5207\u5b9e\u53ef\u884c <sup>23<\/sup>\u3002\u4f20\u7edf\u7ebf\u7a0b\u7684\u91cd\u91cf\u7ea7\u7279\u6027\u4f7f\u5f97\u8fd9\u7c7b\u5e94\u7528\u5728\u5b9e\u8df5\u4e2d\u6210\u672c\u8fc7\u9ad8\u3002\u56e0\u6b64\uff0c\u534f\u7a0b\u7684\u8f7b\u91cf\u7ea7\u7279\u6027\u662f\u5176\u80fd\u591f\u652f\u6491\u8d77\u7ed3\u6784\u5316\u5e76\u53d1\u7b49\u9ad8\u7ea7\u62bd\u8c61\u7684\u6839\u672c\u539f\u56e0\u3002\u5b83\u4e0d\u53ea\u662f\u5bf9\u73b0\u6709\u5f02\u6b65\u6a21\u5f0f\u7684\u6539\u8fdb\uff0c\u800c\u662f\u4f7f\u5176\u6f14\u8fdb\u5230\u5168\u65b0\u5f62\u6001\u7684\u57fa\u77f3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u53e6\u4e00\u4e2a\u89d2\u5ea6\u770b\uff0c\u4ece\u963b\u585e\u5230\u6302\u8d77\u7684\u8f6c\u53d8\uff0c\u6807\u5fd7\u7740\u5e76\u53d1\u7f16\u7a0b\u7684\u7126\u70b9\u4ece\u201c\u4ee5\u7ebf\u7a0b\u4e3a\u4e2d\u5fc3\u201d\u8f6c\u5411\u4e86\u201c\u4ee5\u4efb\u52a1\u4e3a\u4e2d\u5fc3\u201d\u3002\u5728\u4f20\u7edf\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u9700\u8981\u65f6\u523b\u5173\u6ce8\u7ebf\u7a0b\u6c60\u7684\u7ba1\u7406\u3001\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u4ee5\u53ca\u5982\u4f55\u907f\u514d\u963b\u585e\u5b83\u4eec <sup>4<\/sup>\u3002\u800c\u5728\u534f\u7a0b\u7684\u4e16\u754c\u91cc\uff0c\u5f00\u53d1\u8005\u4e3b\u8981\u5173\u6ce8\u7684\u662f\u201c\u4efb\u52a1\u201d\u672c\u8eab\uff08\u5373\u534f\u7a0b\u4f53\u5185\u7684\u4e1a\u52a1\u903b\u8f91\uff09\u3002\u5e95\u5c42\u7684\u7ebf\u7a0b\u6ca6\u4e3a\u4e86\u4e00\u4e2a\u5b9e\u73b0\u7ec6\u8282\uff0c\u7531<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dispatcher \u5728\u5e55\u540e\u8fdb\u884c\u8c03\u5ea6\u7ba1\u7406 <sup>10<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">suspend \u673a\u5236\u5c06\u4efb\u52a1\u7684\u903b\u8f91\u751f\u547d\u5468\u671f\u4e0e\u7ebf\u7a0b\u7684\u7269\u7406\u751f\u547d\u5468\u671f\u5f7b\u5e95\u89e3\u8026 <sup>11<\/sup>\u3002\u8fd9\u79cd\u89e3\u8026\u662f\u534f\u7a0b\u6700\u6838\u5fc3\u7684\u521b\u65b0\u4e4b\u4e00\uff0c\u5b83\u6781\u5927\u5730\u964d\u4f4e\u4e86\u5f00\u53d1\u8005\u7684\u5fc3\u667a\u8d1f\u62c5\uff0c\u4f7f\u5176\u80fd\u591f\u4ece\u590d\u6742\u7684\u7ebf\u7a0b\u7ba1\u7406\u4e2d\u89e3\u653e\u51fa\u6765\uff0c\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%BA%8C%E8%8A%82%EF%BC%9A%E6%B7%B1%E5%85%A5%E5%BA%95%E5%B1%82%EF%BC%9Asuspend_%E5%87%BD%E6%95%B0%E7%9A%84%E7%BC%96%E8%AF%91%E5%99%A8%E8%BD%AC%E6%8D%A2\"><\/span><strong>\u7b2c\u4e8c\u8282\uff1a\u6df1\u5165\u5e95\u5c42\uff1asuspend \u51fd\u6570\u7684\u7f16\u8bd1\u5668\u8f6c\u6362<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u534f\u7a0b\u770b\u4f3c\u795e\u5947\u7684\u7279\u6027\u2014\u2014\u80fd\u591f\u6682\u505c\u548c\u6062\u590d\uff0c\u540c\u65f6\u4fdd\u6301\u4ee3\u7801\u7684\u987a\u5e8f\u6027\u2014\u2014\u5e76\u975e\u51ed\u7a7a\u800c\u6765\u3002\u8fd9\u80cc\u540e\u662f Kotlin \u7f16\u8bd1\u5668\u8fdb\u884c\u7684\u4e00\u7cfb\u5217\u590d\u6742\u800c\u7cbe\u5de7\u7684\u8f6c\u6362\u3002\u672c\u8282\u5c06\u6df1\u5165\u5e95\u5c42\uff0c\u63ed\u5f00\u8fd9\u5c42\u201c\u9b54\u6cd5\u201d\u7684\u9762\u7eb1\uff0c\u8be6\u7ec6\u9610\u8ff0\u7f16\u8bd1\u5668\u662f\u5982\u4f55\u5c06\u4e00\u4e2a\u666e\u901a\u7684 suspend \u51fd\u6570\u8f6c\u6362\u6210\u4e00\u4e2a\u53ef\u7ba1\u7406\u7684\u3001\u975e\u963b\u585e\u7684\u72b6\u6001\u673a\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"21_%E7%BB%AD%E4%BD%93%E4%BC%A0%E9%80%92%E9%A3%8E%E6%A0%BC_Continuation-Passing_Style_CPS%EF%BC%9A%E7%A7%98%E5%AF%86%E6%AD%A6%E5%99%A8\"><\/span><strong>2.1 \u7eed\u4f53\u4f20\u9012\u98ce\u683c (Continuation-Passing Style, CPS)\uff1a\u79d8\u5bc6\u6b66\u5668<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin \u534f\u7a0b\u673a\u5236\u7684\u6838\u5fc3\uff0c\u662f\u7f16\u8bd1\u5668\u5728\u9047\u5230 suspend \u5173\u952e\u5b57\u65f6\u6240\u6267\u884c\u7684\u4e00\u79cd\u540d\u4e3a**\u7eed\u4f53\u4f20\u9012\u98ce\u683c\uff08Continuation-Passing Style, CPS\uff09**\u7684\u8f6c\u6362 <sup>16<\/sup>\u3002CPS \u662f\u4e00\u79cd\u5386\u53f2\u60a0\u4e45\u7684\u7f16\u8bd1\u5668\u6280\u672f\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u6539\u53d8\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5904\u7406\u65b9\u5f0f <sup>4<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e00\u4e2a\u666e\u901a\u7684\u51fd\u6570\u4e2d\uff0c\u5f53\u8ba1\u7b97\u5b8c\u6210\u540e\uff0c\u7ed3\u679c\u4f1a\u901a\u8fc7 return \u8bed\u53e5\u76f4\u63a5\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002\u800c\u5728 CPS \u8f6c\u6362\u540e\uff0c\u51fd\u6570\u4e0d\u518d\u76f4\u63a5 return \u7ed3\u679c\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u4f1a\u63a5\u53d7\u4e00\u4e2a\u989d\u5916\u7684\u3001\u9690\u85cf\u7684\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u88ab\u79f0\u4e3a<strong>\u7eed\u4f53\uff08Continuation\uff09<\/strong> <sup>7<\/sup>\u3002\u8fd9\u4e2a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continuation \u5bf9\u8c61\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u56de\u8c03\uff0c\u51fd\u6570\u5728\u5b8c\u6210\u8ba1\u7b97\u540e\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7eed\u4f53\u5e76\u5c06\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\uff0c\u7531\u7eed\u4f53\u6765\u51b3\u5b9a\u63a5\u4e0b\u6765\u6267\u884c\u4ec0\u4e48\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u6b64\uff0c\u53ef\u4ee5\u8ba4\u4e3a CPS \u662f\u7f16\u8bd1\u5668\u4e3a\u6211\u4eec\u81ea\u52a8\u5b9e\u73b0\u7684\u4e00\u79cd\u9ad8\u5ea6\u7ed3\u6784\u5316\u3001\u7c7b\u578b\u5b89\u5168\u7684\u56de\u8c03\u673a\u5236 <sup>25<\/sup>\u3002\u5b83\u5c06\u201c\u56de\u8c03\u5730\u72f1\u201d\u4e2d\u624b\u52a8\u7ba1\u7406\u7684\u3001\u6df7\u4e71\u7684\u5d4c\u5957\u56de\u8c03\uff0c\u8f6c\u53d8\u6210\u4e86\u7531\u7f16\u8bd1\u5668\u7cbe\u786e\u63a7\u5236\u7684\u3001\u7ebf\u6027\u7684\u7eed\u4f53\u4f20\u9012\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b57\u8282\u7801\u5c42\u9762\uff0c\u4e00\u4e2a suspend \u51fd\u6570\u7684\u7b7e\u540d\u4f1a\u88ab\u4fee\u6539\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u5728 Kotlin \u6e90\u7801\u4e2d\u58f0\u660e\u4e3a suspend fun process(data: Data): Result \u7684\u51fd\u6570\uff0c\u5728\u7f16\u8bd1\u540e\u5176 JVM \u7b7e\u540d\u4f1a\u5927\u81f4\u53d8\u4e3a Object process(Data data, Continuation&lt;Result&gt; continuation)\u3002\u539f\u6765\u7684\u8fd4\u56de\u7c7b\u578b Result \u6210\u4e3a\u4e86\u7eed\u4f53\u7684\u6cdb\u578b\u53c2\u6570\uff0c\u800c\u51fd\u6570\u672c\u8eab\u7684\u65b0\u8fd4\u56de\u7c7b\u578b\u53d8\u4e3a Object\uff08\u6216 Any?\uff09\uff0c\u56e0\u4e3a\u5b83\u73b0\u5728\u53ef\u80fd\u8fd4\u56de\u4e24\u79cd\u4e1c\u897f\uff1a\u4e00\u662f\u8ba1\u7b97\u7684\u6700\u7ec8\u7ed3\u679c\uff08\u5982\u679c\u64cd\u4f5c\u540c\u6b65\u5b8c\u6210\uff09\uff0c\u4e8c\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u6807\u8bb0\u503c COROUTINE_SUSPENDED\uff08\u5982\u679c\u64cd\u4f5c\u9700\u8981\u6302\u8d77\uff09<sup>27<\/sup>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"22_Continuation_%E6%8E%A5%E5%8F%A3%EF%BC%9A%E4%B8%80%E4%BB%BD%E6%81%A2%E5%A4%8D%E6%89%A7%E8%A1%8C%E7%9A%84%E5%A5%91%E7%BA%A6\"><\/span><strong>2.2 Continuation \u63a5\u53e3\uff1a\u4e00\u4efd\u6062\u590d\u6267\u884c\u7684\u5951\u7ea6<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u5728\u5e55\u540e\u4f20\u9012\u7684 Continuation \u5bf9\u8c61\uff0c\u662f\u9075\u5faa Kotlin \u6807\u51c6\u5e93\u4e2d\u4e00\u4e2a\u660e\u786e\u5b9a\u4e49\u7684\u63a5\u53e3\u7684\u5b9e\u4f8b\u3002\u8fd9\u4e2a\u63a5\u53e3\u662f\u534f\u7a0b\u6302\u8d77\u4e0e\u6062\u590d\u673a\u5236\u7684\u57fa\u77f3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continuation&lt;in T&gt; \u63a5\u53e3\u7684\u5b9a\u4e49\u975e\u5e38\u7b80\u6d01 <sup>18<\/sup>\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">interface Continuation&lt;in T&gt; {<br>&nbsp; &nbsp; val context: CoroutineContext<br>&nbsp; &nbsp; fun resumeWith(result: Result&lt;T&gt;)<br>}<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u5305\u542b\u4e24\u4e2a\u6838\u5fc3\u90e8\u5206\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>context: CoroutineContext\uff1a\u5b83\u6301\u6709\u4e00\u4e2a CoroutineContext \u5b9e\u4f8b\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u534f\u7a0b\u6267\u884c\u6240\u9700\u7684\u73af\u5883\u4fe1\u606f\uff0c\u5982\u8c03\u5ea6\u5668\uff08Dispatcher\uff09\u3001\u4f5c\u4e1a\uff08Job\uff09\u7b49\u3002\u534f\u7a0b\u5728\u6062\u590d\u65f6\u9700\u8981\u8fd9\u4e9b\u4fe1\u606f\u6765\u786e\u5b9a\u5728\u54ea\u4e2a\u7ebf\u7a0b\u4e0a\u3001\u4ee5\u4f55\u79cd\u72b6\u6001\u7ee7\u7eed\u6267\u884c\u3002\u8fd9\u90e8\u5206\u5c06\u5728\u7b2c\u56db\u8282\u8be6\u7ec6\u8ba8\u8bba\u3002<\/li>\n\n\n\n<li>resumeWith(result: Result&lt;T&gt;)\uff1a\u8fd9\u662f\u6574\u4e2a\u673a\u5236\u4e2d\u6700\u5173\u952e\u7684\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u7528\u4e8e\u6062\u590d\u534f\u7a0b\u7684\u6267\u884c\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a Result&lt;T&gt; \u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8fd9\u4e2a Result \u5bf9\u8c61\u662f\u4e00\u4e2a\u5c01\u88c5\u7c7b\uff0c\u65e2\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u6210\u529f\u7684\u503c\uff08\u7c7b\u578b\u4e3a T\uff09\uff0c\u4e5f\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u5931\u8d25\u7684\u5f02\u5e38\uff08Throwable\uff09\u3002\u8fd9\u79cd\u8bbe\u8ba1\u786e\u4fdd\u4e86\u65e0\u8bba\u662f\u6b63\u5e38\u8fd4\u56de\u8fd8\u662f\u5f02\u5e38\u629b\u51fa\uff0c\u90fd\u80fd\u901a\u8fc7\u7edf\u4e00\u7684\u901a\u9053\u8fdb\u884c\u4f20\u9012\uff0c\u5b9e\u73b0\u4e86\u5065\u58ee\u7684\u9519\u8bef\u5904\u7406 <sup>18<\/sup>\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0cKotlin \u6807\u51c6\u5e93\u8fd8\u4e3a Continuation \u63d0\u4f9b\u4e86\u4e24\u4e2a\u6269\u5c55\u51fd\u6570\uff1aresume(value: T) \u548c resumeWithException(exception: Throwable)\uff0c\u5b83\u4eec\u662f resumeWith \u7684\u8bed\u6cd5\u7cd6\uff0c\u5206\u522b\u7528\u4e8e\u6062\u590d\u4e00\u4e2a\u6210\u529f\u7684\u7ed3\u679c\u548c\u4e00\u4e2a\u5f02\u5e38 <sup>29<\/sup>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"23_%E7%94%9F%E6%88%90%E7%9A%84%E7%8A%B6%E6%80%81%E6%9C%BA%EF%BC%9Asuspend_%E5%87%BD%E6%95%B0%E7%9A%84%E7%9C%9F%E5%AE%9E%E5%BD%A2%E6%80%81\"><\/span><strong>2.3 \u751f\u6210\u7684\u72b6\u6001\u673a\uff1asuspend \u51fd\u6570\u7684\u771f\u5b9e\u5f62\u6001<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7f16\u8bd1\u5668\u5bf9 suspend \u51fd\u6570\u5e94\u7528 CPS \u8f6c\u6362\u65f6\uff0c\u5b83\u4e0d\u4ec5\u4ec5\u662f\u589e\u52a0\u4e86\u4e00\u4e2a\u53c2\u6570\u3002\u66f4\u91cd\u8981\u7684\u662f\uff0c\u5b83\u5c06\u6574\u4e2a\u51fd\u6570\u4f53\u5f7b\u5e95\u91cd\u6784\u4e3a\u4e00\u4e2a<strong>\u6709\u9650\u72b6\u6001\u673a\uff08Finite State Machine, FSM\uff09<\/strong> <sup>16<\/sup>\u3002\u8fd9\u4e2a\u72b6\u6001\u673a\u901a\u5e38\u88ab\u5b9e\u73b0\u5728\u4e00\u4e2a\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7684\u3001\u5b9e\u73b0\u4e86<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continuation \u63a5\u53e3\u7684\u533f\u540d\u5185\u90e8\u7c7b\u4e2d\uff0c\u5176\u6838\u5fc3\u903b\u8f91\u4f4d\u4e8e invokeSuspend \u65b9\u6cd5\u5185 <sup>27<\/sup>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6302\u8d77\u70b9\u5373\u72b6\u6001<\/strong>\uff1a\u539f\u59cb suspend \u51fd\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6302\u8d77\u70b9\uff08\u5373\u6bcf\u4e00\u6b21\u5bf9\u5176\u4ed6 suspend \u51fd\u6570\u7684\u8c03\u7528\uff09\u90fd\u5bf9\u5e94\u7740\u8fd9\u4e2a\u72b6\u6001\u673a\u7684\u4e00\u4e2a\u72b6\u6001 <sup>27<\/sup>\u3002<\/li>\n\n\n\n<li><strong>label \u5b57\u6bb5<\/strong>\uff1a\u72b6\u6001\u673a\u5982\u4f55\u77e5\u9053\u5f53\u524d\u5904\u4e8e\u54ea\u4e2a\u72b6\u6001\uff1f\u7b54\u6848\u662f\u901a\u8fc7 Continuation \u5bf9\u8c61\u5185\u90e8\u4e00\u4e2a\u6574\u6570\u5b57\u6bb5\uff0c\u901a\u5e38\u547d\u540d\u4e3a label <sup>7<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u5c40\u90e8\u53d8\u91cf\u7684\u4fdd\u5b58<\/strong>\uff1a\u5728\u4f20\u7edf\u7684\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf\u5b58\u50a8\u5728\u8c03\u7528\u6808\u7684\u6808\u5e27\u4e0a\uff0c\u51fd\u6570\u8fd4\u56de\u65f6\u6808\u5e27\u88ab\u9500\u6bc1\uff0c\u5c40\u90e8\u53d8\u91cf\u4e5f\u968f\u4e4b\u6d88\u5931\u3002\u4f46\u534f\u7a0b\u6302\u8d77\u65f6\uff0c\u51fd\u6570\u4f1a\u8fd4\u56de\uff0c\u6808\u5e27\u540c\u6837\u4f1a\u88ab\u9500\u6bc1\u3002\u90a3\u4e48\uff0c\u90a3\u4e9b\u9700\u8981\u5728\u6302\u8d77\u70b9\u524d\u540e\u4fdd\u6301\u4e00\u81f4\u7684\u5c40\u90e8\u53d8\u91cf\u600e\u4e48\u529e\uff1f\u7f16\u8bd1\u5668\u4f1a\u5c06\u8fd9\u4e9b\u53d8\u91cf\u63d0\u5347\uff08hoist\uff09\u4e3a Continuation \u5bf9\u8c61\u81ea\u8eab\u7684\u5b57\u6bb5\u3002\u5f53\u534f\u7a0b\u6302\u8d77\u65f6\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\u88ab\u4fdd\u5b58\u5728\u5806\u4e0a\u7684 Continuation \u5bf9\u8c61\u91cc\uff1b\u5f53\u534f\u7a0b\u6062\u590d\u65f6\uff0c\u518d\u4ece\u8be5\u5bf9\u8c61\u4e2d\u8bfb\u53d6\u51fa\u6765 <sup>30<\/sup>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u72b6\u6001\u673a\u7684\u6267\u884c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521d\u6b21\u8c03\u7528<\/strong>\uff1a\u5f53 suspend \u51fd\u6570\u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\uff0c\u5176\u5bf9\u5e94\u7684 Continuation \u5bf9\u8c61\u7684 label \u4e3a 0\u3002\u4ee3\u7801\u4ece\u5934\u5f00\u59cb\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u9047\u5230\u6302\u8d77\u70b9<\/strong>\uff1a\u5f53\u6267\u884c\u6d41\u9047\u5230\u7b2c\u4e00\u4e2a\u6302\u8d77\u70b9\u65f6\uff0c\u5728\u771f\u6b63\u8c03\u7528\u90a3\u4e2a\u6302\u8d77\u51fd\u6570\u4e4b\u524d\uff0c\u7f16\u8bd1\u5668\u63d2\u5165\u7684\u4ee3\u7801\u4f1a\u505a\u4e24\u4ef6\u4e8b\uff1a\u9996\u5148\uff0c\u5c06\u6240\u6709\u9700\u8981\u8de8\u8d8a\u6302\u70b9\u5b58\u5728\u7684\u5c40\u90e8\u53d8\u91cf\u5b58\u5165 Continuation \u5bf9\u8c61\u7684\u5b57\u6bb5\u4e2d\uff1b\u5176\u6b21\uff0c\u5c06 label \u7684\u503c\u66f4\u65b0\u5230\u4e0b\u4e00\u4e2a\u72b6\u6001\uff08\u4f8b\u5982\uff0clabel = 1\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6062\u590d\u6267\u884c<\/strong>\uff1a\u5f53\u534f\u7a0b\u88ab\u5916\u90e8\u4e8b\u4ef6\uff08\u5982\u7f51\u7edc\u54cd\u5e94\uff09\u901a\u8fc7\u8c03\u7528 continuation.resumeWith(&#8230;) \u6062\u590d\u65f6\uff0cinvokeSuspend \u65b9\u6cd5\u4f1a\u88ab\u518d\u6b21\u8c03\u7528\u3002\u6b64\u65f6\uff0c\u65b9\u6cd5\u5165\u53e3\u5904\u7684 switch\uff08\u6216 when\uff09\u8bed\u53e5\u4f1a\u68c0\u67e5 label \u7684\u503c\u3002\u53d1\u73b0 label \u4e3a 1\uff0c\u6267\u884c\u6d41\u5c31\u4f1a\u76f4\u63a5\u8df3\u8f6c\u5230\u72b6\u6001 1 \u5bf9\u5e94\u7684\u4ee3\u7801\u5757\uff0c\u5373\u7b2c\u4e00\u4e2a\u6302\u8d77\u70b9\u4e4b\u540e\u7684\u4f4d\u7f6e\uff0c\u5e76\u4ece Continuation \u5bf9\u8c61\u4e2d\u6062\u590d\u4e4b\u524d\u4fdd\u5b58\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c <sup>25<\/sup>\u3002<\/li>\n\n\n\n<li><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"24_COROUTINE_SUSPENDED_%E6%9A%82%E5%81%9C%E7%9A%84%E4%BF%A1%E5%8F%B7\"><\/span><strong>2.4 COROUTINE_SUSPENDED: \u6682\u505c\u7684\u4fe1\u53f7<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u8ff0\u4f2a\u4ee3\u7801\u4e2d\uff0c\u51fa\u73b0\u4e86\u4e00\u4e2a\u5173\u952e\u7684\u5e38\u91cf\uff1aCOROUTINE_SUSPENDED\u3002\u8fd9\u4e2a\u7279\u6b8a\u7684\u5bf9\u8c61\u662f\u534f\u7a0b\u6302\u8d77\u673a\u5236\u5f97\u4ee5\u5b9e\u73b0\u7684\u6838\u5fc3\u4fe1\u53f7\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7279\u6b8a\u7684\u6807\u8bb0\u503c<\/strong>\uff1a\u5f53\u4e00\u4e2a suspend \u51fd\u6570\u771f\u6b63\u9700\u8981\u6302\u8d77\u65f6\uff08\u4f8b\u5982\uff0c\u5b83\u8c03\u7528\u7684 requestToken \u542f\u52a8\u4e86\u4e00\u4e2a\u7f51\u7edc\u8bf7\u6c42\uff0c\u65e0\u6cd5\u7acb\u5373\u8fd4\u56de\u7ed3\u679c\uff09\uff0c\u5b83\u5728 JVM \u5b57\u8282\u7801\u5c42\u9762\u5e76\u4e0d\u4f1a\u963b\u585e\uff0c\u800c\u662f\u7acb\u5373\u8fd4\u56de\u8fd9\u4e2a COROUTINE_SUSPENDED \u5355\u4f8b\u5bf9\u8c61 <sup>28<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u8c03\u7528\u6808\u7684\u5c55\u5f00<\/strong>\uff1a\u5f53\u4e00\u4e2a suspend \u51fd\u6570\u7684\u8c03\u7528\u8005\uff08\u5b83\u81ea\u8eab\u4e5f\u5fc5\u987b\u662f suspend \u51fd\u6570\uff09\u63a5\u6536\u5230 COROUTINE_SUSPENDED \u8fd9\u4e2a\u8fd4\u56de\u503c\u65f6\uff0c\u5b83\u5c31\u660e\u767d\u88ab\u8c03\u7528\u8005\u5df2\u7ecf\u6302\u8d77\uff0c\u5c1a\u672a\u5b8c\u6210\u3002\u4e8e\u662f\uff0c\u8c03\u7528\u8005\u4e5f\u4f1a\u7acb\u5373\u505c\u6b62\u81ea\u5df1\u7684\u6267\u884c\uff0c\u5e76\u540c\u6837\u5411\u5b83\u81ea\u5df1\u7684\u4e0a\u5c42\u8c03\u7528\u8005\u8fd4\u56de COROUTINE_SUSPENDED <sup>37<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u91ca\u653e\u7ebf\u7a0b<\/strong>\uff1a\u8fd9\u4e2a\u8fd4\u56de COROUTINE_SUSPENDED \u7684\u8fc7\u7a0b\u4f1a\u50cf\u591a\u7c73\u8bfa\u9aa8\u724c\u4e00\u6837\uff0c\u6cbf\u7740\u6574\u4e2a suspend \u51fd\u6570\u7684\u8c03\u7528\u6808\u5411\u4e0a\u4f20\u64ad\u3002\u6700\u7ec8\uff0c\u542f\u52a8\u8fd9\u4e2a\u534f\u7a0b\u7684\u534f\u7a0b\u6784\u5efa\u5668\uff08\u5982 launch\uff09\u4f1a\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\uff0c\u5e76\u7406\u89e3\u534f\u7a0b\u5df2\u7ecf\u6302\u8d77\u3002\u6b64\u65f6\uff0c\u6574\u4e2a\u8c03\u7528\u6808\u90fd\u5df2\u201c\u5c55\u5f00\u201d\uff08unwound\uff09\uff0c\u6240\u6709\u51fd\u6570\u8c03\u7528\u5747\u5df2\u8fd4\u56de\u3002\u8fd9\u6b63\u662f<strong>\u7ebf\u7a0b\u88ab\u91ca\u653e\u7684\u5173\u952e\u6240\u5728<\/strong> <sup>37<\/sup>\u3002\u56e0\u4e3a\u6ca1\u6709\u4efb\u4f55\u51fd\u6570\u5728\u6267\u884c\u6216\u7b49\u5f85\uff0c\u5f53\u524d\u7ebf\u7a0b\u5c31\u5b8c\u5168\u81ea\u7531\u4e86\uff0c\u53ef\u4ee5\u88ab<br>Dispatcher \u7528\u4e8e\u6267\u884c\u4efb\u4f55\u5176\u4ed6\u4efb\u52a1\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"25_%E4%B8%80%E6%AC%A1%E6%8C%82%E8%B5%B7%E8%B0%83%E7%94%A8%E7%9A%84%E5%AE%8C%E6%95%B4%E8%BF%BD%E8%B8%AA\"><\/span><strong>2.5 \u4e00\u6b21\u6302\u8d77\u8c03\u7528\u7684\u5b8c\u6574\u8ffd\u8e2a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u4ee5\u4e0a\u6240\u6709\u6982\u5ff5\u4e32\u8054\u8d77\u6765\uff0c\u5b8c\u6574\u5730\u8ffd\u8e2a\u4e00\u6b21\u6302\u8d77\u8c03\u7528\u7684\u751f\u547d\u5468\u671f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8c03\u7528<\/strong>\uff1a\u534f\u7a0b\u8c03\u7528 getUserProfile(&#8220;id-123&#8221;)\u3002\u7f16\u8bd1\u5668\u751f\u6210\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a GetUserProfileContinuation \u72b6\u6001\u673a\u5b9e\u4f8b\uff0c\u5176 label \u4e3a 0\u3002<\/li>\n\n\n\n<li><strong>\u6267\u884c\u81f3\u6302\u8d77<\/strong>\uff1a\u4ee3\u7801\u6267\u884c\u5230 requestToken \u8c03\u7528\u3002\u5728\u8c03\u7528\u524d\uff0c\u5b83\u5c06\u5c40\u90e8\u53d8\u91cf id \u7684\u503c &#8220;id-123&#8221; \u4fdd\u5b58\u5230\u72b6\u6001\u673a\u5b9e\u4f8b\u7684\u5b57\u6bb5\u4e2d\uff0c\u5e76\u5c06 label \u8bbe\u7f6e\u4e3a 1\u3002\u7136\u540e\uff0c\u5b83\u8c03\u7528 requestToken\uff0c\u5e76\u5c06\u72b6\u6001\u673a\u5b9e\u4f8b\u81ea\u8eab\u4f5c\u4e3a\u7eed\u4f53\u53c2\u6570\u4f20\u9012\u8fdb\u53bb\u3002<\/li>\n\n\n\n<li><strong>\u6302\u8d77<\/strong>\uff1arequestToken \u51fd\u6570\u542f\u52a8\u4e86\u4e00\u4e2a\u5f02\u6b65\u7f51\u7edc\u8bf7\u6c42\uff0c\u7531\u4e8e\u65e0\u6cd5\u7acb\u5373\u83b7\u5f97\u7ed3\u679c\uff0c\u5b83\u51b3\u5b9a\u6302\u8d77\u3002\u4e8e\u662f\uff0c\u5b83\u8fd4\u56de\u4e86 COROUTINE_SUSPENDED\u3002<\/li>\n\n\n\n<li><strong>\u6808\u5c55\u5f00\u4e0e\u7ebf\u7a0b\u91ca\u653e<\/strong>\uff1agetUserProfile \u51fd\u6570\u63a5\u6536\u5230 COROUTINE_SUSPENDED\uff0c\u4e8e\u662f\u5b83\u4e5f\u7acb\u5373\u8fd4\u56de COROUTINE_SUSPENDED\u3002\u6b64\u65f6\uff0c\u6267\u884c\u7ebf\u7a0b\u88ab\u91ca\u653e\u3002\u90a3\u4e2a\u5305\u542b\u4e86\u534f\u7a0b\u72b6\u6001\u7684 GetUserProfileContinuation \u5bf9\u8c61\uff0c\u73b0\u5728\u88ab\u7f51\u7edc\u5e93\u7684\u56de\u8c03\u673a\u5236\u6240\u6301\u6709\u3002<\/li>\n\n\n\n<li><strong>\u6062\u590d<\/strong>\uff1a\u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u7f51\u7edc\u8bf7\u6c42\u6210\u529f\uff0c\u8fd4\u56de\u4e86 token\u3002\u7f51\u7edc\u5e93\u7684\u56de\u8c03\u88ab\u89e6\u53d1\uff0c\u5b83\u8c03\u7528\u4e86 sm.resumeWith(Result.success(token))\u3002<\/li>\n\n\n\n<li><strong>\u91cd\u65b0\u8fdb\u5165<\/strong>\uff1aDispatcher \u63a5\u6536\u5230\u6062\u590d\u8bf7\u6c42\uff0c\u5c06\u4e00\u4e2a\u6062\u590d\u4efb\u52a1\uff08\u5373\u518d\u6b21\u8c03\u7528 sm.invokeSuspend\uff09\u8c03\u5ea6\u5230\u5408\u9002\u7684\u7ebf\u7a0b\u4e0a\u3002getUserProfile \u51fd\u6570\u88ab\u518d\u6b21\u8c03\u7528\uff0c\u4f20\u5165\u7684\u662f\u540c\u4e00\u4e2a\u72b6\u6001\u673a\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u72b6\u6001\u8df3\u8f6c\u4e0e\u7ee7\u7eed<\/strong>\uff1a\u51fd\u6570\u5165\u53e3\u7684 switch \u8bed\u53e5\u68c0\u67e5\u5230 sm.label \u4e3a 1\uff0c\u4e8e\u662f\u76f4\u63a5\u8df3\u8f6c\u5230 case 1: \u7684\u4ee3\u7801\u5757\u3002\u5b83\u4ece result \u4e2d\u83b7\u53d6\u5230 token\uff0c\u5e76\u4ece\u72b6\u6001\u673a\u5b57\u6bb5\u4e2d\u6062\u590d\u4e86 id \u7684\u503c\u3002\u7136\u540e\uff0c\u4ee3\u7801\u4ece\u7b2c\u4e00\u4e2a\u6302\u8d77\u70b9\u4e4b\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u9047\u5230\u7b2c\u4e8c\u4e2a\u6302\u8d77\u70b9 fetchUser\uff0c\u5e76\u91cd\u590d\u7c7b\u4f3c\u7684\u8fc7\u7a0b\uff0c\u6216\u8005\u6267\u884c\u5230\u51fd\u6570\u672b\u5c3e\u5e76\u8fd4\u56de\u6700\u7ec8\u7ed3\u679c\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u7531\u7f16\u8bd1\u5668\u4e3b\u5bfc\u7684\u3001\u57fa\u4e8e\u72b6\u6001\u673a\u7684\u8f6c\u6362\uff0c\u662f\u4e00\u79cd\u5178\u578b\u7684\u5c06\u590d\u6742\u6027\u4ece\u5f00\u53d1\u8005\u8f6c\u79fb\u5230\u5de5\u5177\u94fe\u7684\u7b56\u7565\u3002\u5f00\u53d1\u8005\u4eab\u53d7\u5230\u4e86\u7f16\u5199\u7b80\u5355\u987a\u5e8f\u4ee3\u7801\u7684\u4fbf\u5229\uff0c\u800c\u7f16\u8bd1\u5668\u5219\u627f\u62c5\u4e86\u5c06\u8fd9\u4e9b\u4ee3\u7801\u8f6c\u5316\u4e3a\u9ad8\u6548\u3001\u975e\u963b\u585e\u7684\u3001\u53ef\u6062\u590d\u7684\u72b6\u6001\u673a\u7684\u7e41\u91cd\u5de5\u4f5c\u3002\u8fd9\u6b63\u662f Kotlin \u534f\u7a0b\u8bbe\u8ba1\u54f2\u5b66\u7684\u4f53\u73b0\uff1a\u63d0\u4f9b\u5f3a\u5927\u7684\u62bd\u8c61\uff0c\u540c\u65f6\u9690\u85cf\u5e95\u5c42\u7684\u590d\u6742\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u5916\uff0cContinuation \u5bf9\u8c61\u7684\u8bbe\u8ba1\u63ed\u793a\u4e86\u534f\u7a0b\u8d85\u8d8a\u4f20\u7edf\u51fd\u6570\u8c03\u7528\u7684\u80fd\u529b\u3002\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u7684\u8c03\u7528\u72b6\u6001\u5b58\u5728\u4e8e\u6808\u4e0a\uff0c\u51fd\u6570\u8fd4\u56de\u5373\u9500\u6bc1\u3002\u800c Continuation \u5c06\u8fd9\u90e8\u5206\u72b6\u6001\u201c\u7269\u5316\u201d\uff08reified\uff09\u5e76\u79fb\u5230\u4e86\u5806\u4e0a <sup>32<\/sup>\u3002\u4f5c\u4e3a\u4e00\u4e2a\u666e\u901a\u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u88ab\u5b58\u50a8\u3001\u5728\u7ebf\u7a0b\u95f4\u4f20\u9012\u3001\u751a\u81f3\u88ab\u5e8f\u5217\u5316 <sup>40<\/sup>\u3002\u8fd9\u6b63\u662f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dispatcher \u80fd\u591f\u5c06\u4e00\u4e2a\u5728\u4e3b\u7ebf\u7a0b\u6302\u8d77\u7684\u534f\u7a0b\uff0c\u5728 I\/O \u7ebf\u7a0b\u4e0a\u6062\u590d\u7684\u6839\u672c\u539f\u56e0\u2014\u2014\u4f20\u9012\u7684\u4e0d\u662f\u7ebf\u7a0b\uff0c\u800c\u662f\u8fd9\u4e2a\u5305\u542b\u4e86\u534f\u7a0b\u201c\u7075\u9b42\u201d\u7684 Continuation \u5bf9\u8c61\u3002\u8fd9\u79cd\u5728\u4e0d\u540c\u6267\u884c\u4e0a\u4e0b\u6587\u4e4b\u95f4\u201c\u4f20\u9001\u201d\u7684\u80fd\u529b\uff0c\u662f\u4f20\u7edf\u7684\u3001\u4e0e\u6808\u7ed1\u5b9a\u7684\u6267\u884c\u6a21\u578b\u65e0\u6cd5\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%B8%89%E8%8A%82%EF%BC%9A%E6%9E%B6%E6%9E%84%E6%94%AF%E6%9F%B1%EF%BC%9A%E7%BB%93%E6%9E%84%E5%8C%96%E5%B9%B6%E5%8F%91\"><\/span><strong>\u7b2c\u4e09\u8282\uff1a\u67b6\u6784\u652f\u67f1\uff1a\u7ed3\u6784\u5316\u5e76\u53d1<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8bf4\u7f16\u8bd1\u5668\u8f6c\u6362\u662f Kotlin \u534f\u7a0b\u7684\u5fae\u89c2\u5b9e\u73b0\u57fa\u7840\uff0c\u90a3\u4e48**\u7ed3\u6784\u5316\u5e76\u53d1\uff08Structured Concurrency\uff09**\u5c31\u662f\u5176\u5b8f\u89c2\u7684\u67b6\u6784\u8bbe\u8ba1\u54f2\u5b66\u3002\u5b83\u4e3a\u770b\u4f3c\u81ea\u7531\u7684\u5e76\u53d1\u4efb\u52a1\u63d0\u4f9b\u4e86\u4e00\u5957\u4e25\u683c\u7684\u3001\u53ef\u9884\u6d4b\u7684\u7ba1\u7406\u6846\u67b6\uff0c\u4ece\u6839\u672c\u4e0a\u89e3\u51b3\u4e86\u4f20\u7edf\u5e76\u53d1\u7f16\u7a0b\u4e2d\u8d44\u6e90\u6cc4\u6f0f\u3001\u9519\u8bef\u4e22\u5931\u548c\u751f\u547d\u5468\u671f\u6df7\u4e71\u7b49\u6838\u5fc3\u75db\u70b9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"31_%E7%BB%93%E6%9E%84%E5%8C%96%E5%B9%B6%E5%8F%91%E7%9A%84%E8%B5%B7%E6%BA%90\"><\/span><strong>3.1 \u7ed3\u6784\u5316\u5e76\u53d1\u7684\u8d77\u6e90<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7ed3\u6784\u5316\u5e76\u53d1\u51fa\u73b0\u4e4b\u524d\uff0c\u542f\u52a8\u4e00\u4e2a\u540e\u53f0\u4efb\u52a1\u901a\u5e38\u91c7\u7528\u201c\u53d1\u540e\u5373\u5fd8\u201d\uff08fire and forget\uff09\u7684\u6a21\u5f0f <sup>41<\/sup>\u3002\u4f8b\u5982\uff0c\u901a\u8fc7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GlobalScope.launch \u542f\u52a8\u4e00\u4e2a\u534f\u7a0b\uff0c\u6216\u8005\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u7b80\u5355\uff0c\u4f46\u9690\u60a3\u5de8\u5927\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8d44\u6e90\u6cc4\u6f0f<\/strong>\uff1a\u5982\u679c\u542f\u52a8\u7684\u4efb\u52a1\u662f\u4e00\u4e2a\u957f\u8fde\u63a5\u6216\u65e0\u9650\u5faa\u73af\uff0c\u800c\u542f\u52a8\u5b83\u7684\u7ec4\u4ef6\uff08\u5982\u4e00\u4e2a Activity \u6216 ViewModel\uff09\u5df2\u7ecf\u88ab\u9500\u6bc1\uff0c\u8fd9\u4e2a\u4efb\u52a1\u53ef\u80fd\u4f1a\u7ee7\u7eed\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u6d6a\u8d39 CPU \u548c\u5185\u5b58\u8d44\u6e90 <sup>11<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u4e22\u5931<\/strong>\uff1a\u5982\u679c\u8fd9\u4e2a\u540e\u53f0\u4efb\u52a1\u629b\u51fa\u5f02\u5e38\uff0c\u800c\u6ca1\u6709\u4e13\u95e8\u7684\u673a\u5236\u6765\u6355\u83b7\u5b83\uff0c\u8fd9\u4e2a\u5f02\u5e38\u53ef\u80fd\u5c31\u4f1a\u88ab\u201c\u541e\u6389\u201d\uff0c\u5bfc\u81f4\u95ee\u9898\u96be\u4ee5\u88ab\u53d1\u73b0\u548c\u8c03\u8bd5 <sup>11<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u751f\u547d\u5468\u671f\u6df7\u4e71<\/strong>\uff1a\u5f00\u53d1\u8005\u9700\u8981\u624b\u52a8\u7ba1\u7406\u8fd9\u4e9b\u4efb\u52a1\u7684\u751f\u547d\u5468\u671f\uff0c\u4f8b\u5982\uff0c\u5728\u7ec4\u4ef6\u9500\u6bc1\u65f6\uff0c\u9700\u8981\u6301\u6709\u5bf9\u4efb\u52a1\u7684\u5f15\u7528\u5e76\u663e\u5f0f\u5730\u53d6\u6d88\u5b83\uff0c\u8fd9\u975e\u5e38\u5bb9\u6613\u51fa\u9519\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0cKotlin \u534f\u7a0b\u7684\u8bbe\u8ba1\u8005\uff0c\u7279\u522b\u662f Roman Elizarov\uff0c\u4e0e Nathaniel J. Smith \u7b49\u4eba\u5171\u540c\u63a8\u5e7f\u4e86\u201c\u7ed3\u6784\u5316\u5e76\u53d1\u201d\u8fd9\u4e00\u6982\u5ff5 <sup>42<\/sup>\u3002\u5176\u6838\u5fc3\u601d\u60f3\u975e\u5e38\u7b80\u5355\uff0c\u5374\u6781\u4e3a\u5f3a\u5927\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4efb\u4f55\u5e76\u53d1\u4efb\u52a1\u7684\u751f\u547d\u5468\u671f\uff0c\u90fd\u5e94\u8be5\u88ab\u7ed1\u5b9a\u5230\u4e00\u4e2a\u660e\u786e\u7684\u8bed\u6cd5\u4f5c\u7528\u57df\uff08syntactic scope\uff09\u4e0a<\/strong> <sup>42<\/sup>\u3002\u8fd9\u4e2a\u4f5c\u7528\u57df\u5b9a\u4e49\u4e86\u4efb\u52a1\u7684\u8d77\u70b9\u548c\u7ec8\u70b9\u3002\u5f53\u7a0b\u5e8f\u7684\u63a7\u5236\u6d41\u79bb\u5f00\u8fd9\u4e2a\u4f5c\u7528\u57df\u65f6\uff0c\u6240\u6709\u5728\u8be5\u4f5c\u7528\u57df\u5185\u542f\u52a8\u7684\u5e76\u53d1\u4efb\u52a1\u90fd\u5fc5\u987b\u4fdd\u8bc1\u5df2\u7ecf\u7ed3\u675f\u3002\u8fd9\u4e0e\u7ed3\u6784\u5316\u7f16\u7a0b\u7528<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if\/else, for, while \u7b49\u7ed3\u6784\u53d6\u4ee3 goto \u8bed\u53e5\u7684\u7406\u5ff5\u5982\u51fa\u4e00\u8f99\uff0c\u65e8\u5728\u8ba9\u7a0b\u5e8f\u7684\u5e76\u53d1\u884c\u4e3a\u4e0e\u5176\u4ee3\u7801\u7684\u9759\u6001\u7ed3\u6784\u4fdd\u6301\u4e00\u81f4\uff0c\u4ece\u800c\u53d8\u5f97\u6613\u4e8e\u7406\u89e3\u548c\u63a8\u7406 <sup>45<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Kotlin \u534f\u7a0b\u4e2d\uff0c\u8fd9\u4e00\u7406\u5ff5\u901a\u8fc7\u5f3a\u5236\u6240\u6709\u534f\u7a0b\u6784\u5efa\u5668\uff08\u5982 launch, async\uff09\u90fd\u5fc5\u987b\u4f5c\u4e3a CoroutineScope \u7684\u6269\u5c55\u51fd\u6570\u6765\u8c03\u7528\uff0c\u4ece\u800c\u5728\u8bed\u8a00\u5c42\u9762\u5f97\u5230\u4fdd\u969c <sup>44<\/sup>\u3002\u8fd9\u4ece\u6839\u672c\u4e0a\u675c\u7edd\u4e86\u5728\u201c\u7a7a\u4e2d\u201d\u968f\u610f\u542f\u52a8\u534f\u7a0b\u7684\u53ef\u80fd\uff0c\u6bcf\u4e00\u4e2a\u534f\u7a0b\u90fd\u5fc5\u987b\u5f52\u5c5e\u4e8e\u4e00\u4e2a\u660e\u786e\u7684\u201c\u7ba1\u8f96\u8303\u56f4\u201d <sup>44<\/sup>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"32_CoroutineScope_%E4%B8%8E_Job_%E5%B1%82%E7%BA%A7%E7%BB%93%E6%9E%84\"><\/span><strong>3.2 CoroutineScope \u4e0e Job \u5c42\u7ea7\u7ed3\u6784<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CoroutineScope \u662f\u5b9e\u73b0\u7ed3\u6784\u5316\u5e76\u53d1\u7684\u6838\u5fc3\u5de5\u5177\u3002\u5b83\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u66f4\u662f\u4e00\u4e2a\u5b9a\u4e49\u4e86\u534f\u7a0b\u751f\u547d\u5468\u671f\u8fb9\u754c\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668 <sup>46<\/sup>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Job \u5143\u7d20<\/strong>\uff1a\u4e00\u4e2a CoroutineScope \u7684 coroutineContext \u4e2d\uff0c\u5fc5\u987b\u5305\u542b\u4e00\u4e2a Job \u5b9e\u4f8b <sup>24<\/sup>\u3002\u8fd9\u4e2a<br>Job \u5bf9\u8c61\u662f\u534f\u7a0b\u7684\u751f\u547d\u5468\u671f\u53e5\u67c4\uff0c\u4ee3\u8868\u4e86\u4e00\u4e2a\u53ef\u53d6\u6d88\u7684\u5de5\u4f5c\u5355\u5143\uff0c\u5e76\u53ef\u4ee5\u8ffd\u8e2a\u5176\u72b6\u6001\uff08\u5982 active, completed, cancelled\uff09\u3002<\/li>\n\n\n\n<li><strong>\u7236\u5b50\u5173\u7cfb<\/strong>\uff1a\u5f53\u5728\u4e00\u4e2a CoroutineScope \u5185\u4f7f\u7528 launch \u6216 async \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u534f\u7a0b\u65f6\uff0c\u8fd9\u4e2a\u65b0\u534f\u7a0b\u4f1a\u83b7\u5f97\u4e00\u4e2a\u65b0\u7684 Job \u5b9e\u4f8b\u3002\u540c\u65f6\uff0c\u8fd9\u4e2a\u65b0\u7684\u5b50 Job \u4f1a\u88ab\u81ea\u52a8\u6dfb\u52a0\u4e3a\u7236 CoroutineScope \u4e2d Job \u7684\u4e00\u4e2a\u5b50\u8282\u70b9 <sup>44<\/sup>\u3002\u8fd9\u6837\uff0c\u5c31\u81ea\u7136\u5f62\u6210\u4e86\u4e00\u4e2a\u6811\u72b6\u7684\u3001\u5c42\u7ea7\u5316\u7684<br>Job \u7ed3\u6784\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a Job \u5c42\u7ea7\u7ed3\u6784\u63d0\u4f9b\u4e86\u4e24\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u4fdd\u8bc1\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7b49\u5f85\u5b50\u4efb\u52a1\u5b8c\u6210<\/strong>\uff1a\u4e00\u4e2a\u7236 Job\uff08\u4ee5\u53ca\u5b83\u6240\u5728\u7684 coroutineScope\uff09\u5728\u5176\u6240\u6709\u5b50 Job \u5168\u90e8\u5b8c\u6210\uff08\u65e0\u8bba\u662f\u6210\u529f\u3001\u5931\u8d25\u8fd8\u662f\u88ab\u53d6\u6d88\uff09\u4e4b\u524d\uff0c\u81ea\u8eab\u4e0d\u4f1a\u8fdb\u5165\u201c\u5b8c\u6210\u201d\u72b6\u6001 <sup>11<\/sup>\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5f53\u4e00\u4e2a\u4f5c\u7528\u57df\u5757\uff08\u5982<br>coroutineScope {&#8230; }\uff09\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fe1\u5176\u5185\u90e8\u542f\u52a8\u7684\u6240\u6709\u5e76\u53d1\u4efb\u52a1\u90fd\u5df2\u7ecf\u7ed3\u675f\u3002<\/li>\n\n\n\n<li><strong>\u53d6\u6d88\u64cd\u4f5c\u7684\u4f20\u64ad<\/strong>\uff1a\u53d6\u6d88\u4e00\u4e2a\u7236 Job\uff0c\u4f1a\u81ea\u52a8\u5730\u3001\u9012\u5f52\u5730\u53d6\u6d88\u5176\u6240\u6709\u7684\u5b50 Job <sup>3<\/sup>\u3002\u8fd9\u4f7f\u5f97\u6e05\u7406\u4e00\u7ec4\u76f8\u5173\u7684\u5e76\u53d1\u4efb\u52a1\u53d8\u5f97\u5f02\u5e38\u7b80\u5355\u548c\u53ef\u9760\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"33_%E5%8F%AF%E9%A2%84%E6%B5%8B%E7%9A%84%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%EF%BC%9A%E5%BC%82%E5%B8%B8%E4%BC%A0%E6%92%AD\"><\/span><strong>3.3 \u53ef\u9884\u6d4b\u7684\u9519\u8bef\u5904\u7406\uff1a\u5f02\u5e38\u4f20\u64ad<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Job \u5c42\u7ea7\u7ed3\u6784\u4e5f\u4e3a\u5f02\u5e38\u5904\u7406\u63d0\u4f9b\u4e86\u6e05\u6670\u3001\u53ef\u9884\u6d4b\u7684\u89c4\u5219\u3002\u5728\u4e00\u4e2a\u6807\u51c6\u7684 Job \u5c42\u7ea7\u4e2d\uff08\u5373\u672a\u4f7f\u7528 SupervisorJob \u7684\u60c5\u51b5\uff09\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b50\u4efb\u52a1\u5931\u8d25<\/strong>\uff1a\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u5b50\u534f\u7a0b\u56e0\u4e3a\u4e00\u4e2a\u672a\u88ab\u6355\u83b7\u7684\u5f02\u5e38\uff08CancellationException \u9664\u5916\uff09\u800c\u5931\u8d25\uff0c\u5b83\u4f1a\u7acb\u5373\u901a\u77e5\u5176\u7236 Job <sup>48<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u7ea7\u8054\u5931\u8d25<\/strong>\uff1a\u7236 Job \u5728\u6536\u5230\u5b50\u4efb\u52a1\u7684\u5931\u8d25\u901a\u77e5\u540e\uff0c\u4f1a\u7acb\u5373\u53d6\u6d88\u81ea\u5df1\uff0c\u5e76\u5c06\u8fd9\u4e2a\u5f02\u5e38\u4f5c\u4e3a\u53d6\u6d88\u539f\u56e0\u3002<\/li>\n\n\n\n<li><strong>\u4f20\u64ad\u7ed9\u5144\u5f1f\u4efb\u52a1<\/strong>\uff1a\u7236 Job \u7684\u53d6\u6d88\u4f1a\u8fdb\u4e00\u6b65\u4f20\u64ad\u7ed9\u5b83\u6240\u6709\u5176\u4ed6\u7684\u5b50 Job\uff0c\u5373\u6240\u8c13\u7684\u201c\u5144\u5f1f\u4efb\u52a1\u201d <sup>44<\/sup>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u201c\u4e00\u4eba\u5931\u8d25\uff0c\u5168\u5bb6\u906d\u6b83\u201d\u7684**\u5feb\u901f\u5931\u8d25\uff08fail-fast\uff09**\u884c\u4e3a\u662f\u7ed3\u6784\u5316\u5e76\u53d1\u7684\u4e00\u4e2a\u5173\u952e\u7279\u6027\u3002\u5b83\u786e\u4fdd\u4e86\u4e00\u4e2a\u7531\u591a\u4e2a\u90e8\u5206\u7ec4\u6210\u7684\u590d\u5408\u5e76\u53d1\u64cd\u4f5c\uff0c\u5728\u4efb\u4f55\u4e00\u90e8\u5206\u5931\u8d25\u65f6\uff0c\u6574\u4e2a\u64cd\u4f5c\u90fd\u4f1a\u88ab\u7ec8\u6b62\uff0c\u4ece\u800c\u907f\u514d\u7cfb\u7edf\u8fdb\u5165\u4e00\u79cd\u4e0d\u4e00\u81f4\u7684\u3001\u90e8\u5206\u5931\u8d25\u7684\u72b6\u6001\u3002\u7531\u4e8e\u5f02\u5e38\u6700\u7ec8\u4f1a\u4f20\u64ad\u5230\u7236\u4f5c\u7528\u57df\uff0c\u6211\u4eec\u53ef\u4ee5\u50cf\u5904\u7406\u540c\u6b65\u4ee3\u7801\u4e00\u6837\uff0c\u5728\u4f5c\u7528\u57df\u7684\u5916\u90e8\u4f7f\u7528\u4e00\u4e2a try-catch \u5757\u6765\u96c6\u4e2d\u5904\u7406\u6240\u6709\u53ef\u80fd\u7684\u5e76\u53d1\u9519\u8bef\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u9519\u8bef\u5904\u7406\u903b\u8f91 <sup>3<\/sup>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"34_%E4%BC%98%E9%9B%85%E7%9A%84%E7%BB%88%E6%AD%A2%EF%BC%9A%E5%8F%96%E6%B6%88%E4%BC%A0%E6%92%AD\"><\/span><strong>3.4 \u4f18\u96c5\u7684\u7ec8\u6b62\uff1a\u53d6\u6d88\u4f20\u64ad<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u5f02\u5e38\u4f20\u64ad\u7c7b\u4f3c\uff0c\u663e\u5f0f\u7684\u53d6\u6d88\u64cd\u4f5c\u4e5f\u4f1a\u6cbf\u7740 Job \u5c42\u7ea7\u7ed3\u6784\u8fdb\u884c\u4f20\u64ad\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7236\u4efb\u52a1\u53d6\u6d88<\/strong>\uff1a\u5f53\u4e00\u4e2a\u7236 Job \u88ab\u663e\u5f0f\u53d6\u6d88\u65f6\uff08\u4f8b\u5982\uff0c\u8c03\u7528 scope.cancel()\uff09\uff0c\u8fd9\u4e2a\u53d6\u6d88\u4fe1\u53f7\u4f1a\u7acb\u5373\u3001\u9012\u5f52\u5730\u5411\u4e0b\u4f20\u64ad\u7ed9\u5b83\u7684\u6240\u6709\u5b50 Job <sup>41<\/sup>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Android \u5f00\u53d1\u4e2d\u7684 ViewModelScope \u662f\u8fd9\u4e00\u673a\u5236\u7684\u7edd\u4f73\u5b9e\u8df5\u8303\u4f8b <sup>3<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ViewModelScope \u662f\u4e00\u4e2a\u4e0e ViewModel \u751f\u547d\u5468\u671f\u7ed1\u5b9a\u7684 CoroutineScope\u3002\u5f53\u7528\u6237\u79bb\u5f00\u67d0\u4e2a\u754c\u9762\uff0c\u5bf9\u5e94\u7684 ViewModel \u5373\u5c06\u88ab\u9500\u6bc1\u65f6\uff0c\u5176 onCleared() \u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\uff0cViewModelScope \u4e5f\u4f1a\u968f\u4e4b\u88ab\u81ea\u52a8\u53d6\u6d88\u3002\u8fd9\u4e00\u64cd\u4f5c\u4f1a\u7acb\u5373\u53d6\u6d88\u6240\u6709\u5728\u8be5 ViewModel \u4e2d\u542f\u52a8\u7684\u3001\u4ecd\u5728\u8fd0\u884c\u7684\u534f\u7a0b\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u6216\u6570\u636e\u5e93\u67e5\u8be2\u3002\u8fd9\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u56e0\u754c\u9762\u9500\u6bc1\u540e\u540e\u53f0\u4efb\u52a1\u4ecd\u5728\u8fd0\u884c\u800c\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f\u548c\u8d44\u6e90\u6d6a\u8d39\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u534f\u7a0b\u7684\u53d6\u6d88\u662f<strong>\u534f\u4f5c\u5f0f<\/strong>\u7684\u3002\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u534f\u7a0b\u5fc5\u987b\u4e3b\u52a8\u68c0\u67e5\u81ea\u5df1\u662f\u5426\u88ab\u53d6\u6d88\uff0c\u624d\u80fd\u53ca\u65f6\u54cd\u5e94\u3002\u8fd9\u901a\u5e38\u901a\u8fc7\u8c03\u7528 kotlinx.coroutines \u5e93\u4e2d\u53ef\u6302\u8d77\u7684\u51fd\u6570\uff08\u5982 delay, yield, withContext \u7b49\uff0c\u5b83\u4eec\u5185\u90e8\u90fd\u5305\u542b\u53d6\u6d88\u68c0\u67e5\uff09\u6765\u5b9e\u73b0\u3002\u5982\u679c\u4e00\u4e2a\u534f\u7a0b\u9677\u5165\u4e86\u4e00\u4e2a\u4e0d\u5305\u542b\u4efb\u4f55\u6302\u8d77\u70b9\u7684\u3001\u7eaf\u8ba1\u7b97\u7684\u5bc6\u96c6\u5faa\u73af\u4e2d\uff0c\u90a3\u4e48\u5373\u4f7f\u5176\u7236 Job \u5df2\u88ab\u53d6\u6d88\uff0c\u5b83\u4e5f\u53ef\u80fd\u65e0\u6cd5\u7acb\u5373\u505c\u6b62\uff0c\u76f4\u5230\u4e0b\u4e00\u6b21\u9047\u5230\u6302\u8d77\u70b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed3\u6784\u5316\u5e76\u53d1\u7684\u5f15\u5165\uff0c\u6807\u5fd7\u7740\u5e76\u53d1\u4efb\u52a1\u7ba1\u7406\u4ece\u4e00\u79cd\u547d\u4ee4\u5f0f\u7684\u3001\u5f00\u53d1\u8005\u4e2a\u4f53\u8d1f\u8d23\u7684\u6a21\u5f0f\uff0c\u8f6c\u53d8\u4e3a\u4e00\u79cd\u58f0\u660e\u5f0f\u7684\u3001\u57fa\u4e8e\u4f5c\u7528\u57df\u7b56\u7565\u7684\u6a21\u5f0f\u3002\u5728\u65e7\u6a21\u5f0f\u4e0b\uff0c\u5f00\u53d1\u8005\u542f\u52a8\u4e00\u4e2a\u4efb\u52a1\u540e\uff0c\u9700\u8981\u547d\u4ee4\u5f0f\u5730\u4fdd\u5b58\u5176\u53e5\u67c4\uff0c\u5728\u5408\u9002\u7684\u65f6\u673a\u624b\u52a8\u53d6\u6d88\uff0c\u5e76\u5355\u72ec\u5904\u7406\u5176\u9519\u8bef <sup>41<\/sup>\u3002\u800c\u5728\u7ed3\u6784\u5316\u5e76\u53d1\u6a21\u578b\u4e0b\uff0c\u5f00\u53d1\u8005\u901a\u8fc7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u58f0\u660e<\/strong>\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u751f\u547d\u5468\u671f\u7684 CoroutineScope\uff08\u5982 viewModelScope\uff09\uff0c\u6765\u5b9a\u4e49\u4e00\u5957\u5e76\u53d1\u7b56\u7565\u3002\u542f\u52a8\u534f\u7a0b\u7684\u884c\u4e3a\uff08launch\uff09\u5c31\u7b49\u540c\u4e8e\u8ba9\u8be5\u4efb\u52a1<strong>\u8ba2\u9605<\/strong>\u4e86\u5176\u6240\u5728\u4f5c\u7528\u57df\u7684\u751f\u547d\u5468\u671f\u3001\u53d6\u6d88\u548c\u9519\u8bef\u5904\u7406\u7b56\u7565\u3002\u8fd9\u79cd\u4ece\u201c\u5982\u4f55\u7ba1\u7406\u8fd9\u4e2a\u4efb\u52a1\u201d\u5230\u201c\u8fd9\u4e2a\u7ec4\u4ef6\u5185\u7684\u6240\u6709\u4efb\u52a1\u9075\u5faa\u4f55\u79cd\u7b56\u7565\u201d\u7684\u8f6c\u53d8\uff0c\u662f\u5e76\u53d1\u7f16\u7a0b\u601d\u7ef4\u4e0a\u7684\u4e00\u6b21\u91cd\u5927\u98de\u8dc3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u5b9e\u73b0\u8fd9\u4e00\u62bd\u8c61\u6982\u5ff5\u7684\u5177\u4f53\u6570\u636e\u7ed3\u6784\uff0c\u6b63\u662f Job \u7684\u7236\u5b50\u5c42\u7ea7\u3002Job \u63a5\u53e3\u53ca\u5176\u5f62\u6210\u6811\u72b6\u7ed3\u6784\u7684\u80fd\u529b\uff0c\u662f\u7ed3\u6784\u5316\u5e76\u53d1\u5f97\u4ee5\u5728\u5e93\u4e2d\u5177\u4f53\u5b9e\u73b0\u7684\u7269\u7406\u57fa\u7840 <sup>44<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">launch \u6784\u5efa\u5668\u8d1f\u8d23\u5c06\u65b0\u521b\u5efa\u7684\u5b50 Job \u6302\u8f7d\u5230\u7236 Job \u4e0a\uff1bJob.cancel() \u7684\u5b9e\u73b0\u5219\u904d\u5386\u8fd9\u4e2a\u5b50\u8282\u70b9\u5217\u8868\u6765\u4f20\u64ad\u53d6\u6d88\u4fe1\u53f7\u3002\u56e0\u6b64\uff0c\u7ed3\u6784\u5316\u5e76\u53d1\u7684\u6240\u6709\u4fdd\u8bc1\u2014\u2014\u7b49\u5f85\u5b50\u5973\u3001\u4f20\u64ad\u53d6\u6d88\u3001\u4f20\u64ad\u5f02\u5e38\u2014\u2014\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u5c42\u7ea7\u5316 Job \u6570\u636e\u7ed3\u6784\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u6765\u5b9e\u73b0\u7684\u3002Job \u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u4efb\u52a1\u7684\u53e5\u67c4\uff0c\u5b83\u66f4\u662f\u4e00\u4e2a\u5e76\u53d1\u6267\u884c\u56fe\u4e2d\u7684\u4e00\u4e2a\u8282\u70b9\uff0c\u627f\u8f7d\u7740\u7ed3\u6784\u5316\u7684\u7236\u5b50\u5173\u7cfb\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E5%9B%9B%E8%8A%82%EF%BC%9A%E6%89%A7%E8%A1%8C%E6%A1%86%E6%9E%B6%EF%BC%9ACoroutineContext_%E4%B8%8E_Dispatcher\"><\/span><strong>\u7b2c\u56db\u8282\uff1a\u6267\u884c\u6846\u67b6\uff1aCoroutineContext \u4e0e Dispatcher<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7406\u89e3\u4e86\u534f\u7a0b\u7684\u5e95\u5c42\u6302\u8d77\u673a\u5236\u548c\u4e0a\u5c42\u67b6\u6784\u54f2\u5b66\u4e4b\u540e\uff0c\u672c\u8282\u5c06\u805a\u7126\u4e8e\u534f\u7a0b\u7684\u8fd0\u884c\u65f6\u73af\u5883\uff0c\u5373\u63a7\u5236\u534f\u7a0b\u201c\u5982\u4f55\u201d\u4ee5\u53ca\u201c\u5728\u54ea\u91cc\u201d\u6267\u884c\u7684\u5173\u952e\u7ec4\u4ef6\uff1aCoroutineContext \u548c CoroutineDispatcher\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"41_CoroutineContext%EF%BC%9A%E5%8D%8F%E7%A8%8B%E7%9A%84%E2%80%9C%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E2%80%9D\"><\/span><strong>4.1 CoroutineContext\uff1a\u534f\u7a0b\u7684\u201c\u73af\u5883\u53d8\u91cf\u201d<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u4e2a\u534f\u7a0b\u90fd\u5728\u4e00\u4e2a\u7279\u5b9a\u7684\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\uff0c\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u7531 CoroutineContext \u7c7b\u578b\u7684\u503c\u8868\u793a\u3002CoroutineContext \u53ef\u4ee5\u88ab\u7406\u89e3\u4e3a\u534f\u7a0b\u7684\u201c\u73af\u5883\u53d8\u91cf\u201d\u96c6\u5408\uff0c\u5b83\u5b9a\u4e49\u4e86\u534f\u7a0b\u7684\u5404\u9879\u884c\u4e3a\u7279\u5f81 <sup>24<\/sup>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49<\/strong>\uff1aCoroutineContext \u662f\u4e00\u4e2a<strong>\u53ef\u7d22\u5f15\u7684\u5143\u7d20\u96c6\u5408<\/strong>\uff08an indexed set of elements\uff09\uff0c\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u3001\u7c7b\u578b\u5b89\u5168\u7684 Map\u3002\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684 Key\uff0c\u7528\u4e8e\u5feb\u901f\u68c0\u7d22 <sup>18<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u6838\u5fc3\u5143\u7d20<\/strong>\uff1a\u4e00\u4e2a CoroutineContext \u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u6838\u5fc3\u5143\u7d20 <sup>24<\/sup>\uff1a<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Job<\/strong>\uff1a\u63a7\u5236\u534f\u7a0b\u7684\u751f\u547d\u5468\u671f\uff0c\u5b9e\u73b0\u7ed3\u6784\u5316\u5e76\u53d1\uff08\u5df2\u5728\u7b2c\u4e09\u8282\u8be6\u8ff0\uff09\u3002<\/li>\n\n\n\n<li><strong>CoroutineDispatcher<\/strong>\uff1a\u51b3\u5b9a\u534f\u7a0b\u5728\u54ea\u4e2a\u7ebf\u7a0b\u6216\u7ebf\u7a0b\u6c60\u4e0a\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>CoroutineName<\/strong>\uff1a\u4e3a\u534f\u7a0b\u6307\u5b9a\u4e00\u4e2a\u540d\u5b57\uff0c\u4e3b\u8981\u7528\u4e8e\u8c03\u8bd5\uff0c\u65b9\u4fbf\u5728\u65e5\u5fd7\u548c\u8c03\u8bd5\u5668\u4e2d\u8bc6\u522b\u4e0d\u540c\u7684\u534f\u7a0b\u3002<\/li>\n\n\n\n<li><strong>CoroutineExceptionHandler<\/strong>\uff1a\u4e00\u4e2a\u4e13\u95e8\u5904\u7406\u672a\u88ab\u6355\u83b7\u7684\u5f02\u5e38\u7684\u5904\u7406\u5668\u3002<\/li>\n\n\n\n<li><strong>\u7ee7\u627f\u4e0e\u7ec4\u5408<\/strong>\uff1aCoroutineContext \u7684\u4e00\u4e2a\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u5176\u7ee7\u627f\u548c\u7ec4\u5408\u80fd\u529b\u3002\u5f53\u5728\u4e00\u4e2a CoroutineScope \u4e2d\u542f\u52a8\u4e00\u4e2a\u5b50\u534f\u7a0b\u65f6\uff0c\u5b50\u534f\u7a0b\u4f1a\u9ed8\u8ba4<strong>\u7ee7\u627f<\/strong>\u7236\u4f5c\u7528\u57df\u7684 CoroutineContext\u3002\u540c\u65f6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u542f\u52a8\u65f6\u901a\u8fc7 + \u64cd\u4f5c\u7b26\u6765<strong>\u8986\u76d6\u6216\u6dfb\u52a0<\/strong>\u65b0\u7684\u4e0a\u4e0b\u6587\u5143\u7d20 <sup>7<\/sup>\u3002\u4f8b\u5982\uff0c<br>launch(Dispatchers.IO + CoroutineName(&#8220;FileDownloader&#8221;)) \u4f1a\u542f\u52a8\u4e00\u4e2a\u5728 IO \u8c03\u5ea6\u5668\u4e0a\u8fd0\u884c\u3001\u4e14\u540d\u4e3a &#8220;FileDownloader&#8221; \u7684\u65b0\u534f\u7a0b\uff0c\u800c\u5176 Job \u5219\u7ee7\u627f\u81ea\u7236\u4f5c\u7528\u57df\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"42_CoroutineDispatcher%EF%BC%9A%E6%89%A7%E8%A1%8C%E7%9A%84%E5%BC%95%E6%93%8E\"><\/span><strong>4.2 CoroutineDispatcher\uff1a\u6267\u884c\u7684\u5f15\u64ce<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CoroutineDispatcher \u662f CoroutineContext \u4e2d\u8d1f\u8d23\u6267\u884c\u8c03\u5ea6\u7684\u6838\u5fc3\u5143\u7d20\u3002\u5b83\u7684\u804c\u8d23\u662f\u51b3\u5b9a\u4e00\u4e2a\u534f\u7a0b\u7684\u4ee3\u7801\uff08\u6216\u8005\u8bf4\uff0c\u5b83\u7684\u7eed\u4f53\u7684\u6062\u590d\u64cd\u4f5c\uff09\u5e94\u8be5\u5728\u54ea\u4e2a\u7ebf\u7a0b\u6216\u7ebf\u7a0b\u6c60\u4e0a\u6267\u884c <sup>10<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CoroutineDispatcher \u5b9e\u73b0\u4e86 ContinuationInterceptor \u63a5\u53e3 <sup>54<\/sup>\u3002\u987e\u540d\u601d\u4e49\uff0c\u5b83\u662f\u4e00\u4e2a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7eed\u4f53\u62e6\u622a\u5668<\/strong>\u3002\u5f53\u4e00\u4e2a\u88ab\u6302\u8d77\u7684\u534f\u7a0b\u9700\u8981\u6062\u590d\u65f6\uff0cresumeWith \u8c03\u7528\u4f1a\u88ab Dispatcher \u62e6\u622a\u3002Dispatcher \u4e0d\u4f1a\u7acb\u5373\u5728\u5f53\u524d\u7ebf\u7a0b\u6267\u884c\u6062\u590d\u64cd\u4f5c\uff0c\u800c\u662f\u5c06\u8fd9\u4e2a\u6062\u590d\u64cd\u4f5c\uff08\u5c01\u88c5\u6210\u4e00\u4e2a Runnable \u5bf9\u8c61\uff09\u63d0\u4ea4\uff08dispatch\uff09\u5230\u5b83\u6240\u7ba1\u7406\u7684\u7ebf\u7a0b\u6216\u7ebf\u7a0b\u6c60\u4e2d\u53bb\u6267\u884c <sup>54<\/sup>\u3002\u6b63\u662f\u8fd9\u4e2a\u62e6\u622a\u548c\u91cd\u65b0\u5206\u6d3e\u7684\u673a\u5236\uff0c\u5b9e\u73b0\u4e86\u534f\u7a0b\u5728\u4e0d\u540c\u7ebf\u7a0b\u95f4\u7684\u5207\u6362\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"43_%E6%8E%8C%E6%8F%A1%E7%BA%BF%E7%A8%8B%E8%B0%83%E5%BA%A6%EF%BC%9ADefault_IO_Main_%E4%B8%8E_Unconfined\"><\/span><strong>4.3 \u638c\u63e1\u7ebf\u7a0b\u8c03\u5ea6\uff1aDefault, IO, Main \u4e0e Unconfined<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">kotlinx.coroutines \u5e93\u63d0\u4f9b\u4e86\u4e00\u7ec4\u9884\u8bbe\u7684 Dispatcher\uff0c\u5b83\u4eec\u9488\u5bf9\u4e0d\u540c\u7684\u5de5\u4f5c\u8d1f\u8f7d\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u662f\u65e5\u5e38\u5f00\u53d1\u4e2d\u6700\u5e38\u7528\u7684\u8c03\u5ea6\u5668\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Dispatchers.Main<\/strong>\uff1a\u6b64\u8c03\u5ea6\u5668\u5c06\u534f\u7a0b\u7684\u6267\u884c\u9650\u5236\u5728\u7279\u5b9a\u7684\u201c\u4e3b\u7ebf\u7a0b\u201d\u4e0a\uff0c\u8fd9\u5728\u62e5\u6709 UI \u7ebf\u7a0b\u7684\u5e73\u53f0\uff08\u5982 Android, JavaFX\uff09\u4e0a\u81f3\u5173\u91cd\u8981\u3002\u6240\u6709\u4e0e UI \u4ea4\u4e92\u7684\u64cd\u4f5c\uff0c\u5982\u66f4\u65b0\u89c6\u56fe\u3001\u54cd\u5e94\u7528\u6237\u70b9\u51fb\u7b49\uff0c\u90fd\u5fc5\u987b\u5728\u6b64\u8c03\u5ea6\u5668\u4e0a\u6267\u884c\uff0c\u4ee5\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168 <sup>24<\/sup>\u3002<\/li>\n\n\n\n<li><strong>Dispatchers.IO<\/strong>\uff1a\u6b64\u8c03\u5ea6\u5668\u4e13\u95e8\u4e3a**I\/O \u5bc6\u96c6\u578b\uff08I\/O-bound\uff09**\u4efb\u52a1\u8bbe\u8ba1\u3002\u5b83\u80cc\u540e\u662f\u4e00\u4e2a\u5171\u4eab\u7684\u3001\u53ef\u6309\u9700\u521b\u5efa\u548c\u6536\u7f29\u7684\u5f39\u6027\u7ebf\u7a0b\u6c60 <sup>52<\/sup>\u3002\u5176\u7ebf\u7a0b\u6c60\u5bb9\u91cf\u901a\u5e38\u8f83\u5927\uff08\u4f8b\u5982\uff0c\u9ed8\u8ba4\u4e0a\u9650\u4e3a 64 \u6216 CPU\u6838\u5fc3\u6570\uff0c\u53d6\u8f83\u5927\u8005\uff09\uff0c\u56e0\u4e3a I\/O \u64cd\u4f5c\uff08\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\uff09\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u5728\u7b49\u5f85\u6570\u636e\uff0c\u800c\u4e0d\u662f\u6d88\u8017 CPU\u3002\u4e00\u4e2a\u5927\u7684\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u5141\u8bb8\u5927\u91cf\u7684 I\/O \u64cd\u4f5c\u5e76\u53d1\u201c\u5728\u9014\u201d\uff0c\u4ece\u800c\u63d0\u9ad8\u7cfb\u7edf\u6574\u4f53\u7684\u541e\u5410\u91cf <sup>24<\/sup>\u3002<\/li>\n\n\n\n<li><strong>Dispatchers.Default<\/strong>\uff1a\u6b64\u8c03\u5ea6\u5668\u4e3a**\u8ba1\u7b97\u5bc6\u96c6\u578b\uff08CPU-bound\uff09**\u4efb\u52a1\u4f18\u5316\u3002\u5b83\u80cc\u540e\u7684\u5171\u4eab\u7ebf\u7a0b\u6c60\u5927\u5c0f\u901a\u5e38\u4e0e\u673a\u5668\u7684 CPU \u6838\u5fc3\u6570\u76f8\u7b49\uff08\u81f3\u5c11\u4e3a 2\uff09<sup>52<\/sup>\u3002\u8fd9\u79cd\u5927\u5c0f\u7684\u8bbe\u7f6e\u662f\u4e3a\u4e86\u8ba9 CPU \u5f97\u5230\u5145\u5206\u5229\u7528\uff0c\u540c\u65f6\u907f\u514d\u56e0\u8fc7\u591a\u7684\u6d3b\u52a8\u7ebf\u7a0b\u5bfc\u81f4\u7684\u9891\u7e41\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3002\u9002\u5408\u6267\u884c\u5982\u5927\u578b\u5217\u8868\u6392\u5e8f\u3001\u590d\u6742 JSON \u89e3\u6790\u3001\u56fe\u50cf\u5904\u7406\u7b49\u7eaf\u8ba1\u7b97\u4efb\u52a1 <sup>24<\/sup>\u3002<\/li>\n\n\n\n<li><strong>Dispatchers.Unconfined<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u3001\u4e0d\u5c06\u534f\u7a0b\u9650\u5236\u5728\u4efb\u4f55\u7279\u5b9a\u7ebf\u7a0b\u7684\u8c03\u5ea6\u5668\u3002\u5f53\u4e00\u4e2a\u534f\u7a0b\u4f7f\u7528 Unconfined \u8c03\u5ea6\u5668\u542f\u52a8\u65f6\uff0c\u5b83\u4f1a\u5728<strong>\u8c03\u7528\u8005\u7ebf\u7a0b<\/strong>\u4e2d\u7acb\u5373\u5f00\u59cb\u6267\u884c\uff0c\u76f4\u5230\u7b2c\u4e00\u4e2a\u6302\u8d77\u70b9\u3002\u5f53\u5b83\u4ece\u6302\u8d77\u4e2d\u6062\u590d\u65f6\uff0c\u5b83\u5c06\u5728<strong>\u6267\u884c\u6062\u590d\u64cd\u4f5c\u7684\u90a3\u4e2a\u7ebf\u7a0b<\/strong>\u4e0a\u7ee7\u7eed\u6267\u884c <sup>10<\/sup>\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2a\u7f51\u7edc\u8bf7\u6c42\u7684\u56de\u8c03\u5728\u67d0\u4e2a\u7f51\u7edc\u5e93\u7684\u540e\u53f0\u7ebf\u7a0b\u4e2d\u89e6\u53d1\u4e86<br>resume\uff0c\u90a3\u4e48\u534f\u7a0b\u5c31\u4f1a\u5728\u8be5\u540e\u53f0\u7ebf\u7a0b\u4e2d\u7ee7\u7eed\u6267\u884c\u3002\u8fd9\u79cd\u4e0d\u786e\u5b9a\u7684\u7ebf\u7a0b\u884c\u4e3a\u4f7f\u5f97 Unconfined \u8c03\u5ea6\u5668\u4e0d\u9002\u5408\u7528\u4e8e\u66f4\u65b0UI\u6216\u64cd\u4f5c\u7ebf\u7a0b\u5c01\u95ed\u7684\u5171\u4eab\u6570\u636e\uff0c\u5b83\u4e3b\u8981\u7528\u4e8e\u67d0\u4e9b\u4e0d\u9700\u8981\u7ebf\u7a0b\u5207\u6362\u5f00\u9500\u4e14\u4e0d\u5173\u5fc3\u6267\u884c\u7ebf\u7a0b\u7684\u7279\u6b8a\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u8868\u603b\u7ed3\u4e86\u4e3b\u8981 Dispatcher \u7684\u7279\u6027\u548c\u9002\u7528\u573a\u666f\uff0c\u4e3a\u5f00\u53d1\u8005\u5728\u5b9e\u8df5\u4e2d\u505a\u51fa\u6b63\u786e\u9009\u62e9\u63d0\u4f9b\u4e86\u6307\u5f15\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Dispatcher<\/td><td>\u5185\u90e8\u673a\u5236<\/td><td>\u4e3b\u8981\u7528\u4f8b<\/td><td>\u6ce8\u610f\u4e8b\u9879\/\u6700\u4f73\u5b9e\u8df5<\/td><\/tr><tr><td><strong>Dispatchers.Main<\/strong><\/td><td>\u5355\u4e00\u7684\u3001\u5e73\u53f0\u7684 UI \u7ebf\u7a0b<\/td><td>UI \u4ea4\u4e92\u3001\u66f4\u65b0 LiveData\u3001\u5feb\u901f\u7684 UI \u76f8\u5173\u8ba1\u7b97<\/td><td>\u4e25\u7981\u6267\u884c\u4efb\u4f55\u963b\u585e\u6216\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u5426\u5219\u4f1a\u51bb\u7ed3 UI\u3002<\/td><\/tr><tr><td><strong>Dispatchers.IO<\/strong><\/td><td>\u5171\u4eab\u7684\u3001\u5f39\u6027\u7684\u3001\u5927\u5bb9\u91cf\u7ebf\u7a0b\u6c60<\/td><td>\u7f51\u7edc\u8bf7\u6c42\u3001\u6570\u636e\u5e93\u8bbf\u95ee\u3001\u6587\u4ef6\u8bfb\u5199\u7b49\u6240\u6709\u963b\u585e\u6027 I\/O \u64cd\u4f5c<\/td><td>\u4e0d\u5e94\u7528\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5360\u7528\u4e3a\u963b\u585e\u64cd\u4f5c\u51c6\u5907\u7684\u7ebf\u7a0b\u3002<\/td><\/tr><tr><td><strong>Dispatchers.Default<\/strong><\/td><td>\u5171\u4eab\u7684\u3001\u5927\u5c0f\u7b49\u4e8e CPU \u6838\u5fc3\u6570\u7684\u7ebf\u7a0b\u6c60<\/td><td>\u5217\u8868\u6392\u5e8f\u3001JSON \u89e3\u6790\u3001\u590d\u6742\u8ba1\u7b97\u7b49 CPU \u5bc6\u96c6\u578b\u4efb\u52a1<\/td><td>\u4e25\u7981\u5728\u6b64\u8c03\u5ea6\u5668\u4e0a\u6267\u884c\u963b\u585e\u6027 I\/O\uff0c\u5426\u5219\u4f1a\u8017\u5c3d\u5b9d\u8d35\u7684\u8ba1\u7b97\u7ebf\u7a0b\uff0c\u5bfc\u81f4 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u201c\u6328\u997f\u201d\u3002<\/td><\/tr><tr><td><strong>Dispatchers.Unconfined<\/strong><\/td><td>\u4e0d\u7ed1\u5b9a\u4efb\u4f55\u7ebf\u7a0b\u6c60\uff0c\u5728\u8c03\u7528\u8005\u7ebf\u7a0b\u542f\u52a8\uff0c\u5728\u6062\u590d\u8005\u7ebf\u7a0b\u7ee7\u7eed<\/td><td>\u67d0\u4e9b\u9ad8\u7ea7\u573a\u666f\uff0c\u6216\u5f53\u64cd\u4f5c\u5fc5\u987b\u7acb\u5373\u6267\u884c\u4e14\u4e0d\u5173\u5fc3\u7ebf\u7a0b\u65f6<\/td><td>\u4e0d\u5e94\u5728\u901a\u7528\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u4e0d\u53ef\u9884\u6d4b\uff0c\u5bb9\u6613\u51fa\u9519\u3002<\/td><\/tr><tr><td><strong>newSingleThreadContext<\/strong><\/td><td>\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u3001\u4e13\u7528\u7684\u5355\u7ebf\u7a0b\u6267\u884c\u5668<\/td><td>\u9700\u8981\u5c06\u4e00\u7cfb\u5217\u4efb\u52a1\u4e25\u683c\u4e32\u884c\u5316\u5728\u7279\u5b9a\u540e\u53f0\u7ebf\u7a0b\u65f6<\/td><td>\u7ebf\u7a0b\u662f\u6602\u8d35\u8d44\u6e90\uff0c\u521b\u5efa\u540e\u5fc5\u987b\u901a\u8fc7 close() \u91ca\u653e\uff0c\u6216\u4f5c\u4e3a\u9876\u7ea7\u53d8\u91cf\u91cd\u7528\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6570\u636e\u6765\u6e90: <sup>10<\/sup><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"44_withContext_%E5%87%BD%E6%95%B0%EF%BC%9A%E7%B2%BE%E7%BB%86%E5%8C%96%E7%9A%84%E7%BA%BF%E7%A8%8B%E6%8E%A7%E5%88%B6\"><\/span><strong>4.4 withContext \u51fd\u6570\uff1a\u7cbe\u7ec6\u5316\u7684\u7ebf\u7a0b\u63a7\u5236<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">withContext \u662f\u5728\u534f\u7a0b\u5185\u90e8\u8fdb\u884c\u7ebf\u7a0b\u5207\u6362\u7684\u6807\u51c6\u3001\u60ef\u7528\u65b9\u5f0f <sup>3<\/sup>\u3002\u5b83\u672c\u8eab\u662f\u4e00\u4e2a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">suspend \u51fd\u6570\uff0c\u5176\u5de5\u4f5c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6682\u505c\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u534f\u7a0b\u3002<\/li>\n\n\n\n<li>\u5c06\u5176\u53c2\u6570\u4e2d\u7684\u4ee3\u7801\u5757\uff0c\u5728 withContext \u6307\u5b9a\u7684 Dispatcher \u4e0a\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u7b49\u5f85\u4ee3\u7801\u5757\u6267\u884c\u5b8c\u6210\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u5728\u539f\u59cb\u7684 Dispatcher \u4e0a\u6062\u590d\u4e4b\u524d\u6682\u505c\u7684\u534f\u7a0b\uff0c\u5e76\u5c06\u4ee3\u7801\u5757\u7684\u7ed3\u679c\u4f5c\u4e3a withContext \u7684\u8fd4\u56de\u503c\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">withContext \u7684\u4e00\u4e2a\u5173\u952e\u4f18\u52bf\u662f\u5176\u6027\u80fd\u3002\u5b83\u7ecf\u8fc7\u4e86\u9ad8\u5ea6\u4f18\u5316\uff0c\u4e0e\u7b49\u6548\u7684\u3001\u57fa\u4e8e\u56de\u8c03\u7684\u7ebf\u7a0b\u5207\u6362\u5b9e\u73b0\u76f8\u6bd4\uff0c\u4e0d\u4f1a\u5f15\u5165\u989d\u5916\u7684\u5f00\u9500 <sup>24<\/sup>\u3002\u66f4\u6709\u751a\u8005\uff0c\u5982\u679c\u5728\u4e00\u4e2a\u5df2\u7ecf\u5904\u4e8e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dispatchers.IO \u7684\u534f\u7a0b\u4e2d\u518d\u6b21\u8c03\u7528 withContext(Dispatchers.IO)\uff0c\u534f\u7a0b\u5e93\u8db3\u591f\u667a\u80fd\uff0c\u4f1a\u53d1\u73b0\u65e0\u9700\u5207\u6362\uff0c\u4ece\u800c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u8c03\u5ea6\u5f00\u9500 <sup>24<\/sup>\u3002\u8fd9\u79cd\u6548\u7387\u4f7f\u5176\u6210\u4e3a\u5b9e\u73b0\u7ec6\u7c92\u5ea6\u7ebf\u7a0b\u63a7\u5236\u7684\u9996\u9009\u5de5\u5177\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CoroutineContext \u7684\u8bbe\u8ba1\u4f7f\u5f97\u534f\u7a0b\u7684\u884c\u4e3a\u53d8\u5f97\u53ef\u7ec4\u5408\u548c\u53ef\u914d\u7f6e\u3002\u4f20\u7edf\u51fd\u6570\u884c\u4e3a\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u534f\u7a0b\u7684\u884c\u4e3a\uff08\u7ebf\u7a0b\u3001\u751f\u547d\u5468\u671f\u3001\u9519\u8bef\u5904\u7406\uff09\u5e76\u975e\u786c\u7f16\u7801\uff0c\u800c\u662f\u901a\u8fc7 CoroutineContext \u52a8\u6001\u63d0\u4f9b <sup>24<\/sup>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">+ \u64cd\u4f5c\u7b26\u5141\u8bb8\u5f00\u53d1\u8005\u50cf\u5e94\u7528\u88c5\u9970\u5668\u6a21\u5f0f\u4e00\u6837\uff0c\u5728\u8c03\u7528\u70b9\u52a8\u6001\u5730\u7ec4\u5408\u8fd9\u4e9b\u884c\u4e3a\u7b56\u7565\u3002\u8fd9\u4f7f\u5f97\u5e93\u4f5c\u8005\u53ef\u4ee5\u7f16\u5199\u4e0e\u4e0a\u4e0b\u6587\u65e0\u5173\u7684\u901a\u7528 suspend \u51fd\u6570\uff0c\u800c\u5e94\u7528\u5f00\u53d1\u8005\u5219\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u573a\u666f\u201c\u63d2\u5165\u201d\u6240\u9700\u7684\u6267\u884c\u7b56\u7565\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u5ea6\u7075\u6d3b\u548c\u53ef\u590d\u7528\u7684\u5e76\u53d1\u4ee3\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u5916\uff0cDispatchers.IO \u548c Dispatchers.Default \u7684\u5206\u79bb\uff0c\u4f53\u73b0\u4e86\u5bf9\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u548c\u5de5\u4f5c\u8d1f\u8f7d\u7c7b\u578b\u7684\u6df1\u523b\u7406\u89e3\u3002Default \u7684\u5927\u5c0f\u4e0e CPU \u6838\u5fc3\u6570\u5339\u914d\uff0c\u662f\u4e3a\u4e86\u907f\u514d\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u56e0\u7ebf\u7a0b\u8fc7\u591a\u800c\u4ea7\u751f\u4e0d\u5fc5\u8981\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u5f00\u9500\u3002\u800c IO \u7684\u5927\u5bb9\u91cf\u7ebf\u7a0b\u6c60\uff0c\u5219\u662f\u4e3a\u4e86\u6700\u5927\u5316 I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u5e76\u53d1\u80fd\u529b\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u4efb\u52a1\u5927\u90e8\u5206\u65f6\u95f4\u5728\u7b49\u5f85\u800c\u975e\u8ba1\u7b97 <sup>52<\/sup>\u3002\u534f\u7a0b\u5e93\u901a\u8fc7\u63d0\u4f9b\u8fd9\u4e9b\u9884\u8bbe\u7684\u3001\u76ee\u6807\u660e\u786e\u7684\u8c03\u5ea6\u5668\uff0c\u5c06\u590d\u6742\u7684\u6027\u80fd\u8c03\u4f18\u95ee\u9898\u4ece\u5f00\u53d1\u8005\u9762\u524d\u62bd\u8c61\u6389\uff0c\u5f15\u5bfc\u5176\u9ed8\u8ba4\u5c31\u8d70\u5411\u4e00\u6761\u9ad8\u6548\u7684\u67b6\u6784\u4e4b\u8def\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%AC%AC%E4%BA%94%E8%8A%82%EF%BC%9A%E5%B9%B6%E5%8F%91%E5%9B%BE%E6%99%AF%E4%B8%AD%E7%9A%84%E6%AF%94%E8%BE%83%E5%88%86%E6%9E%90\"><\/span><strong>\u7b2c\u4e94\u8282\uff1a\u5e76\u53d1\u56fe\u666f\u4e2d\u7684\u6bd4\u8f83\u5206\u6790<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u5168\u9762\u7406\u89e3 Kotlin \u534f\u7a0b\u7684\u4ef7\u503c\uff0c\u6709\u5fc5\u8981\u5c06\u5176\u7f6e\u4e8e\u66f4\u5e7f\u9614\u7684\u5e76\u53d1\u7f16\u7a0b\u6280\u672f\u56fe\u666f\u4e2d\uff0c\u4e0e RxJava \u548c CompletableFuture \u7b49\u4e3b\u6d41\u65b9\u6848\u8fdb\u884c\u6bd4\u8f83\u3002\u8fd9\u79cd\u6bd4\u8f83\u4e0d\u4ec5\u80fd\u63ed\u793a\u534f\u7a0b\u7684\u72ec\u7279\u4f18\u52bf\uff0c\u4e5f\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u505a\u51fa\u66f4\u660e\u667a\u7684\u6280\u672f\u9009\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"51_%E5%8D%8F%E7%A8%8B_vs_%E5%93%8D%E5%BA%94%E5%BC%8F%E6%B5%81_RxJava%EF%BC%9A%E8%8C%83%E5%BC%8F%E4%B9%8B%E4%BA%89\"><\/span><strong>5.1 \u534f\u7a0b vs. \u54cd\u5e94\u5f0f\u6d41 (RxJava)\uff1a\u8303\u5f0f\u4e4b\u4e89<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u534f\u7a0b\u4e0e RxJava \u7684\u5bf9\u6bd4\uff0c\u672c\u8d28\u4e0a\u662f\u4e24\u79cd\u4e0d\u540c\u7f16\u7a0b\u8303\u5f0f\u7684\u78b0\u649e\uff1a\u547d\u4ee4\u5f0f\/\u987a\u5e8f\u5f0f\u7f16\u7a0b\u4e0e\u58f0\u660e\u5f0f\/\u51fd\u6570\u5f0f\u7f16\u7a0b <sup>56<\/sup>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7f16\u7a0b\u6a21\u578b<\/strong>\uff1a\u534f\u7a0b\u7684\u6838\u5fc3\u4f18\u52bf\u5728\u4e8e\u5176<strong>\u547d\u4ee4\u5f0f\u98ce\u683c<\/strong>\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u50cf\u7f16\u5199\u540c\u6b65\u4ee3\u7801\u4e00\u6837\uff0c\u7528 if\/else, for, try\/catch \u7b49\u719f\u6089\u7684\u7ed3\u6784\u6765\u7ec4\u7ec7\u5f02\u6b65\u903b\u8f91 <sup>4<\/sup>\u3002\u800c RxJava \u5219\u662f\u4e00\u79cd<br><strong>\u58f0\u660e\u5f0f\u8303\u5f0f<\/strong>\uff0c\u5b83\u8981\u6c42\u5f00\u53d1\u8005\u5c06\u4e16\u754c\u770b\u4f5c\u662f\u7531\u5f02\u6b65\u6570\u636e**\u6d41\uff08Stream\uff09**\u7ec4\u6210\u7684\uff0c\u5e76\u901a\u8fc7\u4e00\u7cfb\u5217\u64cd\u4f5c\u7b26\uff08operators\uff09\u6765\u5bf9\u8fd9\u4e9b\u6d41\u8fdb\u884c\u8f6c\u6362\u3001\u8fc7\u6ee4\u548c\u7ec4\u5408\uff0c\u5f62\u6210\u4e00\u4e2a\u5904\u7406\u7ba1\u9053 <sup>56<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u6a21\u578b<\/strong>\uff1asuspend \u51fd\u6570\u5929\u7136\u9002\u7528\u4e8e\u5904\u7406<strong>\u5355\u4e2a\u3001\u4e00\u6b21\u6027<\/strong>\u7684\u5f02\u6b65\u7ed3\u679c\uff08a single-shot result\uff09\u3002\u5bf9\u4e8e\u591a\u4e2a\u503c\u7684\u5e8f\u5217\uff0cKotlin \u534f\u7a0b\u63d0\u4f9b\u4e86 Flow API\uff0c\u5b83\u5728\u6982\u5ff5\u4e0a\u4e0e RxJava \u7684 Observable \u6216 Flowable \u7c7b\u4f3c\uff0c\u90fd\u662f\u5bf9\u51b7\u6570\u636e\u6d41\u7684\u62bd\u8c61 <sup>57<\/sup>\u3002\u800c RxJava \u7684\u6574\u4e2a\u4f53\u7cfb\u90fd\u6784\u5efa\u5728\u6d41\u7684\u6982\u5ff5\u4e4b\u4e0a\u3002<\/li>\n\n\n\n<li><strong>\u80cc\u538b\uff08Backpressure\uff09<\/strong>\uff1a\u80cc\u538b\u662f\u5904\u7406\u751f\u4ea7\u8005\u4ea7\u751f\u6570\u636e\u901f\u5ea6\u5feb\u4e8e\u6d88\u8d39\u8005\u5904\u7406\u901f\u5ea6\u95ee\u9898\u7684\u5173\u952e\u673a\u5236\u3002RxJava \u62e5\u6709\u4e00\u4e2a\u975e\u5e38\u6210\u719f\u548c\u590d\u6742\u7684\u80cc\u538b\u7cfb\u7edf\uff0c\u5c24\u5176\u662f\u5728 Flowable \u7c7b\u578b\u4e2d <sup>56<\/sup>\u3002\u5728\u534f\u7a0b\u4e2d\uff0c\u80cc\u538b\u662f\u901a\u8fc7<br>Flow \u7684<strong>\u6302\u8d77\u7279\u6027<\/strong>\u81ea\u7136\u5b9e\u73b0\u7684\uff1a\u5f53\u6d88\u8d39\u8005\u5904\u7406\u4e0d\u8fc7\u6765\u65f6\uff0c\u6536\u96c6\u5668\uff08collector\uff09\u7684 emit \u8c03\u7528\u4f1a\u6302\u8d77\uff0c\u4ece\u800c\u6682\u505c\u4e0a\u6e38\u751f\u4ea7\u8005\u7684\u6267\u884c <sup>56<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u534f\u7a0b\u4f7f\u7528\u6807\u51c6\u7684 try-catch \u5757\u6765\u5904\u7406\u5f02\u5e38\u3002\u5f97\u76ca\u4e8e\u7ed3\u6784\u5316\u5e76\u53d1\uff0c\u5f02\u5e38\u4f1a\u6cbf\u7740 Job \u5c42\u7ea7\u7ed3\u6784\u81ea\u52a8\u4f20\u64ad\uff0c\u53ef\u4ee5\u88ab\u4e0a\u5c42\u4f5c\u7528\u57df\u96c6\u4e2d\u6355\u83b7 <sup>56<\/sup>\u3002RxJava \u5219\u662f\u5728\u6d41\u7684\u7ba1\u9053\u4e2d\u901a\u8fc7\u4e13\u95e8\u7684\u64cd\u4f5c\u7b26\uff08\u5982<br>onError, retry, onErrorReturn\uff09\u6765\u5904\u7406\u9519\u8bef <sup>56<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u5b66\u4e60\u66f2\u7ebf<\/strong>\uff1a\u7531\u4e8e\u5176\u7f16\u7a0b\u6a21\u578b\u4e0e\u4f20\u7edf\u7684\u540c\u6b65\u7f16\u7a0b\u66f4\u4e3a\u63a5\u8fd1\uff0c\u534f\u7a0b\u901a\u5e38\u88ab\u8ba4\u4e3a\u5b66\u4e60\u66f2\u7ebf\u66f4\u4e3a\u5e73\u7f13 <sup>12<\/sup>\u3002\u800c RxJava \u5219\u8981\u6c42\u5f00\u53d1\u8005\u8fdb\u884c\u4e00\u6b21\u601d\u7ef4\u6a21\u5f0f\u7684\u5f7b\u5e95\u8f6c\u53d8\uff0c\u53bb\u9002\u5e94\u54cd\u5e94\u5f0f\u7f16\u7a0b\u7684\u7406\u5ff5\uff0c\u8fd9\u901a\u5e38\u88ab\u8ba4\u4e3a\u66f4\u5177\u6311\u6218\u6027 <sup>56<\/sup>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"52_%E5%8D%8F%E7%A8%8B_vs_CompletableFuture%EF%BC%9A%E7%8E%B0%E4%BB%A3%E7%AE%80%E6%B4%81%E6%80%A7%E4%B8%8E%E4%BC%A0%E7%BB%9F%E8%83%BD%E5%8A%9B%E7%9A%84%E5%AF%B9%E6%AF%94\"><\/span><strong>5.2 \u534f\u7a0b vs. CompletableFuture\uff1a\u73b0\u4ee3\u7b80\u6d01\u6027\u4e0e\u4f20\u7edf\u80fd\u529b\u7684\u5bf9\u6bd4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CompletableFuture \u662f Java 8 \u5f15\u5165\u7684\uff0c\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u8ba1\u7b97\u7684\u5de5\u5177\uff0c\u5b83\u5728\u5f88\u591a\u65b9\u9762\u662f\u4f20\u7edf Future \u7684\u6f14\u8fdb\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7f16\u7a0b\u6a21\u578b<\/strong>\uff1aCompletableFuture \u4f9d\u8d56\u4e8e\u4e00\u8fde\u4e32\u7684\u65b9\u6cd5\u94fe\uff08\u5982 thenApply, thenCompose, thenCombine\uff09\u6765\u7ec4\u5408\u5f02\u6b65\u4efb\u52a1\uff0c\u8fd9\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u51fd\u6570\u5f0f\u7684\u56de\u8c03\u7ec4\u5408\u3002\u5f53\u903b\u8f91\u590d\u6742\u65f6\uff0c\u8fd9\u79cd\u94fe\u5f0f\u8c03\u7528\u4ecd\u7136\u53ef\u80fd\u53d8\u5f97\u5197\u957f\u548c\u96be\u4ee5\u8ffd\u8e2a <sup>6<\/sup>\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u534f\u7a0b\u63d0\u4f9b\u4e86\u5b8c\u5168\u7ebf\u6027\u7684\u3001\u987a\u5e8f\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u53ef\u8bfb\u6027\u66f4\u9ad8 <sup>6<\/sup>\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1aCompletableFuture \u901a\u8fc7 exceptionally \u548c handle \u7b49\u65b9\u6cd5\u5728\u94fe\u4e2d\u5904\u7406\u5f02\u5e38 <sup>6<\/sup>\u3002\u800c\u534f\u7a0b\u5219\u4f9d\u8d56\u4e8e<br>try-catch\uff0c\u8fd9\u5bf9\u4e8e\u5927\u591a\u6570\u5f00\u53d1\u8005\u6765\u8bf4\u66f4\u4e3a\u76f4\u89c2\u548c\u719f\u6089\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u6784\u5316\u5e76\u53d1<\/strong>\uff1a\u8fd9\u662f\u4e24\u8005\u4e4b\u95f4\u6700\u6839\u672c\u7684\u533a\u522b\u3002CompletableFuture <strong>\u6ca1\u6709\u5185\u5efa\u7684\u7ed3\u6784\u5316\u5e76\u53d1\u6982\u5ff5<\/strong> <sup>57<\/sup>\u3002\u5982\u679c\u4f60\u542f\u52a8\u4e86\u591a\u4e2a<br>CompletableFuture\uff0c\u4f60\u9700\u8981\u624b\u52a8\u7ba1\u7406\u5b83\u4eec\u7684\u751f\u547d\u5468\u671f\uff0c\u4f8b\u5982\u5c06\u5b83\u4eec\u6536\u96c6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 CompletableFuture.allOf() \u6765\u7b49\u5f85\u5b83\u4eec\u5168\u90e8\u5b8c\u6210\u3002\u53d6\u6d88\u64cd\u4f5c\u4e5f\u9700\u8981\u624b\u52a8\u4f20\u64ad\u3002\u800c\u534f\u7a0b\u901a\u8fc7 CoroutineScope \u548c Job \u5c42\u7ea7\uff0c\u5c06\u8fd9\u4e00\u5207\u90fd\u81ea\u52a8\u5316\u4e86\uff0c\u63d0\u4f9b\u4e86\u5f00\u7bb1\u5373\u7528\u7684\u7ed3\u6784\u5316\u751f\u547d\u5468\u671f\u7ba1\u7406\u3002<\/li>\n\n\n\n<li><strong>\u4e92\u64cd\u4f5c\u6027<\/strong>\uff1aKotlin \u534f\u7a0b\u4e0e CompletableFuture \u4e4b\u95f4\u6709\u826f\u597d\u7684\u4e92\u64cd\u4f5c\u6027\u3002kotlinx-coroutines-jdk8 \u5e93\u4e3a CompletableFuture \u63d0\u4f9b\u4e86 .await() \u6269\u5c55\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5176\u65e0\u7f1d\u5730\u6302\u8d77\u5e76\u96c6\u6210\u5230\u534f\u7a0b\u4ee3\u7801\u4e2d <sup>60<\/sup>\u3002\u53cd\u4e4b\uff0c\u4e5f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u534f\u7a0b\u7684\u7ed3\u679c\u5305\u88c5\u6210<br>CompletableFuture \u4ee5\u4fbf\u4e0e\u65e7\u7684 Java API \u4ea4\u4e92\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u8868\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u7684\u6a2a\u5411\u5bf9\u6bd4\u77e9\u9635\uff0c\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6839\u636e\u4e0d\u540c\u7ef4\u5ea6\u8fdb\u884c\u6280\u672f\u9009\u578b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u7279\u6027<\/td><td>\u56de\u8c03 (Callbacks)<\/td><td>CompletableFuture<\/td><td>RxJava<\/td><td>Kotlin \u534f\u7a0b<\/td><\/tr><tr><td><strong>\u7f16\u7a0b\u98ce\u683c<\/strong><\/td><td>\u547d\u4ee4\u5f0f\uff0c\u6df1\u5ea6\u5d4c\u5957<\/td><td>\u58f0\u660e\u5f0f\uff0c\u94fe\u5f0f\u8c03\u7528<\/td><td>\u58f0\u660e\u5f0f\uff0c\u6d41\u5904\u7406\u7ba1\u9053<\/td><td>\u547d\u4ee4\u5f0f\uff0c\u987a\u5e8f\u6267\u884c<\/td><\/tr><tr><td><strong>\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong><\/td><td>\u4f4e (\u56de\u8c03\u5730\u72f1)<\/td><td>\u4e2d<\/td><td>\u4e2d (\u9700\u8981\u7406\u89e3 DSL)<\/td><td>\u9ad8 (\u7c7b\u4f3c\u540c\u6b65\u4ee3\u7801)<\/td><\/tr><tr><td><strong>\u9519\u8bef\u5904\u7406<\/strong><\/td><td>\u5728\u6bcf\u4e2a\u56de\u8c03\u4e2d\u624b\u52a8\u5904\u7406<\/td><td>\u94fe\u5f0f\u64cd\u4f5c\u7b26 (exceptionally)<\/td><td>\u6d41\u64cd\u4f5c\u7b26 (onError)<\/td><td>\u6807\u51c6 try-catch<\/td><\/tr><tr><td><strong>\u751f\u547d\u5468\u671f\/\u53d6\u6d88<\/strong><\/td><td>\u624b\u52a8\u7ba1\u7406<\/td><td>\u624b\u52a8\u7ba1\u7406<\/td><td>\u57fa\u4e8e\u8ba2\u9605 (Subscription)<\/td><td>\u81ea\u52a8\u7ba1\u7406 (\u7ed3\u6784\u5316\u5e76\u53d1)<\/td><\/tr><tr><td><strong>\u6570\u636e\u6a21\u578b<\/strong><\/td><td>\u5355\u4e00\u503c<\/td><td>\u5355\u4e00\u503c<\/td><td>\u6d41 (0..N \u4e2a\u503c)<\/td><td>\u5355\u4e00\u503c (suspend) \u6216 \u6d41 (Flow)<\/td><\/tr><tr><td><strong>\u80cc\u538b<\/strong><\/td><td>\u4e0d\u9002\u7528<\/td><td>\u4e0d\u9002\u7528<\/td><td>\u5185\u5efa<\/td><td>\u901a\u8fc7\u6302\u8d77\u5b9e\u73b0 (Flow\/Channel)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6570\u636e\u6765\u6e90: <sup>4<\/sup><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin \u534f\u7a0b\u5728\u8bbe\u8ba1\u4e0a\u4f53\u73b0\u51fa\u4e00\u79cd\u52a1\u5b9e\u7684\u201c\u4e24\u5168\u5176\u7f8e\u201d\u7684\u7efc\u5408\u601d\u60f3\u3002\u5b83\u901a\u8fc7 suspend \u51fd\u6570\u63d0\u4f9b\u4e86\u547d\u4ee4\u5f0f\u5f02\u6b65\u7f16\u7a0b\u7684\u7b80\u6d01\u6027\uff0c\u8fd9\u4f7f\u5176\u5728\u5904\u7406\u4e00\u6b21\u6027\u8bf7\u6c42\u7b49\u573a\u666f\u4e0b\u6bd4 CompletableFuture \u548c RxJava \u66f4\u5177\u4f18\u52bf\u3002\u540c\u65f6\uff0c\u5b83\u53c8\u901a\u8fc7 Flow API \u63d0\u4f9b\u4e86\u529f\u80fd\u5f3a\u5927\u7684\u54cd\u5e94\u5f0f\u6d41\u5904\u7406\u80fd\u529b\uff0c\u8db3\u4ee5\u5e94\u5bf9 RxJava \u6240\u64c5\u957f\u7684\u590d\u6742\u6570\u636e\u6d41\u573a\u666f <sup>56<\/sup>\u3002\u8fd9\u79cd\u53cc\u91cd\u7279\u6027\u4f7f\u5f97<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">kotlinx.coroutines \u5e93\u66f4\u50cf\u4e00\u4e2a\u5168\u9762\u7684\u5e76\u53d1\u5de5\u5177\u7bb1\uff0c\u800c\u975e\u4e00\u4e2a\u5355\u4e00\u8303\u5f0f\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u4efb\u52a1\u7684\u5177\u4f53\u6027\u8d28\uff08\u4e00\u6b21\u6027\u7ed3\u679c vs. \u8fde\u7eed\u6570\u636e\u6d41\uff09\u5728\u540c\u4e00\u4e2a\u751f\u6001\u7cfb\u7edf\u5185\u9009\u62e9\u6700\u5408\u9002\u7684\u5de5\u5177\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c55\u671b\u672a\u6765\uff0cJVM \u5e76\u53d1\u7f16\u7a0b\u7684\u8d8b\u52bf\u53ef\u80fd\u662f\u8d8b\u540c\u7684\u3002Java \u7684 Project Loom \u9879\u76ee\u5f15\u5165\u7684\u865a\u62df\u7ebf\u7a0b\uff08Virtual Threads\uff09\u4e0e Kotlin \u534f\u7a0b\u65e8\u5728\u89e3\u51b3\u540c\u4e00\u4e2a\u6838\u5fc3\u95ee\u9898\uff1a\u8ba9\u5e76\u53d1\u53d8\u5f97\u5ec9\u4ef7 <sup>57<\/sup>\u3002Loom \u5728 JVM \u5c42\u9762\u901a\u8fc7\u8f7b\u91cf\u7ea7\u6808\u6765\u5b9e\u73b0\uff0c\u800c\u534f\u7a0b\u5219\u5728\u7f16\u8bd1\u5668\u5c42\u9762\u901a\u8fc7\u72b6\u6001\u673a\u6765\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u8fd9\u4e24\u8005\u5e76\u975e\u5fc5\u7136\u7684\u7ade\u4e89\u5173\u7cfb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">kotlinx.coroutines \u5e93\u7684 Dispatcher \u62bd\u8c61\u5c42\uff0c\u4f7f\u5176\u5728\u672a\u6765\u5b8c\u5168\u6709\u53ef\u80fd\u5c06\u5176\u5e95\u5c42\u5b9e\u73b0\u4ece\u4f20\u7edf\u7684\u7ebf\u7a0b\u6c60\u5207\u6362\u4e3a\u865a\u62df\u7ebf\u7a0b\u3002\u5982\u679c\u8fd9\u4e00\u8bbe\u60f3\u6210\u771f\uff0c\u5f00\u53d1\u8005\u5c06\u53ef\u4ee5\u7ee7\u7eed\u4f7f\u7528 Kotlin \u534f\u7a0b\u6240\u63d0\u4f9b\u7684\u4f18\u96c5\u8bed\u6cd5\u548c\u7ed3\u6784\u5316\u5e76\u53d1\u7b49\u6240\u6709\u4e0a\u5c42\u4f18\u52bf\uff0c\u540c\u65f6\u4eab\u53d7\u5230\u6765\u81ea JVM \u5e95\u5c42\u7684\u6027\u80fd\u589e\u76ca <sup>60<\/sup>\u3002\u8fd9\u4f7f\u5f97\u534f\u7a0b\u4e0d\u4ec5\u4ec5\u662f\u5f53\u524d\u7684\u4e00\u4e2a\u4f18\u79c0\u89e3\u51b3\u65b9\u6848\uff0c\u66f4\u53ef\u80fd\u6210\u4e3a\u672a\u6765 JVM \u4e0a\u5404\u79cd\u5e95\u5c42\u5e76\u53d1\u6280\u672f\u7684\u4e00\u4e2a\u7edf\u4e00\u7684\u3001\u7528\u6237\u53cb\u597d\u7684\u9ad8\u7ea7 API\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u5176\u957f\u4e45\u7684\u751f\u547d\u529b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%BB%93%E8%AE%BA\"><\/span><strong>\u7ed3\u8bba<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin \u534f\u7a0b\u4e3a\u5f02\u6b65\u7f16\u7a0b\u5e26\u6765\u7684\u9769\u547d\u6027\u53d8\u9769\uff0c\u5e76\u975e\u6e90\u4e8e\u67d0\u9879\u5355\u4e00\u7684\u6280\u672f\u7a81\u7834\uff0c\u800c\u662f\u4e24\u5927\u6838\u5fc3\u521b\u65b0\u7684\u534f\u540c\u6548\u5e94\u7684\u7ed3\u6676\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u5728\u5fae\u89c2\u5c42\u9762\uff0c\u662f<strong>\u7531\u7f16\u8bd1\u5668\u9a71\u52a8\u7684\u6302\u8d77\u673a\u5236<\/strong>\u3002\u901a\u8fc7\u5c06 suspend \u51fd\u6570\u5728\u7f16\u8bd1\u671f\u8f6c\u6362\u4e3a\u9ad8\u6548\u7684\u3001\u975e\u963b\u585e\u7684\u72b6\u6001\u673a\uff08\u5373\u7eed\u4f53\u4f20\u9012\u98ce\u683c\u7684\u5e94\u7528\uff09\uff0cKotlin \u4ece\u6839\u672c\u4e0a\u89e3\u51b3\u4e86\u5f02\u6b65\u64cd\u4f5c\u7684\u6267\u884c\u6548\u7387\u95ee\u9898\u3002\u8fd9\u4e00\u5e95\u5c42\u673a\u5236\u4f7f\u5f97\u534f\u7a0b\u80fd\u591f\u4ee5\u6781\u4f4e\u7684\u8d44\u6e90\u6210\u672c\u5b9e\u73b0\u6302\u8d77\u548c\u6062\u590d\uff0c\u4ece\u800c\u5728\u4e0d\u963b\u585e\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8fbe\u6210\u9ad8\u5e76\u53d1\uff0c\u8fd9\u662f\u5176\u201c\u8f7b\u91cf\u7ea7\u201d\u7279\u6027\u7684\u6280\u672f\u6839\u6e90\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u6b21\uff0c\u5728\u5b8f\u89c2\u5c42\u9762\uff0c\u662f<strong>\u7531\u5e93\u9a71\u52a8\u7684\u67b6\u6784\u54f2\u5b66<\/strong>\uff0c\u5373<strong>\u7ed3\u6784\u5316\u5e76\u53d1<\/strong>\u3002kotlinx.coroutines \u5e93\u901a\u8fc7 CoroutineScope \u548c\u5c42\u7ea7\u5316\u7684 Job \u7ed3\u6784\uff0c\u4e3a\u5e76\u53d1\u4efb\u52a1\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5065\u58ee\u7684\u3001\u53ef\u9884\u6d4b\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u6846\u67b6\u3002\u8fd9\u5957\u6846\u67b6\u5c06\u9519\u8bef\u5904\u7406\u548c\u53d6\u6d88\u64cd\u4f5c\u4ece\u5f00\u53d1\u8005\u624b\u52a8\u7ba1\u7406\u7684\u7e41\u7410\u4efb\u52a1\uff0c\u8f6c\u53d8\u4e3a\u4f5c\u7528\u57df\u5185\u7684\u81ea\u52a8\u5316\u7b56\u7565\uff0c\u6781\u5927\u5730\u63d0\u5347\u4e86\u5e76\u53d1\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3001\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b63\u662f\u8fd9\u79cd\u5e95\u5c42\u6548\u7387\u4e0e\u4e0a\u5c42\u67b6\u6784\u7684\u5b8c\u7f8e\u7ed3\u5408\uff0c\u6210\u529f\u5730\u5c06\u5f02\u6b65\u7f16\u7a0b\u4e2d\u6700\u68d8\u624b\u7684\u90e8\u5206\uff08\u7ebf\u7a0b\u7ba1\u7406\u3001\u72b6\u6001\u540c\u6b65\u3001\u751f\u547d\u5468\u671f\u63a7\u5236\u3001\u9519\u8bef\u5904\u7406\uff09\u4ece\u5f00\u53d1\u8005\u9762\u524d\u62bd\u8c61\u6389\u4e86\u3002\u5b83\u6700\u7ec8\u5151\u73b0\u4e86\u73b0\u4ee3\u5f02\u6b65\u7f16\u7a0b\u8bbe\u8ba1\u7684\u7ec8\u6781\u627f\u8bfa\uff1a\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u7f16\u5199\u51fa\u65e2\u80fd\u5145\u5206\u5229\u7528\u591a\u6838\u786c\u4ef6\u3001\u5b9e\u73b0\u9ad8\u4f38\u7f29\u6027\uff0c\u53c8\u5982\u540c\u7ecf\u5178\u540c\u6b65\u4ee3\u7801\u4e00\u822c\u7b80\u5355\u3001\u6e05\u6670\u548c\u7a33\u5065\u7684\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c55\u671b\u672a\u6765\uff0c\u968f\u7740 Flow API \u5728\u54cd\u5e94\u5f0f\u6570\u636e\u5904\u7406\u9886\u57df\u7684\u6210\u719f\uff0c\u4ee5\u53ca\u4e0e Java \u5e73\u53f0\u672a\u6765\u7279\u6027\uff08\u5982\u865a\u62df\u7ebf\u7a0b\uff09\u6f5c\u5728\u7684\u6df1\u5ea6\u6574\u5408\uff0cKotlin \u534f\u7a0b\u5df2\u7ecf\u8bc1\u660e\u81ea\u5df1\u4e0d\u4ec5\u662f\u5f53\u4e0b\u89e3\u51b3\u5e76\u53d1\u96be\u9898\u7684\u5229\u5668\uff0c\u66f4\u662f\u4e00\u4e2a\u9762\u5411\u672a\u6765\u7684\u3001\u53ef\u6301\u7eed\u6f14\u8fdb\u7684\u5f3a\u5927\u7f16\u7a0b\u8303\u5f0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BC%95%E7%94%A8%E7%9A%84%E8%91%97%E4%BD%9C\"><\/span><strong>\u5f15\u7528\u7684\u8457\u4f5c<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Coroutines | Kotlin Documentation, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-overview.html\">https:\/\/kotlinlang.org\/docs\/coroutines-overview.html<\/a><\/li>\n\n\n\n<li>Kotlin\/JS and Coroutines solve an issue that isn&#8217;t really an issue? &#8211; Libraries, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/discuss.kotlinlang.org\/t\/kotlin-js-and-coroutines-solve-an-issue-that-isnt-really-an-issue\/15628\">https:\/\/discuss.kotlinlang.org\/t\/kotlin-js-and-coroutines-solve-an-issue-that-isnt-really-an-issue\/15628<\/a><\/li>\n\n\n\n<li>Kotlin coroutines on Android, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/developer.android.com\/kotlin\/coroutines\">https:\/\/developer.android.com\/kotlin\/coroutines<\/a><\/li>\n\n\n\n<li>Asynchronous programming techniques | Kotlin Documentation, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/docs\/async-programming.html\">https:\/\/kotlinlang.org\/docs\/async-programming.html<\/a><\/li>\n\n\n\n<li>Kotlin SuspendCoroutine: A Practical Guide For Developers &#8211; DhiWise, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.dhiwise.com\/blog\/design-converter\/kotlin-suspendcoroutine-a-practical-guide-for-developers\">https:\/\/www.dhiwise.com\/blog\/design-converter\/kotlin-suspendcoroutine-a-practical-guide-for-developers<\/a><\/li>\n\n\n\n<li>Java CompletableFuture vs Kotlin Coroutines | DevLach, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/devlach.com\/blog\/kotlin\/kotlin-coroutines-vs-java-completablefuture\">https:\/\/devlach.com\/blog\/kotlin\/kotlin-coroutines-vs-java-completablefuture<\/a><\/li>\n\n\n\n<li>Kotlin 101: Coroutines Quickly Explained &#8211; Rock the JVM, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/rockthejvm.com\/articles\/kotlin-101-coroutines\">https:\/\/rockthejvm.com\/articles\/kotlin-101-coroutines<\/a><\/li>\n\n\n\n<li>Understanding Kotlin Coroutines for Asynchronous Programming &#8211; DEV Community, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/dev.to\/raufagayevv\/understanding-kotlin-coroutines-for-asynchronous-programming-58df\">https:\/\/dev.to\/raufagayevv\/understanding-kotlin-coroutines-for-asynchronous-programming-58df<\/a><\/li>\n\n\n\n<li>Difference between a thread and a coroutine in Kotlin &#8211; Stack Overflow, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/stackoverflow.com\/questions\/43021816\/difference-between-a-thread-and-a-coroutine-in-kotlin\">https:\/\/stackoverflow.com\/questions\/43021816\/difference-between-a-thread-and-a-coroutine-in-kotlin<\/a><\/li>\n\n\n\n<li>Threads vs Coroutines in Kotlin &#8211; Baeldung, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.baeldung.com\/kotlin\/threads-coroutines\">https:\/\/www.baeldung.com\/kotlin\/threads-coroutines<\/a><\/li>\n\n\n\n<li>Coroutines basics | Kotlin Documentation, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-basics.html\">https:\/\/kotlinlang.org\/docs\/coroutines-basics.html<\/a><\/li>\n\n\n\n<li>Asynchronous Programming and Kotlin Coroutines in Android &#8211; GeeksforGeeks, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.geeksforgeeks.org\/kotlin-coroutines-on-android\/\">https:\/\/www.geeksforgeeks.org\/kotlin-coroutines-on-android\/<\/a><\/li>\n\n\n\n<li>Introduction to Coroutines: What Problems Do They Solve? &#8211; SuperKotlin, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/superkotlin.com\/coroutines\/\">https:\/\/superkotlin.com\/coroutines\/<\/a><\/li>\n\n\n\n<li>Kotlin Coroutines vs Threads Performance Benchmark, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.techyourchance.com\/kotlin-coroutines-vs-threads-performance-benchmark\/\">https:\/\/www.techyourchance.com\/kotlin-coroutines-vs-threads-performance-benchmark\/<\/a><\/li>\n\n\n\n<li>Kotlin Coroutines vs Threads Performance Benchmark : r\/androiddev &#8211; Reddit, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.reddit.com\/r\/androiddev\/comments\/17g9n6x\/kotlin_coroutines_vs_threads_performance_benchmark\/\">https:\/\/www.reddit.com\/r\/androiddev\/comments\/17g9n6x\/kotlin_coroutines_vs_threads_performance_benchmark\/<\/a><\/li>\n\n\n\n<li>Understanding Kotlin Suspend Functions Internally &#8211; droidcon, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.droidcon.com\/2025\/04\/02\/understanding-kotlin-suspend-functions-internally\/\">https:\/\/www.droidcon.com\/2025\/04\/02\/understanding-kotlin-suspend-functions-internally\/<\/a><\/li>\n\n\n\n<li>Understanding Kotlin Coroutines with examples &#8211; DECODE, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/decode.agency\/article\/understanding-kotlin-coroutines\/\">https:\/\/decode.agency\/article\/understanding-kotlin-coroutines\/<\/a><\/li>\n\n\n\n<li>Kotlin Coroutine Continuation | Baeldung on Kotlin, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.baeldung.com\/kotlin\/coroutine-continuation\">https:\/\/www.baeldung.com\/kotlin\/coroutine-continuation<\/a><\/li>\n\n\n\n<li>Kotlin coroutine vs thread? | Sololearn: Learn to code for FREE!, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.sololearn.com\/en\/Discuss\/2062567\/kotlin-coroutine-vs-thread\">https:\/\/www.sololearn.com\/en\/Discuss\/2062567\/kotlin-coroutine-vs-thread<\/a><\/li>\n\n\n\n<li>Kotlin Coroutine to escape callback hell &#8211; Stack Overflow, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/stackoverflow.com\/questions\/48877135\/kotlin-coroutine-to-escape-callback-hell\">https:\/\/stackoverflow.com\/questions\/48877135\/kotlin-coroutine-to-escape-callback-hell<\/a><\/li>\n\n\n\n<li>How to avoid callback hell or &#8220;pyramid of doom&#8221;? &#8211; Kotlin Discussions, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/discuss.kotlinlang.org\/t\/how-to-avoid-callback-hell-or-pyramid-of-doom\/216\">https:\/\/discuss.kotlinlang.org\/t\/how-to-avoid-callback-hell-or-pyramid-of-doom\/216<\/a><\/li>\n\n\n\n<li>Comparing Kotlin Coroutines with Callbacks and RxJava | Lukas &#8230;, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.lukaslechner.com\/comparing-kotlin-coroutines-with-callbacks-and-rxjava\/\">https:\/\/www.lukaslechner.com\/comparing-kotlin-coroutines-with-callbacks-and-rxjava\/<\/a><\/li>\n\n\n\n<li>Kotlin Coroutine Confidence: Untangle Your Async, Ship Safety at Speed by Sam Cooper, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/pragprog.com\/titles\/sckotlin\/kotlin-coroutine-confidence\/\">https:\/\/pragprog.com\/titles\/sckotlin\/kotlin-coroutine-confidence\/<\/a><\/li>\n\n\n\n<li>Improve app performance with Kotlin coroutines | Android Developers, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/developer.android.com\/kotlin\/coroutines\/coroutines-adv\">https:\/\/developer.android.com\/kotlin\/coroutines\/coroutines-adv<\/a><\/li>\n\n\n\n<li>2017 KotlinConf &#8211; Deep dive into Coroutines on JVM &#8211; JetBrains, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/resources.jetbrains.com\/storage\/products\/kotlinconf2017\/slides\/2017+KotlinConf+-+Deep+dive+into+Coroutines+on+JVM.pdf\">https:\/\/resources.jetbrains.com\/storage\/products\/kotlinconf2017\/slides\/2017+KotlinConf+-+Deep+dive+into+Coroutines+on+JVM.pdf<\/a><\/li>\n\n\n\n<li>What Does &#8220;With Continuation&#8221; Mean? (2020) &#8211; Hacker News, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/news.ycombinator.com\/item?id=39309157\">https:\/\/news.ycombinator.com\/item?id=39309157<\/a><\/li>\n\n\n\n<li>Coroutines under the hood &#8211; Kt. Academy, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kt.academy\/article\/cc-under-the-hood\">https:\/\/kt.academy\/article\/cc-under-the-hood<\/a><\/li>\n\n\n\n<li>Asynchronous programming with coroutines &#8211; Kotlin language specification, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/spec\/asynchronous-programming-with-coroutines.html\">https:\/\/kotlinlang.org\/spec\/asynchronous-programming-with-coroutines.html<\/a><\/li>\n\n\n\n<li>Continuation &#8211; Kotlin, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/api\/core\/kotlin-stdlib\/kotlin.coroutines\/-continuation\/\">https:\/\/kotlinlang.org\/api\/core\/kotlin-stdlib\/kotlin.coroutines\/-continuation\/<\/a><\/li>\n\n\n\n<li>Kotlin Continuations &#8211; \u200d Jorge Castillo, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/jorgecastillo.dev\/digging-into-kotlin-continuations\">https:\/\/jorgecastillo.dev\/digging-into-kotlin-continuations<\/a><\/li>\n\n\n\n<li>Calling Kotlin Suspending Functions from Java &#8211; Baeldung, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.baeldung.com\/kotlin\/suspend-functions-from-java\">https:\/\/www.baeldung.com\/kotlin\/suspend-functions-from-java<\/a><\/li>\n\n\n\n<li>Can someone explain to me what Coroutines are? : r\/Kotlin &#8211; Reddit, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.reddit.com\/r\/Kotlin\/comments\/162d2lv\/can_someone_explain_to_me_what_coroutines_are\/\">https:\/\/www.reddit.com\/r\/Kotlin\/comments\/162d2lv\/can_someone_explain_to_me_what_coroutines_are\/<\/a><\/li>\n\n\n\n<li>Are Kotlin Coroutines Just Data Structures Using Continuations? &#8211; droidcon, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.droidcon.com\/2025\/01\/28\/are-kotlin-coroutines-just-data-structures-using-continuations\/\">https:\/\/www.droidcon.com\/2025\/01\/28\/are-kotlin-coroutines-just-data-structures-using-continuations\/<\/a><\/li>\n\n\n\n<li>kotlin\/compiler\/backend\/src\/org\/jetbrains\/kotlin\/codegen\/coroutines\/coroutines-codegen.md at master &#8211; GitHub, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/github.com\/JetBrains\/kotlin\/blob\/master\/compiler\/backend\/src\/org\/jetbrains\/kotlin\/codegen\/coroutines\/coroutines-codegen.md\">https:\/\/github.com\/JetBrains\/kotlin\/blob\/master\/compiler\/backend\/src\/org\/jetbrains\/kotlin\/codegen\/coroutines\/coroutines-codegen.md<\/a><\/li>\n\n\n\n<li>The Beginner&#8217;s Guide to Kotlin Coroutine Internals &#8211; DoorDash, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/careersatdoordash.com\/blog\/the-beginners-guide-to-kotlin-coroutine-internals\/\">https:\/\/careersatdoordash.com\/blog\/the-beginners-guide-to-kotlin-coroutine-internals\/<\/a><\/li>\n\n\n\n<li>What does the suspend function mean in a Kotlin Coroutine? &#8211; Stack Overflow, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/stackoverflow.com\/questions\/47871868\/what-does-the-suspend-function-mean-in-a-kotlin-coroutine\">https:\/\/stackoverflow.com\/questions\/47871868\/what-does-the-suspend-function-mean-in-a-kotlin-coroutine<\/a><\/li>\n\n\n\n<li>How a suspend function\/method works under the hood in kotlin &#8230;, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/stackoverflow.com\/questions\/76936370\/how-a-suspend-function-method-works-under-the-hood-in-kotlin\">https:\/\/stackoverflow.com\/questions\/76936370\/how-a-suspend-function-method-works-under-the-hood-in-kotlin<\/a><\/li>\n\n\n\n<li>How does a Coroutine Continuation internally work? &#8211; Stack Overflow, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/stackoverflow.com\/questions\/59005067\/how-does-a-coroutine-continuation-internally-work\">https:\/\/stackoverflow.com\/questions\/59005067\/how-does-a-coroutine-continuation-internally-work<\/a><\/li>\n\n\n\n<li>Using Threads and Coroutines : r\/androiddev &#8211; Reddit, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.reddit.com\/r\/androiddev\/comments\/169ndos\/using_threads_and_coroutines\/\">https:\/\/www.reddit.com\/r\/androiddev\/comments\/169ndos\/using_threads_and_coroutines\/<\/a><\/li>\n\n\n\n<li>How Suspend Functions Work in Kotlin: Under the hood &#8211; droidcon, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.droidcon.com\/2024\/04\/25\/how-suspend-functions-work-in-kotlin-under-the-hood\/\">https:\/\/www.droidcon.com\/2024\/04\/25\/how-suspend-functions-work-in-kotlin-under-the-hood\/<\/a><\/li>\n\n\n\n<li>Things every Kotlin Developer should know about Coroutines. Part 3: Structured Concurrency. &#8211; Art and science of writing good code, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/maxkim.dev\/things-every-kotlin-developer-should-know-about-coroutines-part-3-structured-concurrency\">https:\/\/maxkim.dev\/things-every-kotlin-developer-should-know-about-coroutines-part-3-structured-concurrency<\/a><\/li>\n\n\n\n<li>Structured concurrency &#8211; Wikipedia, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/en.wikipedia.org\/wiki\/Structured_concurrency\">https:\/\/en.wikipedia.org\/wiki\/Structured_concurrency<\/a><\/li>\n\n\n\n<li>Structured concurrency, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/2019.hydraconf.com\/2019\/talks\/68l5ztovlf0xm9aindouzr\/\">https:\/\/2019.hydraconf.com\/2019\/talks\/68l5ztovlf0xm9aindouzr\/<\/a><\/li>\n\n\n\n<li>Structured concurrency explained &#8211; Part 1: Introduction | The Dev &#8230;, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.thedevtavern.com\/blog\/posts\/structured-concurrency-explained\/\">https:\/\/www.thedevtavern.com\/blog\/posts\/structured-concurrency-explained\/<\/a><\/li>\n\n\n\n<li>Roman Elizarov \u2014 Structured concurrency &#8211; YouTube, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.youtube.com\/watch?v=Mj5P47F6nJg\">https:\/\/www.youtube.com\/watch?v=Mj5P47F6nJg<\/a><\/li>\n\n\n\n<li>CoroutineScope | kotlinx.coroutines \u2013 Kotlin Programming Language, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-coroutine-scope\/\">https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-coroutine-scope\/<\/a><\/li>\n\n\n\n<li>Use Kotlin coroutines with lifecycle-aware components | App architecture, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/developer.android.com\/topic\/libraries\/architecture\/coroutines\">https:\/\/developer.android.com\/topic\/libraries\/architecture\/coroutines<\/a><\/li>\n\n\n\n<li>Job | kotlinx.coroutines \u2013 Kotlin Programming Language, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-job\/\">https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-job\/<\/a><\/li>\n\n\n\n<li>coroutineScope | kotlinx.coroutines \u2013 Kotlin Programming Language, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/coroutine-scope.html\">https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/coroutine-scope.html<\/a><\/li>\n\n\n\n<li>KotlinConf 2018 &#8211; Kotlin Coroutines in Practice by Roman Elizarov &#8211; YouTube, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.youtube.com\/watch?v=a3agLJQ6vt8\">https:\/\/www.youtube.com\/watch?v=a3agLJQ6vt8<\/a><\/li>\n\n\n\n<li>Coroutine context and dispatchers | Kotlin Documentation, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/docs\/coroutine-context-and-dispatchers.html\">https:\/\/kotlinlang.org\/docs\/coroutine-context-and-dispatchers.html<\/a><\/li>\n\n\n\n<li>Exploring CoroutineContext &#8211; Tanya Tech Zone, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/tanyatechzone.com\/2024\/06\/26\/exploring-coroutinecontext\/\">https:\/\/tanyatechzone.com\/2024\/06\/26\/exploring-coroutinecontext\/<\/a><\/li>\n\n\n\n<li>kotlinlang.org, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/docs\/coroutine-context-and-dispatchers.html#:~:text=The%20coroutine%20context%20includes%20a,or%20let%20it%20run%20unconfined.\">https:\/\/kotlinlang.org\/docs\/coroutine-context-and-dispatchers.html#:~:text=The%20coroutine%20context%20includes%20a,or%20let%20it%20run%20unconfined.<\/a><\/li>\n\n\n\n<li>CoroutineDispatcher | kotlinx.coroutines \u2013 Kotlin Programming Language, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-coroutine-dispatcher\/\">https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-coroutine-dispatcher\/<\/a><\/li>\n\n\n\n<li>How do functions indicate their suspension points to coroutines? &#8211; Kotlin Discussions, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/discuss.kotlinlang.org\/t\/how-do-functions-indicate-their-suspension-points-to-coroutines\/30003\">https:\/\/discuss.kotlinlang.org\/t\/how-do-functions-indicate-their-suspension-points-to-coroutines\/30003<\/a><\/li>\n\n\n\n<li>Kotlin Coroutines and RxKotlin Comparison | Baeldung on Kotlin, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.baeldung.com\/kotlin\/coroutines-vs-rxkotlin\">https:\/\/www.baeldung.com\/kotlin\/coroutines-vs-rxkotlin<\/a><\/li>\n\n\n\n<li>Will Kotlin Coroutines Become Obsolete? &#8211; Java Code Geeks, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/www.javacodegeeks.com\/2025\/04\/will-kotlin-coroutines-become-obsolete.html\">https:\/\/www.javacodegeeks.com\/2025\/04\/will-kotlin-coroutines-become-obsolete.html<\/a><\/li>\n\n\n\n<li>Difference between CompletableFuture, Future and RxJava&#8217;s Observable &#8211; Stack Overflow, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/stackoverflow.com\/questions\/35329845\/difference-between-completablefuture-future-and-rxjavas-observable\">https:\/\/stackoverflow.com\/questions\/35329845\/difference-between-completablefuture-future-and-rxjavas-observable<\/a><\/li>\n\n\n\n<li>What is the principal difference between kotlin coroutines and Java 8 CompletableFuture?, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/discuss.kotlinlang.org\/t\/what-is-the-principal-difference-between-kotlin-coroutines-and-java-8-completablefuture\/3803\">https:\/\/discuss.kotlinlang.org\/t\/what-is-the-principal-difference-between-kotlin-coroutines-and-java-8-completablefuture\/3803<\/a><\/li>\n\n\n\n<li>Kotlin coroutines or jvm virtual threads? &#8211; Libraries, \u8bbf\u95ee\u65f6\u95f4\u4e3a \u516d\u6708 18, 2025\uff0c <a href=\"https:\/\/discuss.kotlinlang.org\/t\/kotlin-coroutines-or-jvm-virtual-threads\/24050\">https:\/\/discuss.kotlinlang.org\/t\/kotlin-coroutines-or-jvm-virtual-threads\/24050<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u5f02\u6b65\u7f16\u7a0b\u7684\u6301\u4e45\u6311\u6218 \u5728\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u65e0\u8bba\u662f\u6784\u5efa\u670d\u52a1\u5668\u7aef\u5e94\u7528\u3001\u684c\u9762\u8f6f\u4ef6\u8fd8\u662f\u79fb\u52a8\u5e94\u7528\uff0c\u5f02\u6b65\u975e\u963b\u585e\u7f16\u7a0b\u90fd\u5df2\u6210\u4e3a\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206 1\u3002\u5176\u6838\u5fc3\u76ee\u6807\u5728\u4e8e\u89e3\u51b3\u4e00\u4e2a\u6839\u672c\u6027\u7684\u77db\u76fe\uff1a\u5982\u4f55\u5728\u6267\u884c\u53ef\u80fd\u8017\u65f6\u7684\u64cd\u4f5c\uff08\u5982\u7f51\u7edc\u8bf7\u6c42\u3001\u6587\u4ef6 I\/O \u6216\u590d\u6742\u7684\u8ba1\u7b97\uff09\u65f6\uff0c\u65e2\u80fd\u4fdd\u8bc1\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u754c\u9762\u6d41\u7545\u54cd\u5e94\uff0c\u53c8\u80fd\u5b9e\u73b0\u7cfb\u7edf\u8d44\u6e90\u7684\u9ad8\u6548\u5229\u7528\u4e0e\u53ef\u4f38\u7f29\u6027 3\u3002\u5386\u53f2\u4e0a\uff0c\u5f00\u53d1\u8005\u4eec\u91c7\u7528\u4e86\u591a\u79cd\u6280\u672f\u6765\u5e94\u5bf9\u8fd9\u4e00\u6311\u6218\uff0c\u5305\u62ec\u76f4\u63a5\u4f7f\u7528\u7ebf\u7a0b\uff08Threading\uff09\u3001\u56de\u8c03\u51fd\u6570\uff08Callbacks\uff09\u4ee5\u53ca\u8bf8\u5982 Future \u6216 Promise \u4e4b\u7c7b\u7684\u62bd\u8c61\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u4f20\u7edf\u65b9\u6cd5\u65e0\u4e00\u4f8b\u5916\u5730\u5e26\u6765\u4e86\u81ea\u8eab\u7684\u590d\u6742\u6027\uff1a\u539f\u751f\u7ebf\u7a0b\u662f\u91cd\u91cf\u7ea7\u8d44\u6e90\uff0c\u5176\u521b\u5efa\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u6210\u672c\u9ad8\u6602\uff0c\u4e14\u96be\u4ee5\u7ba1\u7406 4\uff1b\u56de\u8c03\u51fd\u6570\u5219\u6781\u6613\u5bfc\u81f4\u201c\u56de\u8c03\u5730\u72f1\u201d\uff08Callback Hell\uff09\uff0c\u4f7f\u5f97\u4ee3\u7801\u903b\u8f91\u96be\u4ee5\u8ffd\u8e2a\u548c\u7ef4\u62a4 4\uff1b\u800c Future\/Promise \u6a21\u578b\u867d\u7136\u6709\u6240\u6539\u8fdb\uff0c\u4f46\u4ecd\u5c06\u5f00\u53d1\u8005\u5f15\u5165\u4e86\u4e00\u79cd\u94fe\u5f0f\u8c03\u7528\u7684\u7f16\u7a0b\u8303\u5f0f\uff0c\u4f7f\u5f97\u9519\u8bef\u5904\u7406\u548c\u63a7\u5236\u6d41\u53d8\u5f97\u590d\u6742 4\u3002 Kotlin \u534f\u7a0b\uff1a\u4e00\u6b21\u8303\u5f0f\u8f6c\u79fb \u5728\u6b64\u80cc\u666f\u4e0b\uff0cKotlin \u534f\u7a0b\u7684\u51fa\u73b0\u5e76\u975e\u4ec5\u4ec5\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u5e93\uff0c\u800c\u662f\u5e26\u6765\u4e86\u4e00\u573a\u6839\u672c\u6027\u7684\u8303\u5f0f\u8f6c\u79fb\u3002\u5b83\u662f\u4e00\u79cd\u5728\u8bed\u8a00\u5c42\u9762\u548c\u7f16\u8bd1\u5668\u5c42\u9762\u6df1\u5ea6\u652f\u6301\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u65e8\u5728\u4ece\u6839\u672c\u4e0a\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b 1\u3002\u534f\u7a0b\u7684\u6838\u5fc3\u627f\u8bfa\u662f\uff1a\u8ba9\u5f00\u53d1\u8005\u80fd\u591f\u4ee5\u7f16\u5199\u540c\u6b65\u3001\u987a\u5e8f\u4ee3\u7801\u7684\u76f4\u89c2\u65b9\u5f0f\uff0c\u6765\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u5f02\u6b65\u3001\u975e\u963b\u585e\u7a0b\u5e8f 4\u3002 \u672c\u62a5\u544a\u65e8\u5728\u5bf9 Kotlin \u534f\u7a0b\u8fdb\u884c\u4e00\u6b21\u5168\u9762\u800c\u6df1\u5165\u7684\u539f\u7406\u5256\u6790\uff0c\u63ed\u793a\u5176\u201c\u9b54\u6cd5\u201d\u80cc\u540e\u7684\u6280\u672f\u5b9e\u8d28\u3002\u6211\u4eec\u5c06\u63a2\u8ba8\u534f\u7a0b\u7684\u529b\u91cf\u6e90\u6cc9\u2014\u2014\u5b83\u5e76\u975e\u6765\u81ea\u5355\u4e00\u7684\u7279\u6027\uff0c\u800c\u662f\u4e24\u5927\u521b\u65b0\u7684\u534f\u540c\u4f5c\u7528\uff1a\u5176\u4e00\uff0c\u662f\u7531\u7f16\u8bd1\u5668\u9a71\u52a8\u7684\u6302\u8d77\uff08Suspension\uff09\u673a\u5236\uff0c\u5b83\u901a\u8fc7\u7cbe\u5de7\u7684\u4ee3\u7801\u8f6c\u6362\uff08Continuation-Passing Style\uff09\u5b9e\u73b0\u4e86\u975e\u963b\u585e\u7b49\u5f85\u7684\u5e95\u5c42\u6548\u7387\uff1b\u5176\u4e8c\uff0c\u662f\u7531\u5e93\u9a71\u52a8\u7684\u67b6\u6784\u6a21\u5f0f\uff0c\u5373\u7ed3\u6784\u5316\u5e76\u53d1\uff08Structured Concurrency\uff09\uff0c\u5b83\u4e3a\u5e76\u53d1\u4efb\u52a1\u63d0\u4f9b\u4e86\u5065\u58ee\u3001\u53ef\u9884\u6d4b\u7684\u751f\u547d\u5468\u671f\u7ba1\u7406\u548c\u9519\u8bef\u5904\u7406\u6846\u67b6\u3002\u901a\u8fc7\u672c\u6b21\u6df1\u5ea6\u89e3\u6790\uff0c\u6211\u4eec\u5c06\u7cfb\u7edf\u6027\u5730\u9610\u660e Kotlin \u534f\u7a0b\u662f\u5982\u4f55\u4ece\u6839\u672c\u4e0a\u6539\u53d8\u5f02\u6b65\u7f16\u7a0b\u7684\u6e38\u620f\u89c4\u5219\u7684\u3002 \u7b2c\u4e00\u8282\uff1a\u57fa\u672c\u62bd\u8c61\uff1a\u6302\u8d77\uff08Suspension\uff09 \u8981\u7406\u89e3\u534f\u7a0b\uff0c\u9996\u5148\u5fc5\u987b\u638c\u63e1\u5176\u6700\u6838\u5fc3\u3001\u6700\u6839\u672c\u7684\u62bd\u8c61\u6982\u5ff5\u2014\u2014\u6302\u8d77\uff08Suspension\uff09\u3002\u6b63\u662f\u8fd9\u4e00\u673a\u5236\u5c06\u534f\u7a0b\u4e0e\u4f20\u7edf\u7684\u5e76\u53d1\u6a21\u578b\uff08\u5982\u7ebf\u7a0b\uff09\u533a\u5206\u5f00\u6765\uff0c\u5e76\u7531\u6b64\u884d\u751f\u51fa\u534f\u7a0b\u7684\u6240\u6709\u5176\u4ed6\u4f18\u52bf\u3002\u672c\u8282\u5c06\u4ece\u534f\u7a0b\u7684\u201c\u8f7b\u91cf\u7ea7\u201d\u7279\u6027\u51fa\u53d1\uff0c\u6df1\u5165\u5256\u6790\u201c\u6302\u8d77\u201d\u4e0e\u201c\u963b\u585e\u201d\u7684\u672c\u8d28\u533a\u522b\uff0c\u5e76\u6700\u7ec8\u9610\u660e\u8fd9\u4e00\u673a\u5236\u5982\u4f55\u5e26\u6765\u4e86\u4ee3\u7801\u6e05\u6670\u5ea6\u7684\u9769\u547d\u6027\u63d0\u5347\u3002 1.1 \u8d85\u8d8a\u7ebf\u7a0b\uff1a\u534f\u7a0b\u7684\u8f7b\u91cf\u7ea7\u672c\u8d28 \u4f20\u7edf\u5e76\u53d1\u7f16\u7a0b\u7684\u57fa\u672c\u5355\u5143\u662f\u7ebf\u7a0b\u3002\u7136\u800c\uff0c\u7ebf\u7a0b\u662f\u4e00\u79cd\u91cd\u91cf\u7ea7\u7684\u3001\u7531\u64cd\u4f5c\u7cfb\u7edf\uff08OS\uff09\u7ba1\u7406\u7684\u8d44\u6e90\u3002\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u4e0d\u4ec5\u610f\u5473\u7740\u5185\u5b58\u7684\u6d88\u8017\uff08\u9700\u8981\u4e3a\u5176\u5206\u914d\u72ec\u7acb\u7684\u6808\u7a7a\u95f4\uff09\uff0c\u8fd8\u4f34\u968f\u7740\u6602\u8d35\u7684 CPU \u4e0a\u4e0b\u6587\u5207\u6362\u6210\u672c 9\u3002\u8fd9\u4f7f\u5f97\u5927\u89c4\u6a21\u5e76\u53d1\u6210\u4e3a\u4e00\u4e2a\u68d8\u624b\u7684\u95ee\u9898\uff1a\u5f53\u9700\u8981\u540c\u65f6\u5904\u7406\u6210\u5343\u4e0a\u4e07\u4e2a\u5e76\u53d1\u4efb\u52a1\u65f6\uff0c\u521b\u5efa\u540c\u7b49\u6570\u91cf\u7684\u7ebf\u7a0b\u51e0\u4e4e\u5fc5\u7136\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u8d44\u6e90\u8017\u5c3d\uff0c\u751a\u81f3\u5f15\u53d1 OutOfMemoryError 4\u3002 Kotlin \u534f\u7a0b\u4e3a\u6b64\u63d0\u4f9b\u4e86\u622a\u7136\u4e0d\u540c\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4e00\u4e2a\u534f\u7a0b\uff0c\u5176\u672c\u8d28\u662f\u4e00\u4e2a\u53ef\u6302\u8d77\u8ba1\u7b97\u7684\u5b9e\u4f8b\uff08an instance of a suspendable computation\uff09\uff0c\u800c\u975e\u4e00\u4e2a\u7ebf\u7a0b 11\u3002\u5b83\u5e38\u88ab\u6bd4\u4f5c\u201c\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\u201d\uff0c\u56e0\u4e3a\u534f\u7a0b\u5e76\u4e0d\u4e0e\u4efb\u4f55\u7279\u5b9a\u7684\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u4e00\u5bf9\u4e00\u7ed1\u5b9a 7\u3002\u534f\u7a0b\u662f\u5b58\u5728\u4e8e\u7528\u6237\u7a7a\u95f4\uff08user-level\uff09\u7684\u62bd\u8c61\uff0c\u5b83\u5728 JVM \u5806\u4e0a\u4ec5\u8868\u73b0\u4e3a\u4e00\u4e2a\u5c0f\u5bf9\u8c61\uff08\u5373\u7a0d\u540e\u5c06\u8be6\u8ff0\u7684\u201cContinuation\u201d\u5bf9\u8c61\uff09\uff0c\u800c\u975e\u4e00\u4e2a\u62e5\u6709\u5e9e\u5927\u539f\u751f\u6808\u7684\u7ebf\u7a0b 3\u3002\u8fd9\u79cd\u8bbe\u8ba1\u5e26\u6765\u4e86\u60ca\u4eba\u7684\u8d44\u6e90\u6548\u7387\uff0c\u4f7f\u5f97\u5728\u5355\u4e2a JVM \u5b9e\u4f8b\u4e2d\u521b\u5efa\u6570\u5341\u4e07\u751a\u81f3\u6570\u767e\u4e07\u4e2a\u534f\u7a0b\u6210\u4e3a\u53ef\u80fd\uff0c\u800c\u4e0d\u4f1a\u89e6\u53ca\u8d44\u6e90\u74f6\u9888 9\u3002 \u7136\u800c\uff0c\u5bf9\u201c\u534f\u7a0b\u6bd4\u7ebf\u7a0b\u5feb\u201d\u8fd9\u4e00\u6d41\u884c\u8bf4\u6cd5\u9700\u8981\u6709\u66f4\u7cbe\u786e\u7684\u7406\u89e3\u3002\u534f\u7a0b\u7684\u6027\u80fd\u4f18\u52bf\u4e3b\u8981\u4f53\u73b0\u5728\u8d44\u6e90\u5229\u7528\u7387\u548c\u9ad8\u5e76\u53d1\u4e0b\u7684\u541e\u5410\u91cf\uff0c\u5c24\u5176\u662f\u5728 I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u3002\u5728\u5355\u4e2a\u4efb\u52a1\u7684**\u542f\u52a8\u5ef6\u8fdf\uff08startup latency\uff09**\u65b9\u9762\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5df2\u7ecf\u9884\u70ed\u7684\u7ebf\u7a0b\u6c60\uff0c\u5176\u542f\u52a8\u4efb\u52a1\u7684\u901f\u5ea6\u53ef\u80fd\u5fae\u5feb\u4e8e\u542f\u52a8\u4e00\u4e2a\u65b0\u534f\u7a0b 14\u3002\u56e0\u6b64\uff0c\u534f\u7a0b\u771f\u6b63\u7684\u6027\u80fd\u589e\u76ca\u6765\u6e90\u4e8e\u5176\u5728\u89c4\u6a21\u5316\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u9ad8\u6548\u7387\uff0c\u800c\u975e\u5728\u6240\u6709\u60c5\u51b5\u4e0b\u7684\u539f\u59cb\u542f\u52a8\u901f\u5ea6 14\u3002 1.2 \u6302\u8d77\u673a\u5236 vs. \u963b\u585e\u7684\u4f4e\u6548 \u4e3a\u4e86\u8fdb\u4e00\u6b65\u7406\u89e3\u534f\u7a0b\u7684\u8f7b\u91cf\u7ea7\u672c\u8d28\uff0c\u5fc5\u987b\u8fa8\u6790\u201c\u6302\u8d77\u201d\u4e0e\u201c\u963b\u585e\u201d\u8fd9\u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\u3002 \u8fd9\u79cd\u6267\u884c\u6a21\u5f0f\u88ab\u79f0\u4e3a\u534f\u4f5c\u5f0f\u591a\u4efb\u52a1\uff08Cooperative Multitasking\uff09\u3002\u4e0e\u7ebf\u7a0b\u7531\u64cd\u4f5c\u7cfb\u7edf\u5728\u4efb\u610f\u65f6\u523b\u5f3a\u5236\u5207\u6362\u7684\u62a2\u5360\u5f0f\u591a\u4efb\u52a1\uff08Preemptive Multitasking\uff09\u4e0d\u540c\uff0c\u534f\u7a0b\u53ea\u5728\u4ee3\u7801\u4e2d\u660e\u786e\u6807\u8bb0\u7684\u6302\u8d77\u70b9\u201c\u81ea\u613f\u5730\u201d\u4ea4\u51fa\u63a7\u5236\u6743 9\u3002\u8fd9\u79cd\u534f\u4f5c\u6027\u8d28\u4f7f\u5f97\u534f\u7a0b\u7684\u884c\u4e3a\u66f4\u52a0\u786e\u5b9a\uff0c\u56e0\u4e3a\u72b6\u6001\u7684\u53d8\u66f4\u53ea\u53ef\u80fd\u53d1\u751f\u5728\u8fd9\u4e9b\u5df2\u77e5\u7684\u6302\u8d77\u70b9\uff0c\u4ece\u800c\u7b80\u5316\u4e86\u5e76\u53d1\u4ee3\u7801\u7684\u8c03\u8bd5\u548c\u63a8\u7406 19\u3002 1.3 \u4ece\u56de\u8c03\u5730\u72f1\u5230\u987a\u5e8f\u6e05\u6670\uff1a\u4ee3\u7801\u53ef\u8bfb\u6027\u7684\u8303\u5f0f\u8f6c\u79fb \u5f02\u6b65\u7f16\u7a0b\u7684\u957f\u671f\u75db\u70b9\u5728\u4e8e\u5176\u5bf9\u4ee3\u7801\u7ed3\u6784\u548c\u53ef\u8bfb\u6027\u7684\u7834\u574f\u3002\u4f20\u7edf\u4e0a\uff0c\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u6700&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[61,63],"class_list":["post-517","post","type-post","status-publish","format-standard","hentry","category-dev","tag-kotlin","tag-63"],"_links":{"self":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=517"}],"version-history":[{"count":2,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/517\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/trantor.ink\/index.php?rest_route=\/wp\/v2\/posts\/517\/revisions\/519"}],"wp:attachment":[{"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trantor.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}