{"id":149198,"date":"2021-05-18T15:38:22","date_gmt":"2021-05-18T14:38:22","guid":{"rendered":"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/"},"modified":"2021-06-03T13:05:11","modified_gmt":"2021-06-03T12:05:11","slug":"kotlin-coroutines-1-5-0-released","status":"publish","type":"kotlin","link":"https:\/\/blog.jetbrains.com\/zh-hans\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/","title":{"rendered":"Kotlin Coroutines 1.5: GlobalScope \u6807\u8bb0\u4e3a delicate\uff0c\u5b8c\u5584\u7684 Channels API \u7b49\u7b49"},"content":{"rendered":"\n<p><em>\u5408\u8457\u8005: <a href=\"https:\/\/blog.jetbrains.com\/author\/svetlana-isakova\/\" class=\"ek-link\">Svetlana Isakova<\/a><\/em><\/p>\n\n\n\n<p>Kotlin Coroutines 1.5.0 \u5df2\u53d1\u5e03!&nbsp;\u4ee5\u4e0b\u4e3a\u65b0\u7248\u672c\u5e26\u6765\u7684\u65b0\u7279\u6027\uff1a<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#globalscope\">GlobalScope API\u5df2\u88ab\u6807\u8bb0\u4e3adelicate<\/a>\u3002GlobalScope\u4f5c\u4e3a\u9ad8\u7ea7\u7684API\u5f88\u5bb9\u6613\u88ab\u6ee5\u7528\u3002&nbsp;\u5728\u53ef\u80fd\u4f1a\u88ab\u6ee5\u7528\u7684\u5730\u65b9\uff0c\u73b0\u5728\u7f16\u8bd1\u5668\u5c06\u53d1\u51fa\u8b66\u544a\uff0c\u5e76\u8981\u6c42\u60a8\u5728\u7a0b\u5e8f\u4e2d\u9009\u62e9\u6027\u5f15\u5165\u8be5\u7c7b\u3002<\/li><li><\/li><li><a href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#junit\">JUnit\u6269\u5c55\u3002<\/a>CoroutinesTimeout\u5df2\u53ef\u5728JUnit5\u4e2d\u4f7f\u7528\u3002<\/li><li><\/li><li><a href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#channels\">\u5b8c\u5584\u7684Channel API\u3002<\/a>\u4ee5\u53ca\u9488\u5bf9\u5e93\u51fd\u6570\u65b0\u7684\u547d\u540d\u65b9\u6848\uff0c\u5f15\u5165\u4e86\u975e\u6302\u8d77\u51fd\u6570<code>trySend<\/code>\u548c<code>tryReceive<\/code>\u4ee5\u4f5c\u4e3a<code>offer<\/code>\u548c<code>poll<\/code>\u66f4\u597d\u7684\u66ff\u4ee3\u3002<\/li><li><\/li><li><a href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#reactive\">\u7a33\u5b9a\u7684Reactive Integrations\u3002<\/a>\u6211\u4eec\u6dfb\u52a0\u4e86\u66f4\u591a\u7528\u4e8e\u5c06Reactive Streams\u7c7b\u578b\u8f6c\u6362\u6210Kotlin Flow\u7684\u51fd\u6570\uff0c\u5f88\u591a\u73b0\u6709\u7684\u51fd\u6570\u548cReactiveContext API\u5df2\u53d8\u5f97\u7a33\u5b9a\u3002<\/li><\/ul>\n\n\n\n<!--more-->\n\n\n<p><iframe loading=\"lazy\" src=\"\/\/player.bilibili.com\/player.html?aid=888481651&amp;bvid=BV15K4y137um&amp;cid=348369952&amp;page=1\" scrolling=\"no\" border=\"0\" frameborder=\"no\" framespacing=\"0\" allowfullscreen=\"true\" width=\"640\" height=\"480\"> <\/iframe><\/p>\n\n\n<p align=\"center\"><a class=\"ek-link jb-download-button\" title=\"Use Coroutines 1.5.0\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#use-coroutines-1-5-0\">\u5f00\u59cb\u4f7f\u7528Coroutines 1.5.0<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"globalscope\">GlobalScope API \u5df2\u88ab\u6807\u8bb0\u4e3a delicate<\/h1>\n\n\n\n<p>\u73b0\u5728 <code>GlobalScope<\/code> \u7c7b\u5df2\u88ab <code>@DelicateCoroutinesApi<\/code> \u6ce8\u89e3\u6240\u6807\u8bb0\u3002&nbsp;\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6240\u6709\u4f7f\u7528 <code>GlobalScope<\/code> \u7684\u5730\u65b9\u90fd\u9700\u8981 <code>@OptIn(DelicateCoroutinesApi::class)<\/code> \u663e\u5f0f\u5f15\u5165\u3002<\/p>\n\n\n\n<p>\u867d\u7136\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u4e0d\u5efa\u8bae\u4f7f\u7528 <code>GlobalScope<\/code>\uff0c\u4f46<a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-basics.html#your-first-coroutine\" target=\"_blank\" rel=\"noopener\">\u5b98\u65b9\u6587\u6863<\/a>\u4ecd\u901a\u8fc7\u8fd9\u4e2a\u7cbe\u7ec6\u7684 API \u5f15\u5165\u8fd9\u4e9b\u6982\u5ff5\u3002<\/p>\n\n\n\n<p>\u5168\u5c40 <code>CoroutineScope<\/code> \u4e0d\u4f1a\u548c\u4efb\u4f55job\u7ed1\u5b9a\u3002&nbsp;GlobalScope \u591a\u7528\u4e8e\u542f\u52a8\u8fd0\u884c\u5728\u6574\u4e2a\u5e94\u7528\u751f\u547d\u5468\u671f\u5185\u4e14\u4e0d\u4f1a\u63d0\u524d\u53d6\u6d88\u7684\u9876\u7ea7\u534f\u7a0b\u3002&nbsp;\u5728 <code>GlobalScope<\/code> \u4e2d\u542f\u52a8\u7684\u6d3b\u52a8\u4e2d\u534f\u7a0b\u4e0d\u4f1a\u8ba9\u8be5\u8fdb\u7a0b\u4fdd\u6301\u5b58\u6d3b\u72b6\u6001\u3002&nbsp;\u5c31\u50cf\u5b88\u62a4\u7ebf\u7a0b\u4e00\u6837\u3002<\/p>\n\n\n\n<p>\u6e90\u4e8e <code>GlobalScope<\/code> \u7684\u7cbe\u5bc6\uff0c\u5176\u4f7f\u7528\u5bb9\u6613\u5bfc\u81f4\u8d44\u6e90\u6216\u5185\u5b58\u6cc4\u9732\u3002&nbsp;\u4ece <code>GlobalScope<\/code> \u4e2d\u542f\u52a8\u7684\u534f\u7a0b\u5e76\u4e0d\u9075\u5b88\u7ed3\u6784\u5316\u5e76\u53d1\u7684\u539f\u5219\uff0c\u4e00\u65e6\u56e0\u4e3a\u67d0\u79cd\u539f\u56e0(\u4f8b\u5982\u8f83\u6162\u7684\u7f51\u901f)\u88ab\u6302\u8d77\u6216\u5ef6\u8fdf\uff0c\u5b83\u4f1a\u6301\u7eed\u8fd0\u884c\u5e76\u6d88\u8017\u8d44\u6e90\u3002&nbsp;\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nfun loadConfiguration() {\n    GlobalScope.launch {\n        val config = fetchConfigFromServer() \/\/ network request\n        updateConfiguration(config)\n    }\n}\n<\/pre>\n\n\n\n<p>\u8c03\u7528 <code>loadConfiguration<\/code> \u4f1a\u5728 <code>GlobalScope<\/code> \u4e2d\u521b\u5efa\u4e00\u4e2a\u540e\u53f0\u5de5\u4f5c\u7684\u534f\u7a0b\uff0c\u5e76\u4e14\u4e0d\u4f1a\u7b49\u5f85\u5176\u53d6\u6d88\u6216\u5b8c\u6210\u3002&nbsp;\u4e00\u65e6\u7f51\u7edc\u5f88\u6162\uff0c\u5b83\u4f1a\u4e00\u76f4\u5728\u540e\u53f0\u7b49\u5f85\u5e76\u4e14\u6d88\u8017\u8d44\u6e90\u3002&nbsp;\u91cd\u590d\u4ee3\u7528 <code>loadConfiguration<\/code> \u5c06\u6d88\u8017\u66f4\u591a\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53ef\u884c\u7684\u66ff\u4ee3<\/h2>\n\n\n\n<p>\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u5e94\u907f\u514d\u4f7f\u7528 <code>GlobalScope<\/code>\uff0c\u5e76\u4e14\u5176\u5305\u542b\u7684\u51fd\u6570\u5e94\u6807\u8bb0\u4e3a <code>suspend<\/code>\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nsuspend fun loadConfiguration() {\n    val config = fetchConfigFromServer() \/\/ network request\n    updateConfiguration(config)\n}\n<\/pre>\n\n\n\n<p>\u5982\u679c\u901a\u8fc7 <code>GlobalScope.launch<\/code> \u542f\u52a8\u591a\u4e2a\u5e76\u53d1\u64cd\u4f5c\uff0c\u5219\u5e94\u5c06\u76f8\u5173\u7684\u64cd\u4f5c\u901a\u8fc7 <code>coroutineScope<\/code> \u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\n\/\/ concurrently load configuration and data\nsuspend fun loadConfigurationAndData() {\n    coroutineScope {\n        launch { loadConfiguration() }\n        launch { loadData() }\n    }\n}\n<\/pre>\n\n\n\n<p>\u4ece\u9876\u5c42\u4ee3\u7801\u7684\u975e\u6302\u8d77\u4e0a\u4e0b\u6587\u4e2d\u542f\u52a8\u5e76\u53d1\u64cd\u4f5c\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528\u6709\u6240\u9650\u5236\u7684 <code>CoroutineScope<\/code> \u5b9e\u4f8b\u6765\u4ee3\u66ff <code>GlobalScope<\/code>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5408\u7406\u7684\u7528\u6cd5<\/h2>\n\n\n\n<p>\u4ec5\u4ec5\u6709\u9650\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u5408\u7406\u4e14\u5b89\u5168\u5730\u4f7f\u7528 <code>GlobalScope<\/code>\uff0c\u4f8b\u5982\u5fc5\u987b\u5728\u6574\u4e2a\u5e94\u7528\u751f\u547d\u5468\u671f\u8fc7\u7a0b\u4e2d\u4fdd\u6301\u6d3b\u52a8\u7684\u9876\u5c42\u540e\u53f0\u8fdb\u7a0b\u3002&nbsp;\u56e0\u6b64\uff0c\u6240\u6709\u4f7f\u7528 <code>GlobalScope<\/code> \u7684\u5730\u65b9\u90fd\u9700\u8981 <code>@OptIn(DelicateCoroutinesApi::class)<\/code> \u663e\u5f0f\u5f15\u5165\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\n\/\/ A global coroutine to log statistics every second, \n\/\/ must be always active\n@OptIn(DelicateCoroutinesApi::class)\nval globalScopeReporter = GlobalScope.launch {\n    while (true) {\n        delay(1000)\n        logStatistics()\n    }\n}\n<\/pre>\n\n\n\n<p>\u6211\u4eec\u5efa\u8bae\u60a8\u4ed4\u7ec6\u5ba1\u67e5\u6240\u6709\u5bf9 <code>GlobalScope<\/code> \u7684\u7528\u6cd5\uff0c\u5e76\u6ce8\u89e3\u90a3\u4e9b\u5c5e\u4e8e\u201c\u5408\u7406\u7528\u4f8b\u201d\u7684\u7528\u6cd5\u3002&nbsp;\u5bf9\u4e8e\u5176\u4ed6\u7528\u6cd5\uff0c\u5b83\u4eec\u5f88\u53ef\u80fd\u662f\u4ee3\u7801\u4e2d\u7684 bug \u2014\u2014 \u5982\u4e0a\u6240\u8ff0\u6765\u66ff\u6362\u6389 <code>GlobalScope<\/code> \u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"junit\">JUnit5 \u6269\u5c55<\/h1>\n\n\n\n<p>\u6211\u4eec\u6dfb\u52a0\u4e86\u5141\u8bb8\u5728\u72ec\u7acb\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u6d4b\u8bd5\u7684 <code>CoroutinesTimeout<\/code> \u6ce8\u89e3\uff0c\u5e76\u5728\u9650\u5236\u7684\u65f6\u95f4\u4e4b\u540e\u8ba9\u6d4b\u8bd5\u5931\u6548\u5e76\u4e2d\u65ad\u7ebf\u7a0b\u3002&nbsp;\u5728\u4e4b\u524d\uff0c<code>CoroutinesTimeout<\/code> \u53ea\u5728 JUnit4 \u4e2d\u53ef\u7528\u3002&nbsp;\u5728\u8fd9\u4e2a\u6b63\u5f0f\u7248\u4e2d\uff0c\u6211\u4eec\u5df2\u5c06\u5176\u96c6\u6210\u5230 JUnit5 \u4e2d\u4e86\u3002<\/p>\n\n\n\n<p>\u8981\u4f7f\u7528\u8fd9\u4e2a\u65b0\u7684\u6ce8\u89e3\uff0c\u8bf7\u5728\u60a8\u7684\u9879\u76ee\u4e2d\u6dfb\u52a0\u4e0b\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\ndependencies {\n  \u2026\n  testImplementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutinesVersion&quot;)\n}\n<\/pre>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u8bf4\u660e\u5982\u4f55\u5728\u6d4b\u8bd5\u4e2d\u4f7f\u7528\u65b0\u7684 <code>CoroutinesTimeout<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport kotlinx.coroutines.debug.junit5.CoroutinesTimeout\nimport kotlinx.coroutines.delay\nimport kotlinx.coroutines.runBlocking\nimport org.junit.Test\n\n@CoroutinesTimeout(100)\nclass CoroutinesTimeoutSimpleTest {\n\n     @CoroutinesTimeout(300)\n     @Test\n     fun firstTest() {\n         runBlocking {\n             delay(200)  \/\/ succeeds\n         }\n     }\n\n     @Test\n     fun secondTest() {\n         runBlocking {\n             delay(200)  \/\/ fails\n         }\n     }\n }\n<\/pre>\n\n\n\n<p>\u5728\u8be5\u793a\u4f8b\u4e2d\uff0cCoroutinesTimeout\u662f\u7c7b\u7ea7\u522b\u6ce8\u89e3\uff0c\u5e76\u4e14\u8fd8\u6807\u8bb0\u4e86<code>&nbsp;firstTest<\/code>\u65b9\u6cd5\u3002&nbsp;\u88ab\u6ce8\u89e3\u7684\u6d4b\u8bd5\u65b9\u6cd5\u4e0d\u4f1a\u8d85\u65f6\uff0c\u56e0\u4e3a\u51fd\u6570\u7ea7\u522b\u7684\u6ce8\u89e3\u8986\u76d6\u4e86\u7c7b\u7ea7\u522b\u7684\u6ce8\u89e3\u3002&nbsp;\u800c<code>secondTest<\/code>\u7528\u5230\u4e86\u7c7b\u7ea7\u522b\u7684\u6ce8\u89e3\uff0c\u56e0\u6b64\u5b83\u4f1a\u8d85\u65f6\u3002<\/p>\n\n\n\n<p>\u6ce8\u89e3\u4ee5\u4e0b\u8ff0\u5f62\u5f0f\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\npackage kotlinx.coroutines.debug.junit5\n\npublic annotation class CoroutinesTimeout(\n    val testTimeoutMs: Long,\n    val cancelOnTimeout: Boolean = false\n)\n<\/pre>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570<code>testTimeoutMs<\/code>\uff0c\u4ee5\u6beb\u79d2\u7ea7\u5b9a\u4e49\u4e86\u8d85\u65f6\u65f6\u95f4\u3002&nbsp;\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>cancelOnTimeout<\/code>\uff0c\u5b9a\u4e49\u4e86\u662f\u5426\u5728\u8d85\u65f6\u540e\u53d6\u6d88\u6240\u6709\u6b63\u5728\u8fd0\u884c\u7684\u534f\u7a0b\u3002&nbsp;\u5982\u679c\u8bbe\u7f6e\u4e3a<code>true<\/code>\uff0c\u5219\u6240\u6709\u534f\u7a0b\u5c06\u88ab\u81ea\u52a8\u53d6\u6d88\u3002<\/p>\n\n\n\n<p>\u6bcf\u5f53\u60a8\u4f7f\u7528<code>CoroutinesTimeout<\/code>\u6ce8\u89e3\uff0c\u5b83\u4f1a\u81ea\u52a8\u542f\u7528<a href=\"https:\/\/blog.jetbrains.com\/kotlin\/2020\/07\/kotlin-1-4-rc-debugging-coroutines\/\">\u534f\u7a0b\u8c03\u8bd5\u5668<\/a>\u5e76\u5728\u8d85\u65f6\u65f6\u8f6c\u50a8\u6240\u6709\u534f\u7a0b\u3002&nbsp;\u5176\u8f6c\u50a8\u5305\u542b\u4e86\u534f\u7a0b\u521b\u5efa\u7684\u5806\u6808\u8f68\u8ff9\u3002&nbsp;\u5982\u679c\u6709\u9700\u8981\u7981\u7528\u5bf9\u521b\u5efa\u7684\u5806\u6808\u8ddf\u8e2a\u4ee5\u52a0\u5feb\u6d4b\u8bd5\u901f\u5ea6\uff0c\u8bf7\u76f4\u63a5\u4f7f\u7528<code><a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/blob\/master\/kotlinx-coroutines-debug\/src\/junit\/junit5\/CoroutinesTimeoutExtension.kt\" target=\"_blank\" rel=\"noopener\">CoroutinesTimeoutExtension<\/a><\/code>\uff0c\u5b83\u5141\u8bb8\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n\n\n\n<p>\u975e\u5e38\u611f\u8c22 <a href=\"https:\/\/github.com\/asarkar\" target=\"_blank\" rel=\"noopener\">Abhijit Sarkar<\/a>\uff0c\u4ed6\u4e3a <a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/issues\/2197\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">JUnit 5 \u7684 CoroutinesTimeout<\/a> \u6784\u5efa\u4e86\u4e00\u4e2a\u5b9e\u7528\u7684\u7684 PoC\u3002&nbsp;\u8fd9\u4e2a\u60f3\u6cd5\u88ab\u5f00\u53d1\u6210\u4e3a\u6211\u4eec\u57281.5\u7248\u672c\u4e2d\u6dfb\u52a0\u7684 <code>CoroutinesTimeout<\/code> \u65b0\u6ce8\u89e3\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"channels\">\u5b8c\u5584\u7684 Channel API<\/h1>\n\n\n\n<p>\u901a\u9053\u662f\u91cd\u8981\u7684\u901a\u4fe1\u539f\u8bed\uff0c\u53ef\u8ba9\u60a8\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u534f\u7a0b\u548c\u56de\u8c03\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002&nbsp;\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u91cd\u65b0\u8bbe\u8ba1\u4e86\u90e8\u5206 Channel API\uff0c\u7528\u66f4\u597d\u7684\u9009\u62e9\u6765\u66ff\u6362\u4e86\u5f15\u8d77\u6df7\u6dc6\u7684 <code>offer<\/code> \u548c <code>poll<\/code> \u51fd\u6570\u3002&nbsp;\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e3a\u6302\u8d77\u548c\u975e\u6302\u8d77\u65b9\u6cd5\u8bbe\u8ba1\u4e86\u4e00\u79cd\u65b0\u7684\u7edf\u4e00\u547d\u540d\u65b9\u6848\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b0\u7684\u547d\u540d\u65b9\u6848<\/h2>\n\n\n\n<p>\u6211\u4eec\u8bd5\u56fe\u5efa\u7acb\u7edf\u4e00\u7684\u547d\u540d\u65b9\u6848\uff0c\u4ee5\u4fbf\u540e\u7eed\u5728\u5176\u4ed6\u5e93\u6216Coroutines API\u4e2d\u4f7f\u7528\u3002&nbsp;\u6211\u4eec\u60f3\u8981\u786e\u4fdd\u51fd\u6570\u7684\u540d\u79f0\u80fd\u591f\u4f20\u8fbe\u5176\u884c\u4e3a\u7684\u4fe1\u606f\u3002&nbsp;\u7ed3\u679c\u662f\uff0c\u6211\u4eec\u63d0\u51fa\u4e86\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<ul><li>\u5e38\u89c4\u7684\u6302\u8d77\u65b9\u6cd5\u4fdd\u6301\u4e0d\u53d8\uff0c\u4f8b\u5982\uff0c<code>send<\/code>\uff0c<code>receive<\/code>\u3002<\/li><li>\u5c01\u88c5\u4e86\u5f02\u5e38\u7684\u76f8\u5e94\u975e\u6302\u8d77\u65b9\u6cd5\u59cb\u7ec8\u4ee5&#8221;try&#8221;\u4e3a\u524d\u7f00\uff1a<code>trySend<\/code> \u548c <code>tryReceive<\/code>\uff0c\u800c\u975e\u65e7\u7684 <code>offer<\/code> \u548c <code>poll<\/code>\u3002<\/li><li>\u65b0\u7684\u5c01\u88c5\u5f02\u5e38\u7684\u6302\u8d77\u65b9\u6cd5\u5c06\u4ee5&#8221;Catching&#8221;\u4f5c\u4e3a\u540e\u7f00\u3002<\/li><\/ul>\n\n\n\n<p>\u8ba9\u6211\u4eec\u6765\u6df1\u5165\u4e86\u89e3\u8fd9\u4e9b\u65b0\u65b9\u6cd5\u7684\u7ec6\u679d\u672b\u8282\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><code><code>Try<\/code> \u51fd\u6570\uff1a<code>send<\/code> \u548c <code>receive<\/code> \u7684\u975e\u6302\u8d77\u7248\u672c<\/code><\/h2>\n\n\n\n<p>\u4e00\u4e2a\u534f\u7a0b\u53ef\u4ee5\u5411\u901a\u9053\u53d1\u9001\u4e00\u4e9b\u4fe1\u606f\uff0c\u800c\u53e6\u4e00\u4e2a\u534f\u7a0b\u53ef\u4ee5\u4ece\u8be5\u901a\u9053\u63a5\u6536\u8fd9\u4e2a\u4fe1\u606f\u3002&nbsp;<code>send<\/code> \u548c <code>receive<\/code> \u51fd\u6570\u90fd\u662f\u6302\u8d77\u7684\u3002&nbsp;\u5982\u679c\u901a\u9053\u5df2\u6ee1\u5e76\u4e14\u4e0d\u80fd\u63a5\u53d7\u65b0\u7684\u5143\u7d20\uff0c\u5219 <code>send<\/code> \u6302\u8d77\u5176\u534f\u7a0b\uff0c\u800c\u5982\u679c\u901a\u9053\u4e2d\u6ca1\u6709\u8fd4\u56de\u5143\u7d20\uff0c\u5219 <code>receive<\/code> \u6302\u8d77\u5176\u534f\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport kotlinx.coroutines.channels.Channel\nimport kotlinx.coroutines.launch\nimport kotlinx.coroutines.runBlocking\n\nfun main() = runBlocking&lt;Unit&gt; {\n    val channel = Channel&lt;String&gt;()\n    launch {\n        \/\/ Suspends until the element can be sent\n        println(&quot;Sending...&quot;)\n        channel.send(&quot;Element&quot;)\n     }\n     \/\/ Suspends until the element can be received\n     println(&quot;Receiving...&quot;)\n     println(channel.receive())\n}\n<\/pre>\n\n\n\n<p>\u8fd9\u4e9b\u51fd\u6570\u62e5\u6709\u53ef\u5728\u540c\u6b65\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u975e\u6302\u8d77\u5f62\u5f0f\uff1a<code>offer<\/code>\u548c<code>poll<\/code>\uff0c\u4f46\u5df2\u88ab\u5e9f\u5f03\uff0c\u73b0\u5728\u63a8\u8350\u4f7f\u7528<code>trySend<\/code>\u548c<code>tryReceive<\/code>\u51fd\u6570\u3002&nbsp;\u8ba9\u6211\u4eec\u8ba8\u8bba\u8fd9\u79cd\u53d8\u52a8\u7684\u539f\u56e0\u3002<\/p>\n\n\n\n<p><code>offer<\/code> \u548c <code>poll<\/code> \u4e0e <code>send<\/code> \u548c <code>receive<\/code> \u505a\u4e86\u76f8\u540c\u7684\u4e8b\uff0c\u4f46\u4e0d\u4f1a\u5f15\u53d1\u6302\u8d77\u3002&nbsp;\u8fd9\u542c\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u800c\u4e14\u5728\u5143\u7d20\u53ef\u88ab\u6536\u53d1\u65f6\u4e00\u5207\u6b63\u5e38\u3002&nbsp;\u4e00\u65e6\u629b\u51fa\u5f02\u5e38\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f&nbsp;<code>send<\/code> \u548c <code>receive<\/code> \u4f1a\u6302\u8d77\u76f4\u5230\u5b83\u4eec\u80fd\u7ee7\u7eed\u4efb\u52a1\u3002&nbsp;\u4e00\u65e6\u901a\u9053\u5df2\u6ee1\u65e0\u6cd5\u6dfb\u52a0\u5143\u7d20\uff0c\u6216\u901a\u9053\u4e3a\u7a7a\u65e0\u6cd5\u68c0\u7d22\u4efb\u4f55\u5143\u7d20\uff0c<code>offer<\/code> \u548c <code>poll<\/code> \u4f1a\u7b80\u5355\u5730\u8fd4\u56de <code>false<\/code> \u548c <code>null<\/code>\u3002&nbsp;\u5b83\u4eec\u90fd\u4f1a\u5728\u5173\u95ed\u4e86\u7684\u901a\u9053\u91cc\u5c1d\u8bd5\u8fd0\u884c\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u4f46\u6700\u540e\u7ed3\u679c\u5374\u4ee4\u4eba\u8ff7\u60d1\u3002<\/p>\n\n\n\n<pre class=\"kotlin-code\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport kotlinx.coroutines.channels.Channel\nimport kotlinx.coroutines.launch\nimport kotlinx.coroutines.runBlocking\n\nfun main() = runBlocking&lt;Unit&gt; {\n    val channel = Channel&lt;String&gt;()\n    launch {\n        println(&quot;Sending...&quot;)\n        \/\/ Doesn&#039;t suspend\n        \/\/ Returns &#039;false&#039; if the channel is full\n        \/\/ Or throws an exception if it&#039;s closed\n        channel.offer(&quot;Element&quot;)\n    }\n    println(&quot;Receiving...&quot;)\n    \/\/ Doesn&#039;t suspend\n    \/\/ Returns &#039;null&#039; if the channel is empty\n    println(channel.poll())\n\/\/  channel.close()\n}\n<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5728\u5143\u7d20\u6dfb\u52a0\u524d\u8c03\u7528 <code>poll <\/code>\u5c06\u4f1a\u7acb\u5373\u8fd4\u56de <code>null<\/code>\u3002&nbsp;\u8bf7\u6ce8\u610f\u4e0d\u8be5\u8fd9\u6837\u4f7f\u7528\uff1a\u60a8\u5e94\u8be5\u6301\u7eed\u5b9a\u671f\u5730\u8f6e\u8be2\u5143\u7d20\uff0c\u6211\u4eec\u662f\u4e3a\u4e86\u7b80\u5316\u8bf4\u660e\u624d\u76f4\u63a5\u8c03\u7528\u3002&nbsp;<code>offer<\/code> \u7684\u8c03\u7528\u4e5f\u662f\u4e0d\u6210\u529f\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u901a\u9053\u662f\u4e00\u4e2a rendezvous \u7c7b\u578b\u7684\u901a\u9053\uff0c\u5e76\u4e14\u5176\u7f13\u51b2\u533a\u5bb9\u91cf\u4e3a\u96f6\u3002&nbsp;\u7ed3\u679c<code> offer <\/code>\u8fd4\u56de\u4e86 <code>false<\/code>\uff0c\u800c <code>poll<\/code> \u8fd4\u56de\u4e86 <code>null<\/code>\uff0c\u4ec5\u4ec5\u662f\u56e0\u4e3a\u5b83\u4eec\u4ee5\u9519\u8bef\u7684\u987a\u5e8f\u88ab\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u53d6\u6d88 <code>channel.close()<\/code>\u8bed\u53e5\u7684\u6ce8\u91ca\uff0c\u5c06\u4f1a\u629b\u51fa\u5f02\u5e38\u3002&nbsp;\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>poll<\/code> \u4ecd\u7136\u4f1a\u50cf\u4e4b\u524d\u4e00\u6837\u8fd4\u56de <code>false<\/code>\u3002&nbsp;\u4f46\u5f53 <code>offer<\/code> \u5c1d\u8bd5\u5411\u5df2\u5173\u95ed\u7684\u901a\u9053\u4e2d\u589e\u52a0\u4e00\u4e2a\u5143\u7d20\u65f6\uff0c\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\u3002&nbsp;\u6211\u4eec\u6536\u5230\u4e86\u5f88\u591a\u62b1\u6028\uff0c\u8868\u793a\u8fd9\u79cd\u884c\u4e3a\u5bb9\u6613\u51fa\u9519\u3002&nbsp;\u8fd9\u5f88\u5bb9\u6613\u4fbf\u5fd8\u8bb0\u53bb\u6355\u83b7\u8be5\u5f02\u5e38\uff0c\u53c8\u60a8\u5b81\u53ef\u5ffd\u7565\u6291\u6216\u4ee5\u5176\u4ed6\u65b9\u5f0f\u53bb\u5904\u7406\uff0c\u4f46\u5b83\u4f1a\u4ee4\u60a8\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n\n\n\n<p>\u65b0\u7684<code>trySend<\/code>\u548c<code>tryReceive<\/code>\u4fee\u590d\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u5e76\u8fd4\u56de\u4e86\u5305\u542b\u66f4\u591a\u7ec6\u8282\u7684\u7ed3\u679c\u3002&nbsp;\u6bcf\u4e2a\u8fd4\u56de\u7684<code>ChannelResult<\/code>\u5b9e\u4f8b\uff0c\u4e3a\u4ee5\u4e0b\u4e09\u79cd\u5176\u4e2d\u4e4b\u4e00\uff1a\u6210\u529f\u7684\u7ed3\u679c\uff0c\u5931\u8d25\u6216\u901a\u9053\u5df2\u5173\u95ed\u7684\u6807\u8bb0\u3002<\/p>\n\n\n\n<pre class=\"kotlin-code\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport kotlinx.coroutines.channels.Channel\nimport kotlinx.coroutines.launch\nimport kotlinx.coroutines.runBlocking\n\nfun main() = runBlocking&lt;Unit&gt; {\n    val channel = Channel&lt;String&gt;()\n    launch {\n        println(&quot;Sending...&quot;)\n        \/\/ Doesn&#039;t suspend\n        \/\/ Returns &#039;Failed&#039; if the channel is full\n        \/\/ Or &#039;Channel was closed&#039; result if it&#039;s closed\n        val result = channel.trySend(&quot;Element&quot;)\n        println(result)\n\n        \/\/We can verify the result\n        if(result.isClosed){\n            println(&quot;Sending failed. The channel is closed.&quot;)\n        }\n    }\n    println(&quot;Receiving...&quot;)\n    println(channel.tryReceive())\n\/\/  channel.close()\n}\n<\/pre>\n\n\n\n<p>\u8be5\u793a\u4f8b\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u4e0a\u4e00\u4e2a\u76f8\u540c\uff0c\u533a\u522b\u5728\u4e8e <code>tryReceive<\/code> \u548c <code>trySend<\/code> \u8fd4\u56de\u4e86\u66f4\u8be6\u5c3d\u7684\u7ed3\u679c\u3002&nbsp;\u60a8\u53ef\u4ee5\u5728\u8f93\u51fa\u4e2d\u770b\u5230 <code>Value(Failed)<\/code>\uff0c\u800c\u4e0d\u662f <code>false<\/code> \u548c <code>null<\/code>\u3002&nbsp;\u518d\u6b21\u53d6\u6d88\u5173\u95ed\u901a\u9053\u8bed\u53e5\u7684\u6ce8\u91ca\uff0c\u73b0\u5728 <code>trySend<\/code> \u4f1a\u8fd4\u56de <code>Closed<\/code> \u5f02\u5e38\u88ab\u6355\u83b7\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u591a\u4e8f\u4e86<a href=\"https:\/\/kotlinlang.org\/docs\/inline-classes.html\" target=\"_blank\" rel=\"noopener\">\u5185\u8054\u503c\u7c7b<\/a>\uff0c<code>ChannelResult<\/code>&nbsp;\u4e0d\u4f1a\u6709\u989d\u5916\u7684\u5c01\u88c5\u7c7b\uff0c\u5e76\u4e14\u5982\u679c\u8fd4\u56de\u7684\u662f\u6210\u529f\u503c\uff0c\u5219\u539f\u6837\u8fd4\u56de\uff0c\u800c\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u5f00\u9500\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5f02\u5e38\u6355\u83b7\u51fd\u6570\uff1a\u5e26\u5f02\u5e38\u5c01\u88c5\u7684\u6302\u8d77\u51fd\u6570<\/h2>\n\n\n\n<p>\u4ece\u8fd9\u4e2a\u7248\u672c\u5f00\u59cb\uff0c\u5e26\u5f02\u5e38\u5c01\u88c5\u7684\u6302\u8d77\u65b9\u6cd5\u5c06\u4ee5&#8221;Catching&#8221;\u4f5c\u4e3a\u540e\u7f00\u3002&nbsp;\u4f8b\u5982\uff0c\u65b0\u7684 <code>receiveCatching<\/code> \u51fd\u6570\u4f1a\u5904\u7406\u901a\u9053\u5173\u95ed\u60c5\u51b5\u4e0b\u7684\u5f02\u5e38\u3002&nbsp;\u8bf7\u601d\u8003\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport kotlinx.coroutines.channels.Channel\nimport kotlinx.coroutines.launch\nimport kotlinx.coroutines.runBlocking\n\nfun main() = runBlocking&lt;Unit&gt; {\n    val channel = Channel&lt;String&gt;()\n    channel.close()\n    println(channel.receiveCatching())\n}\n<\/pre>\n\n\n\n<p>\u5728\u6211\u4eec\u5c1d\u8bd5\u63a5\u6536\u503c\u4e4b\u524d\u901a\u9053\u5df2\u88ab\u5173\u95ed\u3002&nbsp;\u4f46\u662f\u7a0b\u5e8f\u6210\u529f\u6267\u884c\u5b8c\u6bd5\uff0c\u8868\u660e\u8be5\u901a\u9053\u5df2\u5173\u95ed\u3002&nbsp;\u5982\u679c\u5c06 <code>receiveCatching<\/code> \u6362\u6210\u666e\u901a\u7684 <code>receive<\/code> \u51fd\u6570\uff0c\u5b83\u5c06\u629b\u51fa <code>ClosedReceiveChannelException<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport kotlinx.coroutines.channels.Channel\nimport kotlinx.coroutines.launch\nimport kotlinx.coroutines.runBlocking\n\nfun main() = runBlocking&lt;Unit&gt; {\n    val channel = Channel&lt;String&gt;()\n    channel.close()\n    println(channel.receive())\n}\n<\/pre>\n\n\n\n<p>\u76ee\u524d\u6211\u4eec\u53ea\u63d0\u4f9b\u4e86 <code>receiveCatching<\/code> \u548c <code>onReceiveCatching<\/code>(\u800c\u975e\u4e4b\u524d\u5185\u90e8\u7684 <code>receiveOrClosed&nbsp;<\/code>)\uff0c\u4f46\u6211\u4eec\u6253\u7b97\u6dfb\u52a0\u66f4\u591a\u51fd\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8fc1\u79fb\u60a8\u7684\u4ee3\u7801\u81f3\u65b0\u51fd\u6570<\/h2>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u7528\u65b0\u7684\u5feb\u6377\u6267\u884c\u81ea\u52a8\u66ff\u6362\u9879\u76ee\u4e2d <code>offer<\/code> \u548c <code>poll<\/code> \u51fd\u6570\u7684\u6240\u6709\u7528\u6cd5\u3002&nbsp;\u7531\u4e8e <code>offer<\/code> \u8fd4\u56de\u4e86<code>\u5e03\u5c14\u503c<\/code>\uff0c\u56e0\u6b64\u5176\u7b49\u6548\u66ff\u6362\u4e3a <code>channel.trySend(\u201c Element\u201d).isSuccess<\/code>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1600\" height=\"800\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/05\/cor15-offer-deprecated.png\" alt=\"\" class=\"wp-image-142625\"\/><\/figure>\n\n\n\n<p>\u540c\u6837\uff0c<code>poll<\/code> \u51fd\u6570\u8fd4\u56de\u53ef\u7a7a\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u5c06\u5176\u66ff\u6362\u4e3a <code>channel.tryReceive().getOrNull()<\/code>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1440\" height=\"440\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/05\/cor15-poll-deprecated.png\" alt=\"\" class=\"wp-image-142636\"\/><\/figure>\n\n\n\n<p>\u5982\u679c\u8fd4\u56de\u7684\u7ed3\u679c\u5e76\u6ca1\u6709\u88ab\u4f7f\u7528\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u76f4\u63a5\u66ff\u6362\u4e3a\u65b0\u7684\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u5bf9\u4e8e\u5f02\u5e38\u7684\u5904\u7406\u6709\u6240\u4e0d\u540c\uff0c\u56e0\u6b64\u4e5f\u8bb8\u9700\u8981\u60a8\u624b\u52a8\u8fdb\u884c\u5fc5\u8981\u7684\u66f4\u65b0\u3002&nbsp;\u5982\u679c\u60a8\u7684\u4ee3\u7801\u4f9d\u8d56\u4e8e &#8216;offer&#8217; \u548c &#8216;poll&#8217; \u65b9\u6cd5\u5728\u5df2\u5173\u95ed\u901a\u9053\u4e0a\u629b\u51fa\u7684\u5f02\u5e38\uff0c\u5219\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b\u66ff\u4ee3\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><code>channel.offer(\"Element\")<\/code> \u7684\u7b49\u6548\u66ff\u6362\u5728\u5df2\u5173\u95ed\u7684\u901a\u9053\u4f1a\u629b\u51fa\u5f02\u5e38\uff0c\u5373\u4fbf\u901a\u9053\u662f\u6b63\u5e38\u5173\u95ed\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nchannel\n  .trySend(&quot;Element&quot;)\n  .onClosed { throw it ?: ClosedSendChannelException(&quot;Channel was closed&quot;) }\n  .isSuccess\n<\/pre>\n\n\n\n<p>\u5982\u679c\u901a\u9053\u7531\u4e8e\u9519\u8bef\u800c\u5173\u95ed\uff0c\u5219 <code>channel.poll()<\/code> \u7684\u7b49\u6548\u66ff\u6362\u5c06\u5f15\u629b\u51fa\u5f02\u5e38\uff0c\u5982\u679c\u6b63\u5e38\u5173\u95ed\uff0c\u5219\u8fd4\u56de <code>null<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nchannel.tryReceive()\n  .onClosed { if (it != null) throw it }\n  .getOrNull()\n<\/pre>\n\n\n\n<p>\u8fd9\u6837\u7684\u53d8\u52a8\u53cd\u6620\u4e86 <code>offer<\/code> \u548c <code>poll<\/code> \u51fd\u6570\u65e7\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5047\u5b9a\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u60a8\u7684\u4ee3\u7801\u4e0d\u4f1a\u4f9d\u8d56\u4e8e\u5df2\u5173\u95ed\u901a\u9053\u7684\u7ec6\u5fae\u4e4b\u5904\uff0c\u800c\u662f\u4f9d\u8d56\u5b83\u672c\u8eab\u662f\u9519\u8bef\u7684\u6839\u6e90\u3002&nbsp;\u56e0\u6b64\uff0cIDE \u63d0\u4f9b\u7684\u81ea\u52a8\u66ff\u6362\u529f\u80fd\u7b80\u5316\u4e86\u8bed\u4e49\u3002&nbsp;\u5982\u679c\u8fd9\u5e76\u4e0d\u7b26\u5408\u60a8\u7684\u60c5\u51b5\uff0c\u8bf7\u624b\u52a8\u5ba1\u67e5\u5e76\u66f4\u65b0\uff0c\u5e76\u8003\u8651\u5b8c\u5168\u8fdb\u884c\u91cd\u5199\uff0c\u4ee5\u4e0d\u540c\u65b9\u5f0f\u5904\u7406\u901a\u9053\u5df2\u88ab\u5173\u95ed\u7684\u60c5\u51b5\uff0c\u800c\u4e0d\u662f\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"reactive\">\u54cd\u5e94\u5f0f\u96c6\u6210\u8fc8\u5411\u7a33\u5b9a\u4e4b\u65c5<\/h1>\n\n\n\n<p>\u5bf9\u4e8e\u8d1f\u8d23\u54cd\u5e94\u5f0f\u6846\u67b6\u96c6\u6210\u7684\u5927\u90e8\u5206\u51fd\u6570\uff0cKotlin Coroutines \u7684 1.5 \u7248\u672c\u5c06\u5176\u5347\u7ea7\u5230\u7a33\u5b9a API\u3002<\/p>\n\n\n\n<p>\u5728JVM\u751f\u6001\u7cfb\u7edf\uff0c\u53ea\u6709\u5c11\u6570\u6846\u67b6\u53ef\u4ee5\u5904\u7406\u7b26\u5408<a href=\"https:\/\/www.reactive-streams.org\/\" target=\"_blank\" rel=\"noopener\">Reactive Streams<\/a>\u6807\u51c6\u7684\u5f02\u6b65\u6d41\u3002&nbsp;\u4f8b\u5982\uff0c<a href=\"https:\/\/projectreactor.io\/\" target=\"_blank\" rel=\"noopener\">Project Reactor<\/a>\u548c<a href=\"https:\/\/github.com\/ReactiveX\/RxJava\" target=\"_blank\" rel=\"noopener\">RxJava<\/a>\u662f\u8be5\u9886\u57df\u4e2d\u6700\u6d41\u884c\u7684\u4e24\u4e2aJava\u6846\u67b6\u3002<\/p>\n\n\n\n<p>\u800c<a href=\"https:\/\/kotlinlang.org\/docs\/flow.html\" target=\"_blank\" rel=\"noopener\">Kotlin Flow<\/a>\u6709\u6240\u4e0d\u540c\uff0c\u5e76\u4e14\u5176\u7c7b\u578b\u4e0e\u6807\u51c6\u6307\u5b9a\u7684\u4e0d\u517c\u5bb9\uff0c\u4f46\u4ece\u6982\u5ff5\u4e0a\u6765\u8bb2\u5b83\u4eec\u4ecd\u7136\u662f\u6d41\u3002&nbsp;\u53ef\u5c06<code>Flow<\/code>\u8f6c\u6362\u4e3a\u54cd\u5e94\u5f0f(\u89c4\u8303\u4e14\u517c\u5bb9<a href=\"https:\/\/github.com\/reactive-streams\/reactive-streams-jvm\/tree\/master\/tck\" target=\"_blank\" rel=\"noopener\">TCK<\/a>\u7684)<code>Publisher<\/code>\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002&nbsp;\u8fd9\u4e9b\u5f00\u7bb1\u5373\u7528\u7684\u8f6c\u6362\u5668\u662f\u7531<code>kotlinx.coroutines<\/code>\u63d0\u4f9b\u7684\uff0c\u53ef\u4ee5\u5728\u76f8\u5e94\u7684\u54cd\u5e94\u5f0f\u6a21\u5757\u4e2d\u627e\u5230\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u9700\u8981\u4e0eProject Reactor\u7c7b\u578b\u7684\u4e92\u64cd\u4f5c\u6027\uff0c\u5219\u5e94\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u4ee5\u4ee5\u4e0b\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\ndependencies {          \n    implementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-core:${coroutinesVersion}&quot;)\n    implementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-reactor:${coroutinesVersion}&quot;)\n}\n<\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u5982\u679c\u9700\u8981 <em>Reactive Streams<\/em> \u7c7b\u578b\uff0c\u5219\u4f7f\u7528 <code>Flow&lt;T&gt;.asPublisher()<\/code>\uff0c\u6216 <code>Flow&lt;T&gt;.asFlux()<\/code>\uff08\u5982\u679c\u60a8\u9700\u8981 Project Reactor \u7c7b\u578b\uff09\u3002<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\n\/\/ acquire a Flow instance\nval flow: Flow&lt;event&gt; = flow { \u2026 }\n\n\/\/ Convert Flow to Publisher\nval publisher = flow.asPublisher()\n\n\/\/ Convert Flow to Reactor&#039;s Flux\nval flux = flow.asFlux()\n\n\/\/Convert back to Flow \nval anotherFlow = flux.asFlow()\n<\/pre>\n\n\n\n<p>\u8fd9\u662f\u8be5\u4e3b\u9898\u7684\u4e00\u4e2a\u6982\u89c8\u3002&nbsp;\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u8003\u8651\u9605\u8bfb<a href=\"https:\/\/elizarov.medium.com\/reactive-streams-and-kotlin-flows-bfd12772cda4\" target=\"_blank\" rel=\"noopener\">Roman Elizarov\u2019s\u7684Reactive Streams and Kotlin Flows\u7684\u6587\u7ae0<\/a>\u3002<\/p>\n\n\n\n<p>\u867d\u7136\u4e0e\u54cd\u5e94\u5f0f\u5e93\u7684\u96c6\u6210\u6b63\u671d\u7740API\u7a33\u5b9a\u7684\u65b9\u5411\u52aa\u529b\uff0c\u4f46\u4ece\u6280\u672f\u4e0a\u8bb2\uff0c\u76ee\u6807\u662f\u6446\u8131 <code>@ExperimentalCoroutinesApi<\/code>\u5e76\u5b9e\u73b0\u5404\u4e3b\u9898\u7684\u5269\u4f59\u5185\u5bb9\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6539\u5584\u4e0e Reactive Streams \u7684\u96c6\u6210<\/h2>\n\n\n\n<p>\u4e3a\u4e86\u786e\u4fdd\u7b2c\u4e09\u65b9\u6846\u67b6\u4e0e Kotlin Coroutines \u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u6027\uff0c\u4e0e Reactive Streams \u89c4\u8303\u7684\u517c\u5bb9\u6027\u5f88\u91cd\u8981\u3002&nbsp;\u8fd9\u5bf9\u4e8e\u5728\u8001\u65e7\u9879\u76ee\u4e2d\u65e0\u9700\u91cd\u5199\u4efb\u4f55\u4ee3\u7801\u4fbf\u80fd\u5e94\u91c7\u7528 Kotlin Coroutines \u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n\n\n\n<p>\u8fd9\u6b21\u6211\u4eec\u8bbe\u6cd5\u5c06<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/commit\/47a063c0987177551bdbdf09a458998a30571ac2\" target=\"_blank\" rel=\"noopener\">\u5927\u91cf\u7684\u51fd\u6570<\/a>\u63d0\u5347\u5230\u7a33\u5b9a\u7248\u3002&nbsp;\u73b0\u5728\u53ef\u4ee5\u5c06\u4efb\u4f55 Reactive Streams \u7c7b\u578b\u8f6c\u6362\u4e3a <code>Flow<\/code> \u5e76\u8fd4\u56de\u3002&nbsp;\u4f8b\u5982\u65b0\u4ee3\u7801\u53ef\u4ee5\u7528 Coroutines \u7f16\u5199\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u53cd\u5411\u8f6c\u6362\u5668\u4e0e\u65e7\u7684\u54cd\u5e94\u5f0f\u4ee3\u7801\u5e93\u8fdb\u884c\u96c6\u6210\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nfun legacyFunThatHaveToReturnObservable(): Observable&lt;int&gt; {\n  return flow&lt;int&gt; {\n    \/\/ Use the power of flow!\n  }\n  \/\/ various flow operations\n  .asObservable()\n}\n<\/pre>\n\n\n\n<p>\u6b64\u5916\uff0c\u5bf9 <a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/blob\/master\/reactive\/kotlinx-coroutines-reactor\/src\/ReactorContext.kt\" target=\"_blank\" rel=\"noopener\"><code>ReactorContext<\/code><\/a> \u8fdb\u884c\u4e86\u5927\u91cf\u7684<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/pull\/2622\" target=\"_blank\" rel=\"noopener\">\u6539\u8fdb<\/a>\uff0c\u5c06 Reactor \u7684 <a href=\"https:\/\/projectreactor.io\/docs\/core\/release\/reference\/#context\" target=\"_blank\" rel=\"noopener\">Context<\/a> \u5305\u88c5\u5230 <a href=\"https:\/\/kotlinlang.org\/docs\/coroutine-context-and-dispatchers.html\" target=\"_blank\" rel=\"noopener\">CoroutineContext<\/a> \u4e2d\uff0c\u4ee5\u5b9e\u73b0 Project Reactor \u4e0e Kotlin Coroutines \u7684\u65e0\u7f1d\u96c6\u6210\u3002&nbsp;\u901a\u8fc7\u8fd9\u4e2a\u96c6\u6210\uff0c\u4fbf\u80fd\u4f7f\u7528\u534f\u7a0b\u4f20\u9012 Reactor \u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u6240\u6709\u54cd\u5e94\u5f0f\u96c6\u6210\u7684\u4e0a\u4e0b\u6587\u90fd\u662f\u901a\u8fc7\u8ba2\u9605\u8005\u7684\u4e0a\u4e0b\u6587\u9690\u5f0f\u4f20\u64ad\u7684\uff0c\u4f8b\u5982 <code>Mono<\/code>\uff0c<code>Flux<\/code>\uff0c<code>Publisher.asFlow<\/code>\uff0c<code>Flow.asPublisher<\/code> \u548c <code>Flow.asFlux<\/code>\u3002&nbsp;\u8fd9\u662f\u4e00\u4e2a\u5c06\u8ba2\u9605\u8005\u7684 <code>Context<\/code> \u4f20\u64ad\u5230 <code>ReactorContext<\/code> \u7684\u7b80\u6613\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport kotlinx.coroutines.currentCoroutineContext\nimport kotlinx.coroutines.flow.flow\nimport kotlinx.coroutines.reactor.ReactorContext\nimport kotlinx.coroutines.reactor.asFlux\n\nfun main() {\n    val flow = flow&lt;int&gt; {\n       println(&quot;Reactor context in Flow: &quot; +\n          currentCoroutineContext()[ReactorContext]?.context)\n    }\n\n    \/\/ No context\n    \/\/ prints &quot;Reactor context in Flow: null&quot;\n    flow.asFlux().subscribe() \n\n    \/\/ Add subscriber&#039;s context\n    \/\/ prints &quot;Reactor context in Flow: Context1{answer=42}&quot;\n    flow.asFlux()\n        .contextWrite { ctx -&gt; ctx.put(&quot;answer&quot;, 42) }\n        .subscribe() \n}\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6784\u9020\u4e86\u4e00\u4e2a <code>Flow<\/code> \u5b9e\u4f8b\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u65e0\u4e0a\u4e0b\u6587\u7684 Reactor \u7684 Flux \u5b9e\u4f8b\u3002&nbsp;\u8c03\u7528\u4e0d\u5e26\u53c2\u6570\u7684 <code>subscribe()<\/code>\u65b9\u6cd5\uff0c\u5176\u6548\u679c\u662f\u83b7\u53d6\u53d1\u5e03\u8005\u53d1\u9001\u7684<em>\u6240\u6709<\/em>\u6570\u636e\u3002&nbsp;\u7ed3\u679c\u662f\u7a0b\u5e8f\u5c06\u6253\u5370\u51fa\u201c<em>Reactor context in Flow: null<\/em>\u201d\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684\u94fe\u5f0f\u8c03\u7528\u5c06 <code>Flow<\/code> \u8f6c\u6362\u4e3a <code>Flux<\/code>\uff0c\u968f\u540e\u94fe\u5f0f\u8c03\u7528\u5c06\u4e00\u4e2a\u952e\u503c\u5bf9 <em>answer = 42<\/em> \u6dfb\u52a0\u5230\u4e86 Reactor \u7684\u4e0a\u4e0b\u6587\u4e2d\u3002&nbsp;\u5bf9 <code>subscribe()<\/code> \u7684\u8c03\u7528\u89e6\u53d1\u4e86\u6574\u4e2a\u8c03\u7528\u94fe\u3002&nbsp;\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u4e0a\u4e0b\u6587\u5df2\u6dfb\u52a0\u4e86\u6570\u636e\uff0c\u56e0\u6b64\u7a0b\u5e8f\u5c06\u6253\u5370&#8221;<em>Reactor context in Flow: Context1{answer=42}<\/em>\u201d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u65b0\u7684\u5feb\u6377\u51fd\u6570<\/h2>\n\n\n\n<p>\u5728 Coroutines \u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528\u8bf8\u5982 <code>Mono<\/code> \u4e4b\u7c7b\u7684\u54cd\u5e94\u5f0f\u7c7b\u578b\u65f6\uff0c\u6709\u4e00\u4e9b\u65b9\u4fbf\u7684\u51fd\u6570\u53ef\u4ee5\u5728\u4e0d\u963b\u585e\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u68c0\u7d22\u3002&nbsp;\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u6211\u4eec<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/pull\/2628\" target=\"_blank\" rel=\"noopener\">\u5f03\u7528\u4e86<\/a>\u6240\u6709 <code>Publisher<\/code> \u7684 <code>awaitSingleOr*<\/code> \u51fd\u6570\uff0c\u548c\u4e13\u95e8\u4e3a <code>Mono<\/code> \u548c <code>Maybe<\/code> \u8bbe\u8ba1\u7684 <code>await*<\/code> \u51fd\u6570\u3002<\/p>\n\n\n\n<p><code><code>Mono<\/code> \u6700\u591a\u4ea7\u751f\u4e00\u4e2a\u503c\uff0c\u56e0\u6b64\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4e0e\u7b2c\u4e00\u4e2a\u5143\u7d20\u76f8\u540c\u3002&nbsp;\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5220\u9664\u5269\u4f59\u5143\u7d20\u7684\u8bed\u4e49\u662f\u6ca1\u6709\u7528\u7684\u3002&nbsp;\u56e0\u6b64 <code>Mono.awaitFirst()<\/code> \u548c <code>Mono.awaitLast()<\/code> \u88ab\u5f03\u7528\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f <code>Mono.awaitSingle()<\/code>\u3002<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1600\" height=\"906\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/05\/cor15-reactive-streams-awaitFirst.png\" alt=\"\" class=\"wp-image-142647\"\/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"use-coroutines-1-5-0\">\u5f00\u59cb\u4f7f\u7528 kotlinx.coroutines 1.5!<\/h1>\n\n\n\n<p>\u65b0\u7248\u672c\u6709\u7740\u4ee4\u4eba\u5370\u8c61\u6df1\u523b\u7684\u53d8\u52a8\u5217\u8868\u3002&nbsp;\u5728\u5b8c\u5584 Channels API \u7684\u540c\u65f6\u5f00\u53d1\u65b0\u7684\u547d\u540d\u65b9\u6848\u662f\u56e2\u961f\u7684\u4e00\u9879\u663e\u8457\u6210\u5c31\u3002&nbsp;\u540c\u65f6\u6211\u4eec\u975e\u5e38\u6ce8\u91cd\u8ba9 Coroutines API \u5c3d\u53ef\u80fd\u7b80\u5355\u76f4\u89c2\u3002<\/p>\n\n\n\n<p>\u8981\u5f00\u59cb\u4f7f\u7528 Kotlin Coroutines \u7684\u65b0\u7248\u672c\uff0c\u53ea\u9700\u66f4\u65b0 build.gradle.kts \u6587\u4ef6\u7684\u5185\u5bb9\u3002&nbsp;\u9996\u5148\uff0c\u8bf7\u786e\u4fdd\u60a8\u62e5\u6709\u6700\u65b0\u7248\u672c\u7684 Kotlin Gradle \u63d2\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nplugins {\n   kotlin(&quot;jvm&quot;) version &quot;1.5.0&quot;\n}\n<\/pre>\n\n\n\n<p>\u7136\u540e\u66f4\u65b0\u4f9d\u8d56\u7684\u7248\u672c\uff0c\u5305\u62ec Reactive Streams \u7279\u5b9a\u96c6\u6210\u7684\u5e93\u3002<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nval coroutinesVersion = &quot;1.5.0&quot;\n\ndependencies { \n  implementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:$coroutinesVersion&quot;)\n  implementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$coroutinesVersion&quot;)\n  implementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-reactor:$coroutinesVersion&quot;)\n  implementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-rx3:$coroutinesVersion&quot;)\n  ...\n}\n<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u66f4\u591a\u7684\u89c2\u770b\u53ca\u9605\u8bfb\u6750\u6599<\/strong><\/h1>\n\n\n\n<ul><li><a href=\"https:\/\/youtu.be\/EVLnWOcR0is\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Kotlin Coroutines 1.5.0 \u89c6\u9891<\/a><\/li><li><a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-guide.html\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Coroutines \u6307\u5357<\/a><\/li><li><a href=\"https:\/\/kotlin.github.io\/kotlinx.coroutines\/\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">API \u6587\u6863<\/a><\/li><li><a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Kotlin Coroutines\u2019 GitHub repository<\/a><\/li><li><a href=\"https:\/\/blog.jetbrains.com\/kotlin\/2020\/10\/kotlinx-coroutines-1-4-0-introducing-stateflow-and-sharedflow\/\" class=\"ek-link\">Coroutines 1.4.0 \u535a\u6587<\/a><\/li><li><a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/kotlin\/2021\/05\/kotlin-1-5-0-released\/\" class=\"ek-link\">Kotlin 1.5.0 \u53d1\u5e03\u535a\u6587<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5982\u679c\u60a8\u9047\u5230\u4efb\u4f55\u95ee\u9898<\/strong><\/h3>\n\n\n\n<ul><li><a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/issues\" target=\"_blank\" rel=\"noopener\">GitHub\u95ee\u9898\u8ddf\u8e2a\u5668<\/a>\u62a5\u544a\u95ee\u9898\u3002<\/li><li>\u5728Kotlin Slack\u4e0a\u7684#coroutines\u9891\u9053\u4e2d\u5bfb\u6c42\u5e2e\u52a9(<a href=\"https:\/\/surveys.jetbrains.com\/s3\/kotlin-slack-sign-up\" target=\"_blank\" rel=\"noopener\">\u83b7\u5f97\u9080\u8bf7<\/a>)\u3002<\/li><\/ul>\n\n\n<div style=\"background-color: #f1f6fe; margin-bottom: 2px; padding: 4px; margin-right: 0%; text-align: left; min-height: px;\">\u7279\u522b\u611f\u8c22\u7531\u6765\u81ea Kotlin \u793e\u533a\u7684 <strong>\u9ec4\u667a\u806a (pye52)<\/strong> \u4e3a\u672c\u7bc7\u535a\u6587\u63d0\u4f9b\u4e2d\u6587\u8bd1\u6587\u3002<\/div>","protected":false},"author":814,"featured_media":142991,"comment_status":"closed","ping_status":"closed","template":"","categories":[909],"tags":[671],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/149198"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/kotlin"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/814"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=149198"}],"version-history":[{"count":9,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/149198\/revisions"}],"predecessor-version":[{"id":185960,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/149198\/revisions\/185960"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/142991"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=149198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=149198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=149198"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=149198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}