{"id":16213,"date":"2026-03-04T10:49:27","date_gmt":"2026-03-04T02:49:27","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213"},"modified":"2026-03-05T15:31:11","modified_gmt":"2026-03-05T07:31:11","slug":"%e4%ba%8c%e3%80%81%e5%8d%8f%e7%a8%8b%e6%9e%84%e5%bb%ba%e5%99%a8-coroutine-builders","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213","title":{"rendered":"\u4e8c\u3001\u534f\u7a0b\u521b\u5efa"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/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-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#%E4%B8%80%E3%80%81%E5%8D%8F%E7%A8%8B%E6%9E%84%E5%BB%BA%E5%99%A8_Coroutine_Builders\" title=\"\u4e00\u3001\u534f\u7a0b\u6784\u5efa\u5668 (Coroutine Builders)\">\u4e00\u3001\u534f\u7a0b\u6784\u5efa\u5668 (Coroutine Builders)<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#%E4%BA%8C%E3%80%81%E6%9E%84%E9%80%A0%E5%8F%82%E6%95%B0%E5%8D%8F%E7%A8%8B%E4%B8%8A%E4%B8%8B%E6%96%87%EF%BC%88CoroutineContext%EF%BC%89\" title=\"\u4e8c\u3001\u6784\u9020\u53c2\u6570\/\u534f\u7a0b\u4e0a\u4e0b\u6587\uff08CoroutineContext\uff09\">\u4e8c\u3001\u6784\u9020\u53c2\u6570\/\u534f\u7a0b\u4e0a\u4e0b\u6587\uff08CoroutineContext\uff09<\/a><ul class=\"ez-toc-list-level-2\"><li class=\"ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#%3E%3E%3E%E4%B8%8A%E4%B8%8B%E6%96%87%E5%85%83%E7%B4%A0%E7%9A%84%E5%88%86%E7%B1%BB\" title=\"&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u5206\u7c7b\">&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u5206\u7c7b<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#1%E8%B0%83%E5%BA%A6%E5%99%A8%EF%BC%88CoroutineDispatcher%EF%BC%89\" title=\"1.\u8c03\u5ea6\u5668\uff08CoroutineDispatcher\uff09\">1.\u8c03\u5ea6\u5668\uff08CoroutineDispatcher\uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#2%E5%90%8D%E7%A7%B0%EF%BC%88CoroutineName%EF%BC%89\" title=\"2.\u540d\u79f0\uff08CoroutineName\uff09\">2.\u540d\u79f0\uff08CoroutineName\uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#3%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%E5%99%A8%EF%BC%88CoroutineExceptionHandler_%EF%BC%89\" title=\"3.\u5f02\u5e38\u5904\u7406\u5668\uff08CoroutineExceptionHandler \uff09\">3.\u5f02\u5e38\u5904\u7406\u5668\uff08CoroutineExceptionHandler \uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#4%E2%80%A6%E5%85%B6%E5%AE%83%E4%BB%BB%E4%BD%95CoroutineContext%E7%9A%84%E5%AD%90%E7%B1%BB\" title=\"4&#8230;.\u5176\u5b83\u4efb\u4f55CoroutineContext\u7684\u5b50\u7c7b\">4&#8230;.\u5176\u5b83\u4efb\u4f55CoroutineContext\u7684\u5b50\u7c7b<\/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=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#%3E%3E%3E%E4%B8%8A%E4%B8%8B%E6%96%87%E5%85%83%E7%B4%A0%E7%9A%84%E4%BC%A0%E5%85%A5%E6%96%B9%E5%BC%8F\" title=\"&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u4f20\u5165\u65b9\u5f0f\">&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u4f20\u5165\u65b9\u5f0f<\/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=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#1%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E7%9B%B4%E6%8E%A5%E4%BC%A0%E5%85%A5%E5%8D%95%E4%B8%AA%E5%85%83%E7%B4%A0\" title=\"1.\u65b9\u5f0f\u4e00\uff1a\u76f4\u63a5\u4f20\u5165\u5355\u4e2a\u5143\u7d20\">1.\u65b9\u5f0f\u4e00\uff1a\u76f4\u63a5\u4f20\u5165\u5355\u4e2a\u5143\u7d20<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#2%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E4%BD%BF%E7%94%A8_%E7%BB%84%E5%90%88%E5%A4%9A%E4%B8%AA%E5%85%83%E7%B4%A0%EF%BC%88%E6%9C%80%E5%B8%B8%E7%94%A8%EF%BC%89\" title=\"2.\u65b9\u5f0f\u4e8c\uff1a\u4f7f\u7528 + \u7ec4\u5408\u591a\u4e2a\u5143\u7d20\uff08\u6700\u5e38\u7528\uff09\">2.\u65b9\u5f0f\u4e8c\uff1a\u4f7f\u7528 + \u7ec4\u5408\u591a\u4e2a\u5143\u7d20\uff08\u6700\u5e38\u7528\uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-11\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#3%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%9A%E8%87%AA%E5%AE%9A%E4%B9%89CoroutineScope\" title=\"3.\u65b9\u5f0f\u4e09\uff1a\u81ea\u5b9a\u4e49CoroutineScope\">3.\u65b9\u5f0f\u4e09\uff1a\u81ea\u5b9a\u4e49CoroutineScope<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-12\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#%3E%3E%3E%E4%B8%8A%E4%B8%8B%E6%96%87%E5%85%83%E7%B4%A0%E7%9A%84%E8%8E%B7%E5%8F%96\" title=\"&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u83b7\u53d6\">&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u83b7\u53d6<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-13\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#%3E%3E%3E%E4%B8%8A%E4%B8%8B%E6%96%87%E5%88%87%E6%8D%A2%EF%BC%88%E9%87%8D%E7%82%B9%EF%BC%81%EF%BC%81%EF%BC%81%EF%BC%89\" title=\"&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5207\u6362\uff08\u91cd\u70b9\uff01\uff01\uff01\uff09\">&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5207\u6362\uff08\u91cd\u70b9\uff01\uff01\uff01\uff09<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-14\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=16213\/#%E4%B8%89%E3%80%81%E5%90%AF%E5%8A%A8%E6%A8%A1%E5%BC%8F_CoroutineStart\" title=\"\u4e09\u3001\u542f\u52a8\u6a21\u5f0f (CoroutineStart)\">\u4e09\u3001\u542f\u52a8\u6a21\u5f0f (CoroutineStart)<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E4%B8%80%E3%80%81%E5%8D%8F%E7%A8%8B%E6%9E%84%E5%BB%BA%E5%99%A8_Coroutine_Builders\"><\/span>\u4e00\u3001\u534f\u7a0b\u6784\u5efa\u5668 (Coroutine Builders)<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li><strong><code>launch<\/code><\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u534f\u7a0b\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<strong><code>Job<\/code><\/strong>\uff0c\u4ee3\u8868\u534f\u7a0b\u672c\u8eab\u3002\u5b83<strong>\u4e0d\u8fd4\u56de\u4efb\u4f55\u7ed3\u679c<\/strong>\uff0c\u9002\u7528\u4e8e\u201c\u53d1\u540e\u5373\u5fd8\u201d\u7684\u573a\u666f\u3002<\/li>\n<li><strong><code>async<\/code><\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u534f\u7a0b\uff0c\u8fd4\u56de\u4e00\u4e2a<strong><code>Deferred&lt;T&gt;<\/code><\/strong>\u5bf9\u8c61\uff08<code>Job<\/code>\u7684\u5b50\u63a5\u53e3\uff09\u3002\u901a\u8fc7\u5728\u5176\u4e0a\u8c03\u7528<code>.await()<\/code>\uff0c\u53ef\u4ee5<strong>\u6302\u8d77\u5e76\u83b7\u53d6\u6700\u7ec8\u8fd4\u56de\u7684\u7ed3\u679c<\/strong>\u3002\u5e38\u7528\u4e8e\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u3002<\/li>\n<li><strong><code>runBlocking<\/code><\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u534f\u7a0b\u5e76<strong>\u963b\u585e\u5f53\u524d\u7ebf\u7a0b<\/strong>\uff0c\u76f4\u5230\u5176\u5185\u90e8\u6240\u6709\u903b\u8f91\u6267\u884c\u5b8c\u6bd5\u3002\u5b83\u4e3b\u8981\u7528\u4e8e<strong>\u6865\u63a5\u666e\u901a\u963b\u585e\u4ee3\u7801\u548c\u6302\u8d77\u98ce\u683c\u4ee3\u7801<\/strong>\uff08\u5982\u5728<code>main<\/code>\u51fd\u6570\u6216\u6d4b\u8bd5\u4e2d\uff09\uff0c\u65e5\u5e38Android\u5f00\u53d1\u4e2d\u5e94\u907f\u514d\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><strong><code>launch<\/code> \u548c <code>async<\/code> \u5fc5\u987b\u901a\u8fc7 <code>CoroutineScope<\/code> \u624d\u80fd\u8c03\u7528\uff0c\u662f\u5176\u6269\u5c55\u51fd\u6570\u3002<\/strong><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E3%80%81%E6%9E%84%E9%80%A0%E5%8F%82%E6%95%B0%E5%8D%8F%E7%A8%8B%E4%B8%8A%E4%B8%8B%E6%96%87%EF%BC%88CoroutineContext%EF%BC%89\"><\/span>\u4e8c\u3001\u6784\u9020\u53c2\u6570\/\u534f\u7a0b\u4e0a\u4e0b\u6587\uff08CoroutineContext\uff09<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u4e00\u4e2a\u534f\u7a0b\u5bf9\u5e94\u4e00\u4e2aCoroutineContext<\/p>\n<p>\u5b98\u65b9\u51fa\u5904\uff1a<a href=\"https:\/\/developer.android.google.cn\/kotlin\/coroutines\/coroutines-adv?hl=zh-cn\">https:\/\/developer.android.google.cn\/kotlin\/coroutines\/coroutines-adv?hl=zh-cn<\/a><\/p>\n<p><a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.coroutines\/-coroutine-context\/index.html\"><code>CoroutineContext<\/code><\/a> \u4f7f\u7528\u4ee5\u4e0b\u5143\u7d20\u96c6\u5b9a\u4e49\u534f\u7a0b\u7684\u884c\u4e3a\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/kotlin.github.io\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-job\/index.html\"><code>Job<\/code><\/a>\uff1a\u63a7\u5236\u534f\u7a0b\u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<li><a href=\"https:\/\/kotlin.github.io\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-coroutine-dispatcher\/index.html\"><code>CoroutineDispatcher<\/code><\/a>\uff1a\u5c06\u5de5\u4f5c\u5206\u6d3e\u5230\u9002\u5f53\u7684\u7ebf\u7a0b\u3002<\/li>\n<li><a href=\"https:\/\/kotlin.github.io\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-coroutine-name\/index.html\"><code>CoroutineName<\/code><\/a>\uff1a\u534f\u7a0b\u7684\u540d\u79f0\uff0c\u53ef\u7528\u4e8e\u8c03\u8bd5\u3002<\/li>\n<li><a href=\"https:\/\/kotlin.github.io\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-coroutine-exception-handler\/index.html\"><code>CoroutineExceptionHandler<\/code><\/a>\uff1a\u5904\u7406\u672a\u6355\u83b7\u7684\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"%3E%3E%3E%E4%B8%8A%E4%B8%8B%E6%96%87%E5%85%83%E7%B4%A0%E7%9A%84%E5%88%86%E7%B1%BB\"><\/span>&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u5206\u7c7b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"1%E8%B0%83%E5%BA%A6%E5%99%A8%EF%BC%88CoroutineDispatcher%EF%BC%89\"><\/span>1.\u8c03\u5ea6\u5668\uff08CoroutineDispatcher\uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><strong>\u51b3\u5b9a\u534f\u7a0b\u5728\u54ea\u4e2a\u7ebf\u7a0b\u6216\u7ebf\u7a0b\u6c60\u4e0a\u6267\u884c<\/strong>\u3002\u4e3b\u8981\u7c7b\u578b\u6709\uff1a<\/p>\n<ul>\n<li><code>Dispatchers.Main<\/code>\uff1a\u4e3b\u7ebf\u7a0b\uff0c\u7528\u4e8eUI\u64cd\u4f5c\u3002<\/li>\n<li><code>Dispatchers.IO<\/code>\uff1a\u9002\u5408\u6267\u884c\u78c1\u76d8\u6216\u7f51\u7edcI\/O\u64cd\u4f5c\uff08\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\uff09\u3002<\/li>\n<li><code>Dispatchers.Default<\/code>\uff1a\u9002\u5408\u6267\u884cCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff08\u5982\u5217\u8868\u6392\u5e8f\u3001JSON\u89e3\u6790\uff09\u3002<\/li>\n<li><code>Dispatchers.Unconfined<\/code>\uff1a\u4e0d\u9650\u5b9a\u7ebf\u7a0b\uff0c\u5728\u7b2c\u4e00\u4e2a\u6302\u8d77\u70b9\u4e4b\u524d\u8fd0\u884c\u5728\u5f53\u524d\u7ebf\u7a0b\uff0c\u4e4b\u540e\u7531\u8c03\u7528<code>resume<\/code>\u7684\u7ebf\u7a0b\u51b3\u5b9a\uff08\u6781\u5c11\u4f7f\u7528\uff09\u3002<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"2%E5%90%8D%E7%A7%B0%EF%BC%88CoroutineName%EF%BC%89\"><\/span>2.\u540d\u79f0\uff08CoroutineName\uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"3%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%E5%99%A8%EF%BC%88CoroutineExceptionHandler_%EF%BC%89\"><\/span>3.\u5f02\u5e38\u5904\u7406\u5668\uff08CoroutineExceptionHandler \uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3>4....\u5176\u5b83\u4efb\u4f55CoroutineContext\u7684\u5b50\u7c7b<\/h3>\n<h2><span class=\"ez-toc-section\" id=\"%3E%3E%3E%E4%B8%8A%E4%B8%8B%E6%96%87%E5%85%83%E7%B4%A0%E7%9A%84%E4%BC%A0%E5%85%A5%E6%96%B9%E5%BC%8F\"><\/span>&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u4f20\u5165\u65b9\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"1%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E7%9B%B4%E6%8E%A5%E4%BC%A0%E5%85%A5%E5%8D%95%E4%B8%AA%E5%85%83%E7%B4%A0\"><\/span>1.\u65b9\u5f0f\u4e00\uff1a\u76f4\u63a5\u4f20\u5165\u5355\u4e2a\u5143\u7d20<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code>fun main() = runBlocking&lt;Unit&gt; {\n    \/\/ 1. \u4f20\u5165\u8c03\u5ea6\u5668\n    launch(Dispatchers.IO) {\n        println(&quot;IO\u8c03\u5ea6\u5668: ${Thread.currentThread().name}&quot;)\n    }\n\n    \/\/ 2. \u4f20\u5165\u534f\u7a0b\u540d\u79f0\n    launch(CoroutineName(&quot;MyCoroutine&quot;)) {\n        println(&quot;\u534f\u7a0b\u540d\u79f0: ${coroutineContext[CoroutineName]?.name}&quot;)\n    }\n\n    \/\/ 3. \u4f20\u5165 Job\n    val parentJob = Job()\n    launch(parentJob) {\n        println(&quot;\u4f7f\u7528\u81ea\u5b9a\u4e49 Job&quot;)\n    }\n\n    \/\/ 4. \u4f20\u5165\u5f02\u5e38\u5904\u7406\u5668\n    val handler = CoroutineExceptionHandler { _, throwable -&gt;\n        println(&quot;\u5f02\u5e38: ${throwable.message}&quot;)\n    }\n    launch(handler) {\n        throw RuntimeException(&quot;\u51fa\u9519\u4e86&quot;)\n    }\n\n    delay(100)\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"2%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E4%BD%BF%E7%94%A8_%E7%BB%84%E5%90%88%E5%A4%9A%E4%B8%AA%E5%85%83%E7%B4%A0%EF%BC%88%E6%9C%80%E5%B8%B8%E7%94%A8%EF%BC%89\"><\/span>2.\u65b9\u5f0f\u4e8c\uff1a\u4f7f\u7528 + \u7ec4\u5408\u591a\u4e2a\u5143\u7d20\uff08\u6700\u5e38\u7528\uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code>fun main() = runBlocking&lt;Unit&gt; {\n    \/\/ \u7ec4\u5408\u591a\u4e2a\u5143\u7d20\n    launch(Dispatchers.IO + CoroutineName(&quot;\u7ec4\u5408\u534f\u7a0b&quot;)) {\n        println(&quot;\u8c03\u5ea6\u5668: ${coroutineContext[CoroutineDispatcher]}&quot;)\n        println(&quot;\u540d\u79f0: ${coroutineContext[CoroutineName]?.name}&quot;)\n    }\n\n    \/\/ \u7ec4\u5408\u66f4\u591a\u5143\u7d20\n    val handler = CoroutineExceptionHandler { _, _ -&gt; }\n    launch(Dispatchers.Default + CoroutineName(&quot;\u5b8c\u6574&quot;) + handler + Job()) {\n        println(&quot;\u6240\u6709\u5143\u7d20\u90fd\u5b58\u5728&quot;)\n    }\n}<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"3%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%9A%E8%87%AA%E5%AE%9A%E4%B9%89CoroutineScope\"><\/span>3.\u65b9\u5f0f\u4e09\uff1a\u81ea\u5b9a\u4e49CoroutineScope<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u81ea\u5b9a\u4e49CoroutineScope\u53ef\u4ee5\u4f20\u5165\u4e0a\u4e0b\u6587\u53c2\u6570\uff0claunch\u5982\u679c\u4e0d\u4f20\u5165\u4e0a\u4e0b\u6587\u53c2\u6570\u5219\u4f1a\u7ee7\u627fCoroutineScope\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u4e0elaunch\u4e0a\u4e0b\u6587\u7684\u5173\u7cfb<br \/>\n<strong><code>launch<\/code> \u5c31\u50cf\u662f\u4e00\u4e2a&quot;\u4e0a\u4e0b\u6587\u7ee7\u627f\u5668&quot;\uff0c\u5b83\u628a Scope \u7684\u4e0a\u4e0b\u6587\u4f5c\u4e3a&quot;\u9ed8\u8ba4\u503c&quot;\uff0c\u628a\u4f20\u5165\u7684\u4e0a\u4e0b\u6587\u4f5c\u4e3a&quot;\u8986\u76d6\u503c&quot;\uff0c\u5408\u5e76\u540e\u4f5c\u4e3a\u65b0\u534f\u7a0b\u7684\u8fd0\u884c\u73af\u5883\u3002<\/strong><\/p>\n<p>... (\u8fd8\u6709\u5f88\u591a\u5176\u5b83\u4f20\u5165\u65b9\u5f0f)<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%3E%3E%3E%E4%B8%8A%E4%B8%8B%E6%96%87%E5%85%83%E7%B4%A0%E7%9A%84%E8%8E%B7%E5%8F%96\"><\/span>&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5143\u7d20\u7684\u83b7\u53d6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code>\/\/ \u7edf\u4e00\u7684\u83b7\u53d6\u8bed\u6cd5\ncoroutineContext[\u5143\u7d20\u7c7b\u578b]      \/\/ \u63a8\u8350\ncoroutineContext[\u5143\u7d20\u7c7b\u578b.Key]  \/\/ \u663e\u5f0f\u4f7f\u7528 Key<\/code><\/pre>\n<p>\u542f\u52a8\u6a21\u5f0f (CoroutineStart)<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%3E%3E%3E%E4%B8%8A%E4%B8%8B%E6%96%87%E5%88%87%E6%8D%A2%EF%BC%88%E9%87%8D%E7%82%B9%EF%BC%81%EF%BC%81%EF%BC%81%EF%BC%89\"><\/span>&gt;&gt;&gt;\u4e0a\u4e0b\u6587\u5207\u6362\uff08\u91cd\u70b9\uff01\uff01\uff01\uff09<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>withContext<\/code> \u7684\u6838\u5fc3\u4f5c\u7528\u5c31\u662f<strong>\u5207\u6362\u534f\u7a0b\u7684\u4e0a\u4e0b\u6587<\/strong>\uff0c\u7279\u522b\u662f<strong>\u5207\u6362\u7ebf\u7a0b<\/strong>\u3002<\/p>\n<p>\u91cd\u70b9\uff1a<strong>withContext\u4e0d\u4f1a\u521b\u5efa\u65b0\u534f\u7a0b\uff0c\u4f1a\u201c\u963b\u585e\u201d\u5f53\u524d\u534f\u7a0b\u3002<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u7279\u6027<\/th>\n<th style=\"text-align: left;\"><code>withContext<\/code><\/th>\n<th style=\"text-align: left;\"><code>launch<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">\u662f\u5426\u521b\u5efa\u65b0\u534f\u7a0b<\/td>\n<td style=\"text-align: left;\">\u274c \u5426<\/td>\n<td style=\"text-align: left;\">\u2705 \u662f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u662f\u5426\u6302\u8d77<\/td>\n<td style=\"text-align: left;\">\u2705 \u662f<\/td>\n<td style=\"text-align: left;\">\u2705 \u662f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u8fd4\u56de\u7ed3\u679c<\/td>\n<td style=\"text-align: left;\">\u2705 \u8fd4\u56de T<\/td>\n<td style=\"text-align: left;\">\u274c \u8fd4\u56de Job<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u5f02\u5e38\u4f20\u64ad<\/td>\n<td style=\"text-align: left;\">\u76f4\u63a5\u629b\u51fa<\/td>\n<td style=\"text-align: left;\">\u4f20\u64ad\u7ed9\u7236\u534f\u7a0b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u7236\u5b50\u5173\u7cfb<\/td>\n<td style=\"text-align: left;\">\u5f53\u524d\u534f\u7a0b\u7684\u5b50\u4efb\u52a1<\/td>\n<td style=\"text-align: left;\">\u65b0\u534f\u7a0b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\n<p>\u5207\u6362\u7ebf\u7a0b<\/p>\n<pre><code>fun main() = runBlocking {\nprintln(&quot;\u2460 \u4e3b\u7ebf\u7a0b: ${Thread.currentThread().name}&quot;)\n\nwithContext(Dispatchers.IO) {\n    println(&quot;\u2461 \u5207\u6362\u5230IO\u7ebf\u7a0b: ${Thread.currentThread().name}&quot;)\n    delay(100)\n}\n\nprintln(&quot;\u2462 \u81ea\u52a8\u5207\u56de\u539f\u7ebf\u7a0b: ${Thread.currentThread().name}&quot;)\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c<\/p>\n<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u7279\u6027<\/th>\n<th style=\"text-align: left;\"><code>withContext<\/code><\/th>\n<th style=\"text-align: left;\"><code>async<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><strong>\u8fd4\u56de\u503c\u7c7b\u578b<\/strong><\/td>\n<td style=\"text-align: left;\">\u76f4\u63a5\u8fd4\u56de T<\/td>\n<td style=\"text-align: left;\">\u8fd4\u56de Deferred<T><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u662f\u5426\u9700\u8981 await<\/strong><\/td>\n<td style=\"text-align: left;\">\u4e0d\u9700\u8981<\/td>\n<td style=\"text-align: left;\">\u9700\u8981 <code>await()<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u6267\u884c\u65f6\u673a<\/strong><\/td>\n<td style=\"text-align: left;\">\u7acb\u5373\u6267\u884c\uff0c\u6302\u8d77\u7b49\u5f85<\/td>\n<td style=\"text-align: left;\">\u7acb\u5373\u6267\u884c\uff0c\u53ef\u7a0d\u540e await<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u9002\u7528\u573a\u666f<\/strong><\/td>\n<td style=\"text-align: left;\">\u9700\u8981\u7ed3\u679c\u5e76\u7b49\u5f85<\/td>\n<td style=\"text-align: left;\">\u53ef\u4ee5\u7a0d\u540e\u83b7\u53d6\u7ed3\u679c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1><span class=\"ez-toc-section\" id=\"%E4%B8%89%E3%80%81%E5%90%AF%E5%8A%A8%E6%A8%A1%E5%BC%8F_CoroutineStart\"><\/span>\u4e09\u3001\u542f\u52a8\u6a21\u5f0f (CoroutineStart)<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li><strong><code>DEFAULT<\/code><\/strong>\uff1a<strong>\u7acb\u5373<\/strong>\u8c03\u5ea6\u6267\u884c\u534f\u7a0b\uff0c\u968f\u65f6\u53ef\u4ee5\u53d6\u6d88\u3002<\/li>\n<li><strong><code>LAZY<\/code><\/strong>\uff1a<strong>\u61d2\u52a0\u8f7d\u6a21\u5f0f<\/strong>\uff0c\u53ea\u6709\u5728\u9700\u8981\u65f6\uff08\u5982\u8c03\u7528<code>Job.start()<\/code>\u3001<code>Job.join()<\/code>\u6216<code>Deferred.await()<\/code>\uff09\u624d\u4f1a\u5f00\u59cb\u6267\u884c\u3002<\/li>\n<li><code>ATOMIC<\/code>\uff1a\u7c7b\u4f3c\u4e8e<code>DEFAULT<\/code>\uff0c\u4f46\u5728\u5f00\u59cb\u8fd0\u884c\u524d<strong>\u4e0d\u80fd\u88ab\u53d6\u6d88<\/strong>\u3002<\/li>\n<li><code>UNDISPATCHED<\/code>\uff1a\u534f\u7a0b\u521b\u5efa\u540e\uff0c\u7acb\u5373\u5728\u5f53\u524d\u7ebf\u7a0b\u6267\u884c\uff0c\u76f4\u5230\u9047\u5230\u7b2c\u4e00\u4e2a\u6302\u8d77\u70b9\u3002<\/li>\n<\/ul>\n<button class=\"simplefavorite-button\" data-postid=\"16213\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 \u4e00\u3001\u534f\u7a0b\u6784\u5efa\u5668 (Coroutine Builders)\u4e8c\u3001\u6784\u9020\u53c2\u6570\/\u534f\u7a0b\u4e0a\u4e0b\u6587\uff08Corout [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1254],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/16213"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16213"}],"version-history":[{"count":30,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/16213\/revisions"}],"predecessor-version":[{"id":16269,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/16213\/revisions\/16269"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16213"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}