{"id":151652,"date":"2021-06-08T04:42:42","date_gmt":"2021-06-08T03:42:42","guid":{"rendered":"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/"},"modified":"2021-06-08T04:42:45","modified_gmt":"2021-06-08T03:42:45","slug":"kotlin-coroutines-1-5-0-released-ja","status":"publish","type":"kotlin","link":"https:\/\/blog.jetbrains.com\/ja\/kotlin\/2021\/06\/kotlin-coroutines-1-5-0-released-ja\/","title":{"rendered":"Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3 1.5: GlobalScope \u304c\u30c7\u30ea\u30b1\u30fc\u30c8\u6271\u3044\u306b\u3001\u30c1\u30e3\u30f3\u30cd\u30eb API \u306e\u6539\u5584\u306a\u3069"},"content":{"rendered":"<p>Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3 1.5.0 \u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u307e\u3057\u305f\uff01 \u65b0\u3057\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u306b\u306f\u4ee5\u4e0b\u306e\u5185\u5bb9\u304c\u8ffd\u52a0\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li><a class=\"ek-link\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#globalscope\">GlobalScope \u304c\u30c7\u30ea\u30b1\u30fc\u30c8\uff08Delicate\uff1a\u53d6\u308a\u6271\u3044\u306b\u6ce8\u610f\u304c\u5fc5\u8981\uff09\u306a API \u3068\u3057\u3066\u30de\u30fc\u30af\u3055\u308c\u307e\u3057\u305f\u3002<\/a>GlobalScope \u306f\u8aa4\u7528\u3057\u3084\u3059\u3044\u9ad8\u5ea6\u306a API \u3067\u3059\u3002 \u305d\u306e\u305f\u3081\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u30fc\u306f\u8aa4\u7528\u306e\u53ef\u80fd\u6027\u3092\u8b66\u544a\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u304a\u3051\u308b\u30af\u30e9\u30b9\u306e\u4f7f\u7528\u306b\u5bfe\u3057\u3066\u30aa\u30d7\u30c8\u30a4\u30f3\u3092\u8981\u6c42\u3059\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/li>\n<\/ul>\n<ul>\n<li><a class=\"ek-link\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#junit\">JUnit \u7528\u62e1\u5f35\u6a5f\u80fd\u3002<\/a>JUnit 5 \u3067 CoroutinesTimeout \u304c\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/li>\n<\/ul>\n<ul>\n<li><a class=\"ek-link\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#channels\">Channel API \u306e\u6539\u5584\u3002<\/a>\u30e9\u30a4\u30d6\u30e9\u30ea\u95a2\u6570\u306e\u540d\u524d\u4ed8\u3051\u30b9\u30ad\u30fc\u30e0\u304c\u65b0\u3057\u304f\u306a\u3063\u305f\u306e\u3068\u540c\u6642\u306b\u3001\u975e\u30b5\u30b9\u30da\u30f3\u30c9\u95a2\u6570\u306e <code>trySend<\/code> \u3068 <code>tryReceive<\/code> \u304c <code>offer<\/code> \u3068 <code>poll<\/code> \u306e\u3088\u308a\u512a\u308c\u305f\u4ee3\u66ff\u95a2\u6570\u3068\u3057\u3066\u5c0e\u5165\u3055\u308c\u307e\u3057\u305f\u3002<\/li>\n<\/ul>\n<ul>\n<li><a class=\"ek-link\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#reactive\">Reactive \u7d71\u5408\u306e\u5b89\u5b9a\u5316\u3002<\/a>Reactive Streams \u3068 Korlin Flow \u9593\u3067\u306e\u5909\u63db\u306b\u4f7f\u7528\u3067\u304d\u308b\u95a2\u6570\u3092\u3055\u3089\u306b\u8ffd\u52a0\u3057\u3001\u591a\u6570\u306e\u65e2\u5b58\u306e\u95a2\u6570\u3068 ReactiveContext API \u3092\u5b89\u5b9a\u3055\u305b\u307e\u3057\u305f\u3002<\/li>\n<\/ul>\n<p>\u3053\u306e\u30d6\u30ed\u30b0\u8a18\u4e8b\u3067\u306f\u3001\u65b0\u3057\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u3078\u306e\u79fb\u884c\u306b\u95a2\u3059\u308b\u63a8\u5968\u4e8b\u9805\u3082\u8a18\u8f09\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\" title=\"Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3 1.5: GlobalScope \u306e\u30c7\u30ea\u30b1\u30fc\u30c8\u6307\u5b9a\u3001\u30c1\u30e3\u30f3\u30cd\u30eb API \u306e\u6539\u5584\u306a\u3069\" src=\"https:\/\/www.youtube.com\/embed\/EVLnWOcR0is?feature=oembed\" width=\"500\" height=\"281\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/figure>\n<p align=\"center\"><a class=\"ek-link jb-download-button\" title=\"\u30b3\u30eb\u30fc\u30c1\u30f3 1.5.0 \u3092\u4f7f\u3046\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-coroutines-1-5-0-released\/#use-coroutines-1-5-0\">\u30b3\u30eb\u30fc\u30c1\u30f3 1.5.0 \u3092\u4f7f\u3044\u59cb\u3081\u308b<\/a><\/p>\n<h1 id=\"globalscope\">GlobalScope \u3092\u30c7\u30ea\u30b1\u30fc\u30c8 API \u6271\u3044\u306b<\/h1>\n<p><code>GlobalScope<\/code> \u30af\u30e9\u30b9\u306b <code>@DelicateCoroutinesApi<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u4ed8\u3051\u3089\u308c\u307e\u3057\u305f\u3002 \u4eca\u5f8c <code>GlobalScope<\/code> \u3092\u4f7f\u7528\u3059\u308b\u969b\u306f\u3001<code>@OptIn(DelicateCoroutinesApi::class)<\/code> \u306b\u3088\u308b\u660e\u793a\u7684\u306a\u30aa\u30d7\u30c8\u30a4\u30f3\u304c\u5fc5\u8981\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><code>GlobalScope<\/code> \u306e\u4f7f\u7528\u306f\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u306b\u304a\u3044\u3066\u63a8\u5968\u3055\u308c\u307e\u305b\u3093\u304c\u3001<a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-basics.html#your-first-coroutine\" target=\"_blank\" rel=\"noopener\">\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u3067\u306f\u5f15\u304d\u7d9a\u304d\u30c7\u30ea\u30b1\u30fc\u30c8\uff08Delicate\uff1a\u53d6\u308a\u6271\u3044\u306b\u6ce8\u610f\u304c\u5fc5\u8981\uff09\u306a API \u3092\u4f7f\u7528\u3057\u305f\u6982\u5ff5\u3068\u3057\u3066\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30b0\u30ed\u30fc\u30d0\u30eb <code>CoroutineScope<\/code> \u306f\u3069\u306e\u30b8\u30e7\u30d6\u306b\u3082\u30d0\u30a4\u30f3\u30c9\u3055\u308c\u307e\u305b\u3093\u3002 \u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u306f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5bff\u547d\u3092\u901a\u3058\u3066\u52d5\u4f5c\u3059\u308b\u6700\u4e0a\u4f4d\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u8d77\u52d5\u3059\u308b\u306b\u306f\u4f7f\u7528\u3055\u308c\u3001\u9014\u4e2d\u3067\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u307e\u305b\u3093\u3002 <code>GlobalScope<\/code> \u3067\u8d77\u52d5\u3055\u308c\u305f\u30a2\u30af\u30c6\u30a3\u30d6\u306a\u30b3\u30eb\u30fc\u30c1\u30f3\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u3092 alive \u306b\u7dad\u6301\u3059\u308b\u3082\u306e\u3067\u306f\u306a\u304f\u3001 \u30c7\u30fc\u30e2\u30f3\u30b9\u30ec\u30c3\u30c9\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3057\u307e\u3059\u3002<\/p>\n<p><code>GlobalScope<\/code> \u306f\u3001\u4f7f\u7528\u3059\u308b\u3068\u8aa4\u3063\u3066\u30ea\u30bd\u30fc\u30b9\u30ea\u30fc\u30af\u3084\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3092\u767a\u751f\u3055\u305b\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3001\u53d6\u308a\u6271\u3044\u306b\u6ce8\u610f\u306a API \u3067\u3059\u3002 <code>GlobalScope<\/code> \u3067\u8d77\u52d5\u3057\u305f\u30b3\u30eb\u30fc\u30c1\u30f3\u306f\u69cb\u9020\u5316\u3055\u308c\u305f\u4e26\u884c\u6027\u306e\u539f\u5247\u306e\u5bfe\u8c61\u5916\u3067\u3042\u308b\u305f\u3081\u3001\uff08\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304c\u9045\u3044\u306a\u3069\u306e\uff09\u554f\u984c\u306b\u3088\u3063\u3066\u30cf\u30f3\u30b0\u3057\u305f\u308a\u9045\u5ef6\u304c\u767a\u751f\u3057\u305f\u308a\u3059\u308b\u3068\u3001\u305d\u306e\u307e\u307e\u52d5\u4f5c\u3057\u3001\u30ea\u30bd\u30fc\u30b9\u3092\u6d88\u8cbb\u3057\u7d9a\u3051\u3066\u3057\u307e\u3044\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u6b21\u306e\u30b3\u30fc\u30c9\u3092\u8003\u5bdf\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">fun loadConfiguration() {\n    GlobalScope.launch {\n        val config = fetchConfigFromServer() \/\/ network request\n        updateConfiguration(config)\n    }\n}\n<\/pre>\n<p><code>loadConfiguration<\/code> \u306e\u547c\u3073\u51fa\u3057\u306b\u3088\u3063\u3066 <code>GlobalScope<\/code> \u3067\u30b3\u30eb\u30fc\u30c1\u30f3\u304c\u4f5c\u6210\u3055\u308c\u3001\u305d\u308c\u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3057\u305f\u308a\u5b8c\u4e86\u3092\u5f85\u6a5f\u3057\u305f\u308a\u3059\u308b\u898f\u5b9a\u3082\u306a\u304f\u3001\u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9\u3067\u52d5\u4f5c\u3057\u3066\u3044\u307e\u3059\u3002 \u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306e\u901f\u5ea6\u304c\u4f4e\u4e0b\u3059\u308b\u3068\u3001\u30b3\u30eb\u30fc\u30c1\u30f3\u306f\u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9\u3067\u5f85\u6a5f\u3057\u3001\u30ea\u30bd\u30fc\u30b9\u3092\u6d88\u8cbb\u3057\u7d9a\u3051\u3066\u3057\u307e\u3044\u307e\u3059\u3002 <code>loadConfiguration<\/code> \u3092\u7e70\u308a\u8fd4\u3057\u3066\u547c\u3073\u51fa\u305b\u3070\u3001\u6d88\u8cbb\u3055\u308c\u308b\u30ea\u30bd\u30fc\u30b9\u304c\u3055\u3089\u306b\u5897\u52a0\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<h2>\u8003\u3048\u3089\u308c\u308b\u4ee3\u66ff\u624b\u6cd5<\/h2>\n<p>\u591a\u304f\u306e\u5834\u5408\u3001<code>GlobalScope<\/code> \u306e\u4f7f\u7528\u306f\u907f\u3051\u308b\u3079\u304d\u3067\u3042\u308a\u3001\u6b21\u306e\u4f8b\u306e\u3088\u3046\u306b\u3001\u305d\u308c\u3092\u542b\u3080\u64cd\u4f5c\u3092 <code>suspend<\/code> \u3067\u30de\u30fc\u30af\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">suspend fun loadConfiguration() {\n    val config = fetchConfigFromServer() \/\/ network request\n    updateConfiguration(config)\n}\n<\/pre>\n<p><code>GlobalScope.launch<\/code> \u3092\u4f7f\u7528\u3057\u3066\u8907\u6570\u306e\u4e26\u884c\u51e6\u7406\u304c\u8d77\u52d5\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306b\u306f\u3001\u5bfe\u5fdc\u3059\u308b\u64cd\u4f5c\u306f\u4ee3\u308f\u308a\u306b <code>coroutineScope<\/code> \u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">\/\/ concurrently load configuration and data\nsuspend fun loadConfigurationAndData() {\n    coroutineScope {\n        launch { loadConfiguration() }\n        launch { loadData() }\n    }\n}\n<\/pre>\n<p>\u6700\u4e0a\u4f4d\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u4e26\u884c\u51e6\u7406\u304c\u975e\u30b5\u30b9\u30da\u30f3\u30c9\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u304b\u3089\u8d77\u52d5\u3055\u308c\u308b\u969b\u306b\u306f\u3001<code>GlobalScope<\/code> \u306e\u4ee3\u308f\u308a\u306b\u3001\u9069\u5207\u306b\u9650\u5b9a\u3055\u308c\u305f <code>CoroutineScope<\/code> \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>\u6b63\u5f53\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9<\/h2>\n<p><code>GlobalScope<\/code> \u304c\u5408\u7406\u7684\u304b\u3064\u5b89\u5168\u306b\u4f7f\u7528\u3055\u308c\u308b\u72b6\u6cc1\u306f\u9650\u5b9a\u7684\u3067\u3059\u3002\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5bff\u547d\u671f\u9593\u4e2d\u306b\u30a2\u30af\u30c6\u30a3\u30d6\u306a\u72b6\u614b\u3092\u7dad\u6301\u3059\u308b\u5fc5\u8981\u306e\u3042\u308b\u6700\u4e0a\u4f4d\u306e\u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9\u30d7\u30ed\u30bb\u30b9\u306a\u3069\u3067\u3059\u3002 \u305d\u306e\u305f\u3081\u3001<code>GlobalScope<\/code> \u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b <code>@OptIn(DelicateCoroutinesApi::class)<\/code> \u3092\u4f7f\u3063\u3066\u660e\u793a\u7684\u306b\u30aa\u30d7\u30c8\u30a4\u30f3\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">\/\/ 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<p><code>GlobalScope<\/code> \u306e\u4f7f\u7528\u7b87\u6240\u3092\u3088\u304f\u30ec\u30d3\u30e5\u30fc\u3057\u3001\u300c\u6b63\u5f53\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u300d\u306b\u8a72\u5f53\u3059\u308b\u3082\u306e\u306e\u307f\u306b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4ed8\u3051\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002 \u307b\u304b\u306e\u4f7f\u7528\u72b6\u6cc1\u306b\u304a\u3044\u3066\u306f\u3001\u30b3\u30fc\u30c9\u306e\u30d0\u30b0\u306e\u539f\u56e0\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u3088\u3046\u306a <code>GlobalScope<\/code> \u306b\u3064\u3044\u3066\u306f\u524d\u8ff0\u306e\u65b9\u6cd5\u3067\u66f8\u304d\u63db\u3048\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1 id=\"junit\">JUnit 5 \u7528\u306e\u62e1\u5f35\u6a5f\u80fd<\/h1>\n<p>\u30c6\u30b9\u30c8\u3092\u5225\u306e\u30b9\u30ec\u30c3\u30c9\u3067\u5b9f\u884c\u3057\u3001\u6307\u5b9a\u306e\u6642\u9593\u5236\u9650\u3092\u8d85\u3048\u308b\u3068\u5931\u6557\u306b\u3057\u3066\u30b9\u30ec\u30c3\u30c9\u3092\u4e2d\u65ad\u3055\u305b\u308b\u3053\u3068\u306e\u3067\u304d\u308b <code>CoroutinesTimeout<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002 \u4ee5\u524d\u306e <code>CoroutinesTimeout<\/code> \u306f JUnit 4 \u5411\u3051\u306b\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3057\u305f\u304c\u3001 \u4eca\u56de\u306e\u30ea\u30ea\u30fc\u30b9\u3067\u306f\u3001JUnit 5 \u3068\u306e\u7d71\u5408\u3092\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u65b0\u3057\u3044\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001\u6b21\u306e\u4f9d\u5b58\u95a2\u4fc2\u3092\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">dependencies {\n  \u2026\n  testImplementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutinesVersion\")\n}\n<\/pre>\n<p>\u4ee5\u4e0b\u306b\u3001\u30c6\u30b9\u30c8\u3067\u65b0\u3057\u3044 <code>CoroutinesTimeout<\/code> \u3092\u4f7f\u7528\u3059\u308b\u65b9\u6cd5\u306e\u4f8b\u3092\u7c21\u5358\u306b\u793a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">import 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<p>\u3053\u306e\u4f8b\u3067\u306f\u3001\u30b3\u30eb\u30fc\u30c1\u30f3\u306e\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u306f\u30af\u30e9\u30b9\u30ec\u30d9\u30eb\u3067 <code>firstTest<\/code> \u3092\u6307\u5b9a\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3059\u3002 \u95a2\u6570\u306e\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u30af\u30e9\u30b9\u30ec\u30d9\u30eb\u306e\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3059\u308b\u305f\u3081\u3001\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u4ed8\u304d\u306e\u30c6\u30b9\u30c8\u306f\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u3057\u307e\u305b\u3093\u3002 <code>secondTest<\/code> \u306f\u30af\u30e9\u30b9\u30ec\u30d9\u30eb\u306e\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u3057\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u3057\u3066\u5ba3\u8a00\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">package kotlinx.coroutines.debug.junit5\n\npublic annotation class CoroutinesTimeout(\n    val testTimeoutMs: Long,\n    val cancelOnTimeout: Boolean = false\n)\n<\/pre>\n<p>\u6700\u521d\u306e <code>testTimeoutMs<\/code> \u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306f\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u6642\u9593\u3092\u30df\u30ea\u79d2\u3067\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002 2 \u3064\u76ee\u306e <code>cancelOnTimeout<\/code> \u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306f\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u6642\u306b\u5b9f\u884c\u4e2d\u306e\u3059\u3079\u3066\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u30ad\u30e3\u30f3\u30bb\u30eb\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3057\u307e\u3059\u3002 <code>true<\/code> \u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u3059\u3079\u3066\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u306f\u81ea\u52d5\u7684\u306b\u30ad\u30e3\u30f3\u30bb\u30eb\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><code>CoroutinesTimeout<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3059\u308b\u969b\u306f\u3001<a href=\"https:\/\/blog.jetbrains.com\/kotlin\/2020\/07\/kotlin-1-4-rc-debugging-coroutines\/\">\u30b3\u30eb\u30fc\u30c1\u30f3\u30c7\u30d0\u30c3\u30ac\u30fc<\/a>\u304c\u5fc5\u305a\u81ea\u52d5\u7684\u306b\u6709\u52b9\u306b\u306a\u308a\u3001\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u6642\u306e\u3059\u3079\u3066\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u30c0\u30f3\u30d7\u3057\u307e\u3059\u3002 \u30c0\u30f3\u30d7\u306b\u306f\u30b3\u30eb\u30fc\u30c1\u30f3\u4f5c\u6210\u306e\u30b9\u30bf\u30c3\u30af\u30c8\u30ec\u30fc\u30b9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002 \u30c6\u30b9\u30c8\u3092\u9ad8\u901f\u5316\u3059\u308b\u305f\u3081\u306b\u4f5c\u6210\u306e\u30b9\u30bf\u30c3\u30af\u30c8\u30ec\u30fc\u30b9\u3092\u7121\u52b9\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u3053\u306e\u69cb\u6210\u3092\u884c\u3048\u308b <code><a class=\"ek-link\" href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/blob\/master\/kotlinx-coroutines-debug\/src\/junit\/junit5\/CoroutinesTimeoutExtension.kt\" target=\"_blank\" rel=\"noopener\">CoroutinesTimeoutExtension<\/a><\/code> \u3092\u76f4\u63a5\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><a class=\"ek-link\" href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/issues\/2197\" target=\"_blank\" rel=\"noopener\">JUnit 5 \u7528\u306e CoroutinesTimeout<\/a> \u306b\u95a2\u3059\u308b\u6709\u76ca\u306a PoC \u3092\u4f5c\u6210\u3057\u3066\u304f\u308c\u305f <a href=\"https:\/\/github.com\/asarkar\" target=\"_blank\" rel=\"noopener\">Abhijit Sarkar<\/a> \u306b\u611f\u8b1d\u3044\u305f\u3057\u307e\u3059\u3002 \u3053\u306e\u30a2\u30a4\u30c7\u30a2\u3092\u57fa\u306b\u30011.5 \u30ea\u30ea\u30fc\u30b9\u306b\u8ffd\u52a0\u3057\u305f\u65b0\u3057\u3044 <code>CoroutinesTimeout<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u4f5c\u6210\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<h1 id=\"channels\">Channel API \u306e\u6539\u5584<\/h1>\n<p>Channel \u306f\u91cd\u8981\u306a\u30b3\u30df\u30e5\u30cb\u30b1\u30fc\u30b7\u30e7\u30f3\u30d7\u30ea\u30df\u30c6\u30a3\u30d6\u3067\u3001\u3055\u307e\u3056\u307e\u306a\u30b3\u30eb\u30fc\u30c1\u30f3\u3068\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u9593\u3067\u30c7\u30fc\u30bf\u3092\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u3053\u306e\u30ea\u30ea\u30fc\u30b9\u3067\u306f\u3001Channel API \u306e\u6a5f\u80fd\u3092\u5c11\u3057\u6539\u826f\u3057\u3001\u56f0\u60d1\u3092\u62db\u3044\u3066\u3044\u305f <code>offer<\/code> \u3068 <code>poll<\/code> \u95a2\u6570\u3092\u3088\u308a\u512a\u308c\u305f\u4ee3\u66ff\u95a2\u6570\u306b\u7f6e\u304d\u63db\u3048\u307e\u3057\u305f\u3002 \u540c\u6642\u306b\u3001\u30b5\u30b9\u30da\u30f3\u30c9\u30e1\u30bd\u30c3\u30c9\u3068\u975e\u30b5\u30b9\u30da\u30f3\u30c9\u30e1\u30bd\u30c3\u30c9\u5411\u3051\u306e\u4e00\u8cab\u3057\u305f\u65b0\u3057\u3044\u540d\u524d\u4ed8\u3051\u30b9\u30ad\u30fc\u30e0\u3082\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<\/p>\n<h2>\u65b0\u3057\u3044\u540d\u524d\u4ed8\u3051\u30b9\u30ad\u30fc\u30e0<\/h2>\n<p>\u540d\u524d\u4ed8\u3051\u30b9\u30ad\u30fc\u30e0\u306b\u4e00\u8cab\u6027\u3092\u6301\u305f\u305b\u308b\u3088\u3046\u306b\u6539\u5584\u3057\u3001\u307b\u304b\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3084\u30b3\u30eb\u30fc\u30c1\u30f3 API \u3067\u3082\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002 \u95a2\u6570\u306e\u540d\u524d\u304b\u3089\u632f\u308b\u821e\u3044\u304c\u308f\u304b\u308b\u3088\u3046\u306b\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002 \u305d\u306e\u305f\u3081\u3001\u65b0\u3057\u3044\u30b9\u30ad\u30fc\u30e0\u3067\u306f\u6b21\u306e\u3088\u3046\u306a\u53d6\u308a\u6c7a\u3081\u304c\u306a\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u901a\u5e38\u306e\u30b5\u30b9\u30da\u30f3\u30c9\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u306f\u3001<code>send<\/code> \u3084 <code>receive<\/code> \u306a\u3069\u306e\u3088\u3046\u306b\u305d\u306e\u307e\u307e\u3067\u3059\u3002<\/li>\n<li>\u30a8\u30e9\u30fc\u3092\u30ab\u30d7\u30bb\u30eb\u5316\u3057\u305f\u975e\u30b5\u30b9\u30da\u30f3\u30c9\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u306f\u3001\u4ee5\u524d\u306e <code>offer<\/code> \u3084 <code>poll<\/code> \u3067\u306f\u306a\u304f\u3001<code>trySend<\/code> \u3084 <code>tryReceive<\/code> \u306e\u3088\u3046\u306b\u63a5\u982d\u8f9e\u3092\u300ctry\u300d\u3067\u4e00\u8cab\u3055\u305b\u3066\u3044\u307e\u3059\u3002<\/li>\n<li>\u30a8\u30e9\u30fc\u3092\u30ab\u30d7\u30bb\u30eb\u5316\u3059\u308b\u65b0\u3057\u3044\u30b5\u30b9\u30da\u30f3\u30c9\u30e1\u30bd\u30c3\u30c9\u306b\u306f\u3001\u300cCatching\u300d\u3068\u3044\u3046\u63a5\u5c3e\u8f9e\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<p>\u3053\u308c\u3089\u306e\u65b0\u3057\u3044\u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>Try \u95a2\u6570: send \u3068 receive \u306b\u5bfe\u5fdc\u3059\u308b\u975e\u30b5\u30b9\u30da\u30f3\u30c9\u95a2\u6570<\/h2>\n<p>1 \u3064\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u306f\u30c1\u30e3\u30f3\u30cd\u30eb\u306b\u60c5\u5831\u3092\u9001\u4fe1\u3057\u3001\u3082\u3046 1 \u3064\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u306f\u305d\u306e\u60c5\u5831\u3092\u53d7\u4fe1\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 <code>send<\/code> \u3068 <code>receive<\/code> \u306f\u3068\u3082\u306b\u30b5\u30b9\u30da\u30f3\u30c9\u95a2\u6570\u3067\u3059\u3002 <code>send<\/code> \u306f\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u3044\u3063\u3071\u3044\u3067\u3042\u308a\u3001\u65b0\u3057\u3044\u8981\u7d20\u3092\u53d7\u3051\u5165\u308c\u3089\u308c\u306a\u3044\u5834\u5408\u306b\u3001\u305d\u306e\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u4e00\u6642\u7684\u306b\u4e2d\u65ad\u3057\u3001<code>receive<\/code> \u306f\u8fd4\u3059\u8981\u7d20\u304c\u30c1\u30e3\u30f3\u30cd\u30eb\u306b\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306b\u3001\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u4e00\u6642\u7684\u306b\u4e2d\u65ad\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\">import 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(\"Sending...\")\n        channel.send(\"Element\")\n     }\n     \/\/ Suspends until the element can be received\n     println(\"Receiving...\")\n     println(channel.receive())\n}\n<\/pre>\n<p>\u3053\u308c\u3089\u306e\u95a2\u6570\u306b\u306f\u540c\u671f\u30b3\u30fc\u30c9\u3067\u3053\u308c\u3089\u3068\u5bfe\u7167\u3057\u3066\u4f7f\u7528\u3059\u308b\u3001<code>offer<\/code> \u3068 <code>poll<\/code> \u3068\u3044\u3046\u975e\u30b5\u30b9\u30da\u30f3\u30c9\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u3053\u308c\u3089\u306f\u975e\u63a8\u5968\u3068\u306a\u308a\u3001<code>trySend<\/code> \u3068 <code>tryReceive<\/code> \u306b\u7f6e\u304d\u63db\u3048\u3089\u308c\u3066\u3044\u307e\u3059\u3002 \u3053\u306e\u3088\u3046\u306b\u5909\u66f4\u3055\u308c\u305f\u7406\u7531\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><code>offer<\/code> \u3068 <code>poll<\/code> \u306f <code>send<\/code> \u3068 <code>receive<\/code> \u3068\u540c\u3058\u3053\u3068\u3092\u5b9f\u884c\u3057\u307e\u3059\u304c\u3001\u4e00\u6642\u7684\u306b\u4e2d\u65ad\u3055\u305b\u308b\u6a5f\u80fd\u306f\u3042\u308a\u307e\u305b\u3093\u3002 \u305d\u3046\u8a00\u3048\u3070\u3001\u5358\u7d14\u3067\u3042\u308a\u3001\u8981\u7d20\u304c\u9001\u4fe1\u307e\u305f\u306f\u53d7\u4fe1\u3055\u308c\u308b\u969b\u306b\u3059\u3079\u3066\u306f\u6b63\u3057\u304f\u52d5\u4f5c\u3059\u308b\u3088\u3046\u306b\u805e\u3053\u3048\u307e\u3059\u304c\u3001 \u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u306b\u306f\u3001\u3069\u3046\u306a\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002 <code>send<\/code> \u3068 <code>receive<\/code> \u306f\u3001\u30b8\u30e7\u30d6\u3092\u5b9f\u884c\u3067\u304d\u308b\u72b6\u614b\u306b\u306a\u308b\u307e\u3067\u4e00\u6642\u7684\u306b\u4e2d\u65ad\u3057\u307e\u3059\u304c\u3001 <code>offer<\/code> \u3068 <code>poll<\/code> \u306b\u3064\u3044\u3066\u306f\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u3044\u3063\u3071\u3044\u3067\u3042\u308b\u305f\u3081\u306b\u8981\u7d20\u304c\u8ffd\u52a0\u3055\u308c\u306a\u3044\u5834\u5408\u3084\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u7a7a\u3067\u3042\u308b\u305f\u3081\u306b\u53d6\u5f97\u3067\u304d\u308b\u8981\u7d20\u304c\u306a\u3044\u5834\u5408\u306b\u306f\u3001\u305d\u308c\u305e\u308c <code>false<\/code> \u3068 <code>null<\/code> \u3092\u8fd4\u3059\u3053\u3068\u3057\u304b\u3067\u304d\u307e\u305b\u3093\u3002 \u30af\u30ed\u30fc\u30ba\u3055\u308c\u305f\u30c1\u30e3\u30f3\u30cd\u30eb\u3068\u901a\u4fe1\u3057\u3088\u3046\u3068\u3059\u308c\u3070\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\u3057\u3001\u3053\u306e\u3053\u3068\u304c\u6df7\u4e71\u3092\u62db\u3044\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\">import 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(\"Sending...\")\n        \/\/ Doesn't suspend\n        \/\/ Returns 'false' if the channel is full\n        \/\/ Or throws an exception if it's closed\n        channel.offer(\"Element\")\n    }\n    println(\"Receiving...\")\n    \/\/ Doesn't suspend\n    \/\/ Returns 'null' if the channel is empty\n    println(channel.poll())\n\/\/  channel.close()\n}\n<\/pre>\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001<code>poll<\/code> \u306f\u8981\u7d20\u304c\u8ffd\u52a0\u3055\u308c\u308b\u524d\u306b\u547c\u3073\u51fa\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3059\u3050\u306b <code>null<\/code> \u3092\u8fd4\u3057\u3066\u3044\u307e\u3059\u3002 \u3053\u308c\u306f\u60f3\u5b9a\u3055\u308c\u308b\u4f7f\u7528\u65b9\u6cd5\u3067\u306f\u306a\u3044\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u8981\u7d20\u306e\u30dd\u30fc\u30ea\u30f3\u30b0\u3092\u901a\u5e38\u901a\u308a\u7d9a\u3051\u308b\u3068\u3053\u308d\u3067\u3059\u304c\u3001\u3053\u306e\u8aac\u660e\u3092\u5358\u7d14\u5316\u3059\u308b\u305f\u3081\u306b\u76f4\u63a5\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\u3002 <code>offer<\/code> \u306e\u547c\u3073\u51fa\u3057\u306b\u3064\u3044\u3066\u3082\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u30e9\u30f3\u30c7\u30d6\u30fc\u30e2\u30fc\u30c9\u3067\u3042\u308a\u3001\u30d0\u30c3\u30d5\u30a1\u5bb9\u91cf\u304c\u30bc\u30ed\u3067\u3042\u308b\u305f\u3081\u5931\u6557\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002 \u305d\u306e\u305f\u3081\u3001<code>offer<\/code> \u306f <code>false<\/code> \u3092\u3001<code>poll<\/code> \u306f <code>null<\/code> \u3092\u8fd4\u3057\u3066\u3057\u307e\u3046\u306e\u3067\u3059\u3002\u5358\u306a\u308b\u547c\u3073\u51fa\u3057\u9806\u5e8f\u306e\u8aa4\u308a\u304c\u539f\u56e0\u3067\u3059\u3002<\/p>\n<p>\u4e0a\u306e\u4f8b\u306e <code>channel.close()<\/code> \u30b9\u30c6\u30fc\u30c8\u30e1\u30f3\u30c8\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u89e3\u9664\u3057\u3001\u4f8b\u5916\u304c\u30b9\u30ed\u30fc\u3055\u308c\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002 \u3053\u306e\u5834\u5408\u3001<code>poll<\/code> \u306f\u524d\u3068\u540c\u69d8\u306b <code>false<\/code> \u3092\u8fd4\u3057\u307e\u3059\u304c\u3001 <code>offer<\/code> \u306b\u3064\u3044\u3066\u306f\u3059\u3067\u306b\u30af\u30ed\u30fc\u30ba\u3068\u306a\u3063\u305f\u30c1\u30e3\u30f3\u30cd\u30eb\u306b\u8981\u7d20\u3092\u8ffd\u52a0\u3057\u3088\u3046\u3068\u3057\u3001\u5931\u6557\u3057\u3066\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\u3057\u3066\u3044\u307e\u3059\u3002 \u3053\u306e\u3088\u3046\u306a\u52d5\u4f5c\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308a\u304c\u3061\u3060\u3068\u3044\u3046\u4e0d\u6e80\u3092\u305f\u304f\u3055\u3093\u8033\u306b\u3057\u307e\u3057\u305f\u3002 \u3053\u306e\u4f8b\u5916\u3092\u30ad\u30e3\u30c3\u30c1\u3059\u308b\u3053\u3068\u306f\u5fd8\u308c\u3084\u3059\u304f\u3001\u4ee3\u308f\u308a\u306b\u7121\u8996\u3057\u305f\u308a\u5225\u306e\u65b9\u6cd5\u3067\u51e6\u7406\u3057\u305f\u308a\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u30af\u30e9\u30c3\u30b7\u30e5\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u8ab2\u984c\u3092\u4fee\u6b63\u3057\u3066\u3088\u308a\u8a73\u7d30\u306a\u7d50\u679c\u3092\u8fd4\u3059\u305f\u3081\u306b\u3001\u65b0\u3057\u3044 <code>trySend<\/code> \u3068 <code>tryReceive<\/code> \u304c\u5c0e\u5165\u3055\u308c\u307e\u3057\u305f\u3002 \u305d\u308c\u305e\u308c <code>ChannelResult<\/code> \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u8fd4\u3057\u3001\u3053\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304b\u3089\u3001\u6210\u529f\u3001\u5931\u6557\u3001\u307e\u305f\u306f\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u30af\u30ed\u30fc\u30ba\u3057\u3066\u3044\u308b\u3053\u3068\u3092\u793a\u3059\u5185\u5bb9\u3092\u5f97\u3089\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\">import 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(\"Sending...\")\n        \/\/ Doesn't suspend\n        \/\/ Returns 'Failed' if the channel is full\n        \/\/ Or 'Channel was closed' result if it's closed\n        val result = channel.trySend(\"Element\")\n        println(result)\n\n        \/\/We can verify the result\n        if(result.isClosed){\n            println(\"Sending failed. The channel is closed.\")\n        }\n    }\n    println(\"Receiving...\")\n    println(channel.tryReceive())\n\/\/  channel.close()\n}\n<\/pre>\n<p>\u3053\u306e\u4f8b\u306f\u524d\u306e\u4f8b\u3068\u540c\u3058\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u304c\u3001<code>tryReceive<\/code> \u3068 <code>trySend<\/code> \u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u8a73\u3057\u3044\u7d50\u679c\u304c\u8fd4\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002 \u51fa\u529b\u304c\u3001<code>false<\/code> \u3068 <code>null<\/code> \u306e\u4ee3\u308f\u308a\u306b <code>Value(Failed)<\/code> \u3068\u306a\u3063\u3066\u3044\u308b\u306e\u304c\u308f\u304b\u308b\u3067\u3057\u3087\u3046\u3002 \u30c1\u30e3\u30f3\u30cd\u30eb\u3092\u30af\u30ed\u30fc\u30ba\u3057\u3066\u3044\u308b\u884c\u306e\u30b3\u30e1\u30f3\u30c8\u3092\u3082\u3046\u4e00\u5ea6\u89e3\u9664\u3057\u3001<code>trySend<\/code> \u304c\u4f8b\u5916\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u3066 <code>Closed<\/code> \u306e\u7d50\u679c\u3092\u8fd4\u3059\u3088\u3046\u306b\u306a\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><a href=\"https:\/\/kotlinlang.org\/docs\/inline-classes.html\" target=\"_blank\" rel=\"noopener\">inline value \u30af\u30e9\u30b9<\/a>\u306b\u3088\u308a\u3001<code>ChannelResult<\/code> \u3092\u4f7f\u7528\u3057\u3066\u3082\u8ffd\u52a0\u306e\u30e9\u30c3\u30d1\u30fc\u304c\u4f5c\u6210\u3055\u308c\u308b\u3053\u3068\u304c\u306a\u304f\u3001successful \u5024\u304c\u8fd4\u3055\u308c\u308b\u5834\u5408\u3067\u3082\u3001\u30aa\u30fc\u30d0\u30fc\u30d8\u30c3\u30c9\u3092\u4f34\u3046\u3053\u3068\u306a\u304f\u305d\u306e\u307e\u307e\u8fd4\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>Catching \u95a2\u6570: \u30a8\u30e9\u30fc\u3092\u30ab\u30d7\u30bb\u30eb\u5316\u3059\u308b\u30b5\u30b9\u30da\u30f3\u30c9\u95a2\u6570<\/h2>\n<p>\u3053\u306e\u30ea\u30ea\u30fc\u30b9\u3088\u308a\u3001\u30a8\u30e9\u30fc\u3092\u30ab\u30d7\u30bb\u30eb\u5316\u3059\u308b\u30b5\u30b9\u30da\u30f3\u30c9\u30e1\u30bd\u30c3\u30c9\u306b\u306f\u3001\u300cCatching\u300d\u3068\u3044\u3046\u63a5\u5c3e\u8f9e\u304c\u4f7f\u7528\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002 \u305f\u3068\u3048\u3070\u3001\u65b0\u3057\u3044 <code>receiveCatching<\/code> \u95a2\u6570\u306f\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u30af\u30ed\u30fc\u30ba\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u306e\u4f8b\u5916\u3092\u51e6\u7406\u3057\u307e\u3059\u3002 \u4ee5\u4e0b\u306e\u7c21\u5358\u306a\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\">import 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<p>\u30c1\u30e3\u30f3\u30cd\u30eb\u306f\u3001\u5024\u3092\u53d6\u5f97\u3057\u3088\u3046\u3068\u3059\u308b\u524d\u306b\u30af\u30ed\u30fc\u30ba\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002 \u305f\u3060\u3057\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u30af\u30ed\u30fc\u30ba\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u3001\u6210\u529f\u3068\u3057\u3066\u5b8c\u4e86\u3057\u307e\u3059\u3002 <code>receiveCatching<\/code> \u3092\u901a\u5e38\u306e <code>receive<\/code> \u95a2\u6570\u306b\u7f6e\u304d\u63db\u3048\u308b\u3068\u3001<code>ClosedReceiveChannelException<\/code> \u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\">import 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<p>\u73fe\u6642\u70b9\u3067\u306f\u3001<code>receiveCatching<\/code> \u3068 <code>onReceiveCatching<\/code>\uff08\u4ee5\u524d\u306e\u5185\u90e8\u306e <code>receiveOrClosed<\/code> \u306e\u4ee3\u66ff\uff09\u3057\u304b\u63d0\u4f9b\u3057\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u4eca\u5f8c\u3055\u3089\u306b\u95a2\u6570\u3092\u8ffd\u52a0\u3059\u308b\u4e88\u5b9a\u3067\u3059\u3002<\/p>\n<h2>\u65b0\u3057\u3044\u95a2\u6570\u306b\u30b3\u30fc\u30c9\u3092\u79fb\u884c\u3059\u308b<\/h2>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u5185\u3067 <code>offer<\/code> \u3068 <code>poll<\/code> \u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u7b87\u6240\u3092\u81ea\u52d5\u7684\u306b\u65b0\u3057\u3044\u547c\u3073\u51fa\u3057\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 <code>offer<\/code> \u306f <code>Boolean<\/code> \u3092\u8fd4\u3059\u305f\u3081\u3001\u305d\u308c\u306b\u76f8\u5f53\u3059\u308b\u30b3\u30fc\u30c9\u3068\u3057\u3066 <code>channel.trySend(\"Element\").isSuccess<\/code> \u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-142625\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/05\/cor15-offer-deprecated.png\" alt=\"\" width=\"1600\" height=\"800\" \/><\/figure>\n<p>\u540c\u69d8\u306b\u3001<code>poll<\/code> \u95a2\u6570\u3082 null \u8a31\u5bb9\u8981\u7d20\u3092\u8fd4\u3059\u305f\u3081\u3001<code>channel.tryReceive().getOrNull()<\/code> \u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-142636\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/05\/cor15-poll-deprecated.png\" alt=\"\" width=\"1440\" height=\"440\" \/><\/figure>\n<p>\u547c\u3073\u51fa\u3057\u306e\u7d50\u679c\u304c\u4f7f\u7528\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001\u76f4\u63a5\u65b0\u3057\u3044\u547c\u3073\u51fa\u3057\u306b\u7f6e\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u5916\u51e6\u7406\u306e\u52d5\u4f5c\u306f\u7570\u306a\u3063\u3066\u3057\u307e\u3046\u305f\u3081\u3001\u3053\u308c\u306b\u3064\u3044\u3066\u306f\u624b\u4f5c\u696d\u3067\u5fc5\u8981\u306a\u66f4\u65b0\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 \u300coffer\u300d\u3068\u300cpoll\u300d\u3067\u30af\u30ed\u30fc\u30ba\u3057\u305f\u30c1\u30e3\u30f3\u30cd\u30eb\u306b\u5bfe\u3059\u308b\u4f8b\u5916\u306e\u30b9\u30ed\u30fc\u3092\u884c\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u7f6e\u304d\u63db\u3048\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p><code>channel.offer(\"Element\")<\/code> \u306b\u76f8\u5f53\u3059\u308b\u65b0\u3057\u3044\u30b3\u30fc\u30c9\u306f\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u30af\u30ed\u30fc\u30ba\u3067\u3042\u308b\u5834\u5408\u306b\u3001\u901a\u5e38\u901a\u308a\u306b\u30af\u30ed\u30fc\u30ba\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u3067\u3042\u3063\u3066\u3082\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">channel\n  .trySend(\"Element\")\n  .onClosed { throw it ?: ClosedSendChannelException(\"Channel was closed\") }\n  .isSuccess\n<\/pre>\n<p><code>channel.poll()<\/code> \u306b\u76f8\u5f53\u3059\u308b\u65b0\u3057\u3044\u30b3\u30fc\u30c9\u306f\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u304c\u30a8\u30e9\u30fc\u3067\u30af\u30ed\u30fc\u30ba\u3055\u308c\u305f\u5834\u5408\u306b\u306f\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\u3057\u3001\u901a\u5e38\u901a\u308a\u30af\u30ed\u30fc\u30ba\u3055\u308c\u305f\u5834\u5408\u306b\u306f <code>null<\/code> \u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">channel.tryReceive()\n  .onClosed { if (it != null) throw it }\n  .getOrNull()\n<\/pre>\n<p>\u3053\u306e\u5909\u66f4\u306f\u3001\u4ee5\u524d\u306e <code>offer<\/code> \u3068 <code>poll<\/code> \u95a2\u6570\u306e\u52d5\u4f5c\u3092\u53cd\u6620\u3057\u305f\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u307b\u3068\u3093\u3069\u306e\u5834\u5408\u306b\u304a\u3044\u3066\u3001\u30af\u30ed\u30fc\u30ba\u3055\u308c\u305f\u30c1\u30e3\u30f3\u30cd\u30eb\u306b\u5bfe\u51e6\u3059\u308b\u30b3\u30fc\u30c9\u306b\u3053\u3046\u3044\u3063\u305f\u5fae\u5999\u306a\u52d5\u4f5c\u304c\u4f7f\u308f\u308c\u3066\u3044\u306a\u3044\u3053\u3068\u3001\u307e\u305f\u3053\u308c\u306f\u30d0\u30b0\u306e\u539f\u56e0\u3060\u3063\u305f\u3068\u60f3\u5b9a\u3057\u3001 IDE \u304c\u63d0\u4f9b\u3059\u308b\u81ea\u52d5\u7f6e\u63db\u3067\u306f\u3001\u30bb\u30de\u30f3\u30c6\u30a3\u30af\u30b9\u3092\u5358\u7d14\u5316\u3057\u3066\u3044\u307e\u3059\u3002 \u3053\u306e\u30b1\u30fc\u30b9\u304c\u5f53\u3066\u306f\u307e\u3089\u306a\u3044\u5834\u5408\u306f\u3001\u4f7f\u7528\u72b6\u6cc1\u3092\u78ba\u8a8d\u3057\u3001\u624b\u52d5\u3067\u66f4\u65b0\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u307e\u305f\u3001\u30af\u30ed\u30fc\u30ba\u3055\u308c\u305f\u30c1\u30e3\u30f3\u30cd\u30eb\u3092\u4f8b\u5916\u3092\u30b9\u30ed\u30fc\u305b\u305a\u306b\u5225\u306e\u65b9\u6cd5\u3067\u51e6\u7406\u3059\u308b\u5834\u5408\u306f\u3001\u5b8c\u5168\u306b\u66f8\u304d\u76f4\u3059\u3053\u3068\u3092\u691c\u8a0e\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1 id=\"reactive\">\u5b89\u5b9a\u5316\u3092\u76ee\u6307\u3057\u305f Reactive \u306e\u7d71\u5408<\/h1>\n<p>\u30d0\u30fc\u30b8\u30e7\u30f3 1.5 \u306e Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3\u3067\u306f\u3001Reactive \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u306e\u7d71\u5408\u306b\u95a2\u308f\u308b\u307b\u3068\u3093\u3069\u306e\u95a2\u6570\u3092 stable \u306a API \u306b\u6607\u683c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>JVM \u30a8\u30b3\u30b7\u30b9\u30c6\u30e0\u306b\u306f\u3001<a href=\"https:\/\/www.reactive-streams.org\/\" target=\"_blank\" rel=\"noopener\">Reactive Streams<\/a> \u6a19\u6e96\u306b\u6e96\u62e0\u3057\u3066\u975e\u540c\u671f\u30b9\u30c8\u30ea\u30fc\u30e0\u51e6\u7406\u3092\u6271\u3046\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u3053\u306e\u5206\u91ce\u3067\u6700\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u3066\u3044\u308b Java \u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306f\u3001<a href=\"https:\/\/projectreactor.io\/\" target=\"_blank\" rel=\"noopener\">Project Reactor<\/a> \u3068 <a href=\"https:\/\/github.com\/ReactiveX\/RxJava\" target=\"_blank\" rel=\"noopener\">RxJava<\/a> \u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/kotlinlang.org\/docs\/flow.html\" target=\"_blank\" rel=\"noopener\">Kotlin Flows<\/a> \u306f\u3053\u308c\u3089\u3068\u306f\u7570\u306a\u308a\u3001\u6a19\u6e96\u304c\u6307\u5b9a\u3059\u308b\u578b\u3068\u306f\u4e92\u63db\u6027\u304c\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u6982\u5ff5\u7684\u306b\u306f\u30b9\u30c8\u30ea\u30fc\u30e0\u3068\u540c\u7b49\u3067\u3059\u3002 \u305d\u306e\u305f\u3081\u3001<code>Flow<\/code> \u304b\u3089 Reactive\uff08\u4ed5\u69d8\u3068 <a href=\"https:\/\/github.com\/reactive-streams\/reactive-streams-jvm\/tree\/master\/tck\" target=\"_blank\" rel=\"noopener\">TCK<\/a> \u6e96\u62e0\uff09\u306e <code>Publisher<\/code> \u3078\u3001\u307e\u305f\u305d\u306e\u9006\u65b9\u5411\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002 \u3053\u306e\u3088\u3046\u306a\u5909\u63db\u306f\u3001<code>kotlinx.coroutines<\/code> \u3092\u4f7f\u3063\u3066\u3059\u3050\u306b\u5229\u7528\u3067\u304d\u3001\u5bfe\u5fdc\u3059\u308b Reactive \u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u542b\u307e\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305f\u3068\u3048\u3070\u3001Project Reactor \u578b\u306e\u904b\u7528\u4e92\u63db\u6027\u304c\u5fc5\u8981\u3067\u3042\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u4f9d\u5b58\u95a2\u4fc2\u3092\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">dependencies {          \n    implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-core:${coroutinesVersion}\")\n    implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-reactor:${coroutinesVersion}\")\n}\n<\/pre>\n<p>\u3059\u308b\u3068\u3001<em>Reactive Streams<\/em> \u578b\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f <code>Flow&lt;T&gt;.asPublisher()<\/code> \u3092\u3001Project Reactor \u578b\u3092\u76f4\u63a5\u4f7f\u7528\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u5834\u5408\u306f <code>Flow&lt;T&gt;.asFlux()<\/code> \u3092\u4f7f\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">\/\/ 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's Flux\nval flux = flow.asFlux()\n\n\/\/Convert back to Flow \nval anotherFlow = flux.asFlow()\n<\/pre>\n<p>\u4e0a\u8a18\u306f\u3001\u7c21\u5358\u306a\u307e\u3068\u3081\u3067\u3059\u3002 \u8a73\u7d30\u306b\u95a2\u5fc3\u306e\u3042\u308b\u65b9\u306f\u3001<a href=\"https:\/\/elizarov.medium.com\/reactive-streams-and-kotlin-flows-bfd12772cda4\" target=\"_blank\" rel=\"noopener\">Reactive Streams \u3068 Kotlin Flows \u306b\u95a2\u3059\u308b Roman Elizarov \u306e\u8a18\u4e8b<\/a>\u3092\u304a\u8aad\u307f\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>Reactive \u30e9\u30a4\u30d6\u30e9\u30ea\u3068\u306e\u7d71\u5408\u4f5c\u696d\u306f API \u306e stable \u5316\u3092\u30b4\u30fc\u30eb\u3068\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u53b3\u5bc6\u306b\u8a00\u3048\u3070\u3001<code>@ExperimentalCoroutinesApi<\/code> \u3092\u53d6\u308a\u9664\u304f\u3053\u3068\u3068\u3001\u6b8b\u308a\u306e\u69d8\u3005\u306a\u30c8\u30d4\u30c3\u30af\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3092\u76ee\u6a19\u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>Reactive Streams \u3068\u306e\u7d71\u5408\u306e\u6539\u5584<\/h2>\n<p>\u30b5\u30fc\u30c9\u30d1\u30fc\u30c6\u30a3\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068 Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3\u306e\u904b\u7528\u4e92\u63db\u6027\u3092\u78ba\u7acb\u3059\u308b\u306b\u306f\u3001Reactive Streams \u4ed5\u69d8\u3068\u306e\u4e92\u63db\u6027\u304c\u91cd\u8981\u3067\u3059\u3002 \u30b3\u30fc\u30c9\u3092\u307e\u308b\u3054\u3068\u66f8\u304d\u63db\u3048\u305a\u306b Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u30ec\u30ac\u30b7\u30fc\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u63a1\u7528\u3059\u308b\u4e0a\u3067\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u3001<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/commit\/47a063c0987177551bdbdf09a458998a30571ac2\" target=\"_blank\" rel=\"noopener\">\u975e\u5e38\u306b\u591a\u6570\u306e\u95a2\u6570<\/a>\u3092 stable \u30b9\u30c6\u30fc\u30bf\u30b9\u306b\u6607\u683c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002 \u3059\u3079\u3066\u306e Reactive Streams \u5b9f\u88c5\u304b\u3089 <code>Flow<\/code> \u306b\u578b\u3092\u5909\u63db\u3057\u3001\u305d\u306e\u9006\u306e\u5909\u63db\u3082\u53ef\u80fd\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002 \u305f\u3068\u3048\u3070\u3001\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u4f7f\u3063\u3066\u65b0\u3057\u3044\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3067\u304d\u307e\u3059\u304c\u3001\u9006\u5909\u63db\u306b\u3088\u3063\u3066\u53e4\u3044 Reactive \u30b3\u30fc\u30c9\u30d9\u30fc\u30b9\u306b\u7d71\u5408\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">fun 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<p>\u307e\u305f\u3001\u591a\u6570\u306e<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/pull\/2622\" target=\"_blank\" rel=\"noopener\">\u6539\u5584<\/a>\u304c <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> \u306b\u9069\u7528\u3055\u308c\u3066\u304a\u308a\u3001Reactor \u306e<a href=\"https:\/\/projectreactor.io\/docs\/core\/release\/reference\/#context\" target=\"_blank\" rel=\"noopener\">\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8<\/a>\u3092 <a href=\"https:\/\/kotlinlang.org\/docs\/coroutine-context-and-dispatchers.html\" target=\"_blank\" rel=\"noopener\">CoroutineContext<\/a> \u306b\u30e9\u30c3\u30d7\u3057\u3066\u3001Project Reactor \u3068 Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3\u9593\u306e\u30b7\u30fc\u30e0\u30ec\u30b9\u306a\u7d71\u5408\u3092\u5b9f\u73fe\u3057\u3066\u3044\u307e\u3059\u3002 \u3053\u306e\u7d71\u5408\u306b\u3088\u308a\u3001\u30b3\u30eb\u30fc\u30c1\u30f3\u3092\u4ecb\u3057\u3066 Reactor \u306e Context \u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u4f1d\u642c\u3067\u304d\u308b\u3088\u3046\u306b\u3082\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306f\u3001<code>Mono<\/code>\u3001<code>Flux<\/code>\u3001<code>Publisher.asFlow<\/code>\u3001<code>Flow.asPublisher<\/code>\u3001<code>Flow.asFlux<\/code> \u306a\u3069\u306e\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d0\u30fc\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u901a\u3058\u3066\u3059\u3079\u3066\u306e Reactive \u7d71\u5408\u306b\u3088\u3063\u3066\u660e\u793a\u7684\u306b\u4f1d\u642c\u3055\u308c\u307e\u3059\u3002 \u4ee5\u4e0b\u306b\u3001\u30b5\u30d6\u30b9\u30af\u30e9\u30a4\u30d0\u30fc\u306e <code>Context<\/code> \u3092 <code>ReactorContext<\/code> \u306b\u4f1d\u642c\u3059\u308b\u7c21\u5358\u306a\u4f8b\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">import 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(\"Reactor context in Flow: \" +\n          currentCoroutineContext()[ReactorContext]?.context)\n    }\n\n    \/\/ No context\n    \/\/ prints \"Reactor context in Flow: null\"\n    flow.asFlux().subscribe() \n\n    \/\/ Add subscriber's context\n    \/\/ prints \"Reactor context in Flow: Context1{answer=42}\"\n    flow.asFlux()\n        .contextWrite { ctx -&gt; ctx.put(\"answer\", 42) }\n        .subscribe() \n}\n<\/pre>\n<p>\u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001<code>Flow<\/code> \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u69cb\u7bc9\u3057\u3066\u3001Reactor \u306e Flux \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u7121\u3057\u3067\u5909\u63db\u3057\u3066\u3044\u307e\u3059\u3002 \u5f15\u6570\u3092\u4f7f\u308f\u305a\u306b <code>subscribe()<\/code> \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3068\u3001\u30d1\u30d6\u30ea\u30c3\u30b7\u30e3\u30fc\u306b<em>\u3059\u3079\u3066\u306e<\/em>\u306e\u30c7\u30fc\u30bf\u3092\u9001\u4fe1\u3059\u308b\u3088\u3046\u8981\u6c42\u3059\u308b\u52b9\u679c\u304c\u3042\u308a\u307e\u3059\u3002 \u305d\u306e\u305f\u3081\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u300c<em>Reactor context in Flow: null<\/em>\u300d\u3068\u3044\u3046\u30d5\u30ec\u30fc\u30ba\u3092\u51fa\u529b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306e\u547c\u3073\u51fa\u3057\u30c1\u30a7\u30fc\u30f3\u3082 <code>Flow<\/code> \u3092 <code>Flux<\/code> \u306b\u5909\u63db\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u30ad\u30fc\u5024\u30da\u30a2\u306e <em>answer=42<\/em> \u3092\u3053\u306e\u30c1\u30a7\u30fc\u30f3\u306e Reactor \u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306b\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\u3002 <code>subscribe()<\/code> \u3078\u306e\u547c\u3073\u51fa\u3057\u306b\u3088\u3063\u3066\u3053\u306e\u30c1\u30a7\u30fc\u30f3\u304c\u30c8\u30ea\u30ac\u30fc\u3055\u308c\u307e\u3059\u3002 \u3053\u306e\u5834\u5408\u3001\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u304c\u5165\u529b\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u300c<em>Reactor context in Flow: Context1{answer=42}<\/em>\u300d\u3068\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n<h2>\u65b0\u3057\u3044\u4fbf\u5229\u306a\u95a2\u6570<\/h2>\n<p>\u30b3\u30eb\u30fc\u30c1\u30f3\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3067 <code>Mono<\/code> \u306e\u3088\u3046\u306a Reactive \u578b\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3068\u304d\u306b\u30b9\u30ec\u30c3\u30c9\u3092\u30d6\u30ed\u30c3\u30af\u305b\u305a\u306b\u53d6\u5f97\u3067\u304d\u308b\u4fbf\u5229\u306a\u95a2\u6570\u304c\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3059\u3002 \u3053\u306e\u30ea\u30ea\u30fc\u30b9\u3067\u306f\u3001\u4efb\u610f\u306e <code>Publisher<\/code> \u306b\u304a\u3051\u308b <code>awaitSingleOr*<\/code> \u95a2\u6570\u3068\u3001<code>Mono<\/code> \u3068 <code>Maybe<\/code> \u306e\u7279\u6b8a\u306a\u4e00\u90e8\u306e <code>await*<\/code> \u95a2\u6570\u3092<a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/pull\/2628\" target=\"_blank\" rel=\"noopener\">\u975e\u63a8\u5968<\/a>\u306b\u6307\u5b9a\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><code>Mono<\/code> \u306f\u3001\u6700\u5927 1 \u3064\u306e\u5024\u3092\u751f\u6210\u3059\u308b\u305f\u3081\u3001\u6700\u5f8c\u306e\u8981\u7d20\u3068\u6700\u521d\u306e\u8981\u7d20\u304c\u540c\u4e00\u3068\u306a\u308a\u307e\u3059\u3002 \u3053\u306e\u5834\u5408\u3001\u6b8b\u308a\u306e\u8981\u7d20\u3092\u5207\u308a\u843d\u3068\u3059\u30bb\u30de\u30f3\u30c6\u30a3\u30af\u30b9\u304c\u4e0d\u8981\u306b\u306a\u308a\u307e\u3059\u3002 \u305d\u306e\u305f\u3081\u3001<code>Mono.awaitFirst()<\/code> \u3068 <code>Mono.awaitLast()<\/code> \u3092\u975e\u63a8\u5968\u306b\u3057\u3001<code>Mono.awaitSingle()<\/code> \u306b\u7f6e\u304d\u63db\u3048\u307e\u3057\u305f\u3002<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-142647\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/05\/cor15-reactive-streams-awaitFirst.png\" alt=\"\" width=\"1600\" height=\"906\" \/><\/figure>\n<h1 id=\"use-coroutines-1-5-0\">kotlinx.coroutines 1.5.0 \u3092\u4f7f\u3044\u59cb\u3081\u307e\u3057\u3087\u3046\uff01<\/h1>\n<p>\u65b0\u3057\u3044\u30ea\u30ea\u30fc\u30b9\u306b\u306f\u3001\u76ee\u899a\u307e\u3057\u3044\u5909\u66f4\u304c\u305f\u304f\u3055\u3093\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002 Channel API \u3092\u6539\u5584\u3057\u3066\u3044\u308b\u969b\u306b\u4f5c\u6210\u3055\u308c\u305f\u65b0\u3057\u3044\u540d\u524d\u4ed8\u3051\u30b9\u30ad\u30fc\u30e0\u306f\u3001\u30c1\u30fc\u30e0\u306b\u3088\u308b\u7279\u7b46\u306b\u5024\u3059\u308b\u9054\u6210\u3067\u3059\u3002 \u4e00\u65b9\u3001\u30b3\u30eb\u30fc\u30c1\u30f3 API \u3092\u3067\u304d\u308b\u9650\u308a\u5358\u7d14\u3067\u76f4\u611f\u7684\u306a API \u306b\u3059\u308b\u3068\u3044\u3046\u5927\u304d\u306a\u72d9\u3044\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3\u306e\u65b0\u3057\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u4f7f\u3044\u59cb\u3081\u308b\u306b\u306f\u3001build.gradle.kts \u30d5\u30a1\u30a4\u30eb\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u66f4\u65b0\u3057\u3066\u304f\u3060\u3055\u3044\u3002 \u6700\u521d\u306b\u3001\u6700\u65b0\u30d0\u30fc\u30b8\u30e7\u30f3\u306e Kotlin Gradle \u30d7\u30e9\u30b0\u30a4\u30f3\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">plugins {\n   kotlin(\"jvm\") version \"1.5.0\"\n}\n<\/pre>\n<p>\u6b21\u306b\u3001Reactive Streams \u3068\u306e\u7279\u5b9a\u306e\u7d71\u5408\u6a5f\u80fd\u3092\u542b\u3080\u30e9\u30a4\u30d6\u30e9\u30ea\u306a\u3069\u3001\u4f9d\u5b58\u95a2\u4fc2\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u66f4\u65b0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"kotlin-code\" style=\"visibility: hidden; padding: 36px 0;\" data-highlight-only=\"true\">val coroutinesVersion = \"1.5.0\"\n\ndependencies { \n  implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:$coroutinesVersion\")\n  implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$coroutinesVersion\")\n  implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-reactor:$coroutinesVersion\")\n  implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-rx3:$coroutinesVersion\")\n  ...\n}\n<\/pre>\n<h1>\u52d5\u753b\u3068\u305d\u306e\u4ed6\u306e\u60c5\u5831<\/h1>\n<ul>\n<li><a class=\"ek-link\" href=\"https:\/\/youtu.be\/EVLnWOcR0is\" target=\"_blank\" rel=\"noopener\">Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3 1.5.0 \u306b\u95a2\u3059\u308b\u52d5\u753b<\/a><\/li>\n<li><a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-guide.html\" target=\"_blank\" rel=\"noopener\">\u30b3\u30eb\u30fc\u30c1\u30f3\u30ac\u30a4\u30c9<\/a><\/li>\n<li><a href=\"https:\/\/kotlin.github.io\/kotlinx.coroutines\/\" target=\"_blank\" rel=\"noopener\">API \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\" target=\"_blank\" rel=\"noopener\">Kotlin \u30b3\u30eb\u30fc\u30c1\u30f3\u306e GitHub \u30ea\u30dd\u30b8\u30c8\u30ea<\/a><\/li>\n<li><a class=\"ek-link\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2020\/10\/kotlinx-coroutines-1-4-0-introducing-stateflow-and-sharedflow\/\">\u30b3\u30eb\u30fc\u30c1\u30f3 1.4.0 \u306b\u95a2\u3059\u308b\u30d6\u30ed\u30b0\u8a18\u4e8b<\/a><\/li>\n<li><a class=\"ek-link\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2021\/05\/kotlin-1-5-0-released\/\">Kotlin 1.5.0 \u306b\u95a2\u3059\u308b\u30d6\u30ed\u30b0\u8a18\u4e8b<\/a><\/li>\n<\/ul>\n<h3>\u554f\u984c\u306b\u76f4\u9762\u3057\u305f\u5834\u5408<\/h3>\n<ul>\n<li>\u8ab2\u984c\u3092 <a href=\"https:\/\/github.com\/Kotlin\/kotlinx.coroutines\/issues\" target=\"_blank\" rel=\"noopener\">GitHub \u306e\u8ab2\u984c\u30c8\u30e9\u30c3\u30ab\u30fc<\/a>\u306b\u5831\u544a\u3059\u308b\u3002<\/li>\n<li>Kotlin Slack \u306e #coroutines \u30c1\u30e3\u30f3\u30cd\u30eb\u3067\u30d8\u30eb\u30d7\u3092\u6c42\u3081\u308b\uff08<a href=\"https:\/\/surveys.jetbrains.com\/s3\/kotlin-slack-sign-up\" target=\"_blank\" rel=\"noopener\">\u62db\u5f85\u3092\u53d7\u3051\u308b<\/a>\uff09\u3002<\/li>\n<\/ul>\n","protected":false},"author":811,"featured_media":142995,"comment_status":"closed","ping_status":"closed","template":"","categories":[909],"tags":[671],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/kotlin\/151652"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/kotlin"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/types\/kotlin"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/users\/811"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/comments?post=151652"}],"version-history":[{"count":10,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/kotlin\/151652\/revisions"}],"predecessor-version":[{"id":151665,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/kotlin\/151652\/revisions\/151665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/media\/142995"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/media?parent=151652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/categories?post=151652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/tags?post=151652"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ja\/wp-json\/wp\/v2\/cross-post-tag?post=151652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}