{"id":10279,"date":"2020-09-20T09:09:28","date_gmt":"2020-09-20T00:09:28","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=10279"},"modified":"2022-07-30T15:27:56","modified_gmt":"2022-07-30T06:27:56","slug":"%ec%98%88%ec%a0%9c%eb%a1%9c-%ec%a0%95%eb%a6%ac%ed%95%98%eb%8a%94-%ec%bd%94%ed%8b%80%eb%a6%b0%ec%9d%98-%ec%bd%94%eb%a1%9c%ed%8b%b4kotlin-coroutine","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=10279","title":{"rendered":"\uc608\uc81c\ub85c \uc815\ub9ac\ud558\ub294 \ucf54\ud2c0\ub9b0\uc758 \ucf54\ub8e8\ud2f4(Kotlin Coroutine)"},"content":{"rendered":"<p>\ucf54\ub8e8\ud2f4\uc740 \uc2a4\ub808\ub4dc\uc640 \uae30\ub2a5\uc801\uc73c\ub85c \uac19\uc9c0\ub9cc, \uc2a4\ub808\ub4dc\uc5d0 \ube44\uad50\ud558\uba74 \uc880\ub354 \uac00\ubccd\uace0 \uc720\uc5f0\ud558\uba70 \ud55c\ub2e8\uacc4 \ub354 \uc9c4\ud654\ub41c \ubcd1\ub82c \ud504\ub85c\uadf8\ub798\ubc0d\uc744 \uc704\ud55c \uae30\uc220\uc785\ub2c8\ub2e4. \ud558\ub098\uc758 \uc2a4\ub808\ub4dc \ub0b4\uc5d0\uc11c \uc5ec\ub7ec\uac1c\uc758 \ucf54\ub8e8\ud2f4\uc774 \uc2e4\ud589\ub418\ub294 \uac1c\ub150\uc778\ub370, \uc544\ub798\uc758 \ucf54\ub4dc\ub294 \ub3d9\uc77c\ud55c \uae30\ub2a5\uc744 \uc2a4\ub808\ub4dc\uc640 \ucf54\ub8e8\ud2f4\uc73c\ub85c \uac01\uac01 \uad6c\ud604\ud55c \ucf54\ub4dc\uc758 \uc608\uc2dc\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nThread(Runnable {\r\n    for(i in 1..10) {\r\n        Thread.sleep(1000L)\r\n        print(\"I'm working in Thread.\")\r\n    }\r\n}).start()\r\n\r\nGlobalScope.launch() {\r\n    repeat(10) {\r\n        delay(1000L)\r\n        print(\"I'm working in Coroutine.\")\r\n    }\r\n}\r\n<\/pre>\n<p>\uc544\ub798\ub294 \ucf54\ub8e8\ud2f4\uc5d0 \ub300\ud574\uc11c \ucd08\uc810\uc744 \ub9de\ucdb0\uc11c \uac00\uc7a5 \uac04\ub2e8\ud55c \ucf54\ub8e8\ud2f4\uc758 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nprint(\"Start Main Thread\")\r\n\r\nGlobalScope.launch {\r\n    delay(3000)\r\n    print(\"in Coroutine ...\")\r\n}\r\n\r\nprint(\"End Main Thread\")\r\n<\/pre>\n<p>\ucf54\ub8e8\ud2f4\uc740 GlobalScope.launch\ub85c \uc815\uc758\ub418\uba70 { .. } \uc73c\ub85c \ubb36\uc740 \ucf54\ub4dc\uac00 \ube44\ub3d9\uae30\uc801\uc73c\ub85c \uc2e4\ud589\ub429\ub2c8\ub2e4. \uc2e4\ud589 \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: Start Main Thread\r\nV: End Main Thread\r\nV: in Coroutine ...\r\n<\/pre>\n<p>\ub2e4\uc74c\uc740 \ube44\ub3d9\uae30\uc801\uc73c\ub85c \uc2e4\ud589\ub41c \ucf54\ub8e8\ud2f4\uc774 \uc644\ub8cc\ub418\uc5b4 \uadf8 \uacb0\uacfc\ub97c \ubc18\ud658\ubc1b\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nGlobalScope.launch {\r\n    launch {\r\n        print(\"At Here!\")\r\n    }\r\n\r\n    val value: Int = async {\r\n        var total = 0\r\n        for (i in 1..10) total += i\r\n        total\r\n    }.await()\r\n\r\n    print(\"$value\")\r\n}\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: At Here!\r\nV: 55\r\n<\/pre>\n<p>\ub2e4\uc74c \ucf54\ub4dc \uc5ed\uc2dc \ube44\ub3d9\uae30\uc801\uc73c\ub85c \uc2e4\ud589\ub41c \ucf54\ub8e8\ud2f4\uc758 \uc644\ub8cc\ub97c \uae30\ub2e4\ub9ac\uace0 \uadf8 \uacb0\uacfc\ub97c \ubc18\ud658\ubc1b\uc544 \ucd9c\ub825\ud558\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nGlobalScope.launch {\r\n    val x = doSomething()\r\n    print(\"done something, $x\")\r\n}\r\n\r\nprivate suspend fun doSomething():Int {\r\n    val value: Int = GlobalScope.async(Dispatchers.IO) {\r\n        var total = 0\r\n        for (i in 1..10) total += i\r\n        print(\"do something in a suspend method: $total\")\r\n        total\r\n    }.await()\r\n\r\n    return value\r\n}\r\n<\/pre>\n<p>\ube44\ub3d9\uae30\uc801\uc73c\ub85c \uc2e4\ud589\ub418\ub294 \ucf54\ub8e8\ud2f4\uc744 \ubcc4\ub3c4\uc758 \ud568\uc218\ub85c \ubd84\ub9ac\ud588\ub294\ub370, \ucf54\ub8e8\ud2f4 \ub0b4\ubd80\uc5d0\uc11c \uc2e4\ud589\ub418\ub294 \ud568\uc218\ub294 suspend\ub85c \uc9c0\uc815\ud574\uc57c \ud569\ub2c8\ub2e4. \uc704\uc758 \ucf54\ub4dc\uc758 \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: do something in a suspend method: 55\r\nV: done something, 55\r\n<\/pre>\n<p>\uc774\ubc88\uc5d0\ub294 2\uac1c\uc758 \ucf54\ub8e8\ud2f4\uc744 \uc2e4\ud589\ud558\uace0 \uc774 2\uac1c\uc758 \uacb0\uacfc\ub97c \ubc1b\uc544 \ucd9c\ub825\ud558\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nprint(\"Start...\")\r\nGlobalScope.launch(Dispatchers.Main) {\r\n    val job1 = async(Dispatchers.IO) {\r\n        var total = 0\r\n        for (i in 1..10) {\r\n            total += i\r\n            delay(100)\r\n        }\r\n        print(\"job1\")\r\n        total\r\n    }\r\n\r\n    val job2 = async(Dispatchers.Main) {\r\n        var total = 0\r\n        for (i in 1..10) {\r\n            delay(100)\r\n            total += i\r\n        }\r\n        print(\"job2\")\r\n        total\r\n    }\r\n\r\n    val result1 = job1.await()\r\n    val result2 = job2.await()\r\n\r\n    print(\"results are $result1 and $result2\")\r\n}\r\nprint(\"End.\")\r\n<\/pre>\n<p>\uc704\uc758 \ucf54\ub4dc\uc5d0\uc11c \ubcfc \uc218 \uc788\ub294 Dispatchers.Main, Dispatchers.IO\ub294 \uac01\uac01 UI \ubcc0\uacbd \ub4f1\uc744 \ucc98\ub9ac\ud558\ub294 \uba54\uc778 \uc2a4\ub808\ub4dc \uadf8\ub9ac\uace0 \uc785\ucd9c\ub825 \uc5f0\uc0b0\uc744 \ucc98\ub9ac\ud558\uae30\uc5d0 \uc801\ud569\ud55c IO \uc2a4\ub808\ub4dc\ub97c \uc758\ubbf8\ud558\uba70, \ucf54\ub8e8\ud2f4\ub4e4\uc740 \uc774\ucc98\ub7fc \uc9c0\uc815\ub41c \uc2a4\ub808\ub4dc \ub0b4\uc5d0\uc11c \uc2e4\ud589\ub429\ub2c8\ub2e4. \uc704 \ucf54\ub4dc\uc758 \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: Start...\r\nV: End.\r\nV: job1\r\nV: job2\r\nV: results are 55 and 55\r\n<\/pre>\n<p>\ub2e4\uc74c\uc740 \ucf54\ub8e8\ud2f4\uc774 \uc644\ub8cc\ub97c \uae30\ub2e4\ub9ac\uae30 \uc704\ud55c await \ud638\ucd9c\uc744 \uc0ac\uc6a9\ud558\uc9c0 \uc54a\ub294 \ub610\ub2e4\ub978 \ubc29\ubc95\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nGlobalScope.launch(Dispatchers.IO) {\r\n    val v = withContext(Dispatchers.Main) {\r\n        var total = 0\r\n        for (i in 1..10) {\r\n            delay(100)\r\n            total += i\r\n        }\r\n\r\n        total\r\n    }\r\n\r\n    print(\"result: $v\")\r\n    print(\"Do something in IO thread\")\r\n}\r\n<\/pre>\n<p>withContext\ub97c \uc368\uc11c \uc0c8\ub85c\uc6b4 \ucf54\ub8e8\ud2f4\uc744 \ub2e4\ub978 \uc2a4\ub808\ub4dc\uc5d0\uc11c \ub3d9\uae30\uc801\uc73c\ub85c \uc2e4\ud589\ud558\ub3c4\ub85d \ud558\ub294 \ucf54\ub4dc\uc785\ub2c8\ub2e4. \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: result: 55\r\nV: Do something in IO thread\r\n<\/pre>\n<p>launch\ub294 Job \uac1d\uccb4\ub97c \ubc18\ud658\ud558\ub294\ub370, \uc774\ub97c \ud1b5\ud574 \ub2e4\uc74c \uc608\uc81c\ucc98\ub7fc \ucf54\ub8e8\ud2f4\uc744 \uc911\uac04\uc5d0 \uc911\ub2e8\uc2dc\ud0ac \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nprint(\"start..\")\r\n\r\nval job = GlobalScope.launch() {\r\n    repeat(10) {\r\n        delay(1000L)\r\n        print(\"I'm working.\")\r\n    }\r\n}\r\n\r\nrunBlocking {\r\n    delay(3000L)\r\n    job.cancel()\r\n}\r\n\r\nprint(\"stop\")\r\n<\/pre>\n<p>\uc2e4\ud589 \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: start..\r\nV: I'm working.\r\nV: I'm working.\r\nV: stop\r\n<\/pre>\n<p>\uc774\ubc88\uc5d0\ub294 Job \uac1d\uccb4\ub97c \ud1b5\ud574 \ucf54\ub8e8\ud2f4\uc774 \uc644\uc804\uc774 \uc885\ub8cc\ub420\ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9ac\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nprint(\"start..\")\r\n\r\nval job = GlobalScope.launch() {\r\n    repeat(10) {\r\n        delay(1000L)\r\n        print(\"I'm working.\")\r\n    }\r\n}\r\n\r\nrunBlocking {\r\n    job.join()\r\n}\r\n\r\nprint(\"stop\")\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: start..\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\nV: stop\r\n<\/pre>\n<p>\ucf54\ub8e8\ud2f4\uc740 \uc815\ud574\uc9c4 \uc2dc\uac04\uc774 \ub418\uba74 \ucf54\ub8e8\ud2f4\uc758 \uc644\ub8cc\ub418\uc9c0 \ubabb\ud560\uc9c0\ub77c\ub3c4 \uc911\uc9c0\ud558\uac8c \ud560 \uc218 \uc788\ub294\ub370, \uc544\ub798\uc758 \ucf54\ub4dc\uac00 \ubc14\ub85c \uadf8 \uc608\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nprint(\"start\")\r\n\r\nval job = GlobalScope.launch {\r\n    withTimeout(4000L) {\r\n        repeat(10) {\r\n            delay(1000L)\r\n            print(\"I'm working.\")\r\n        }\r\n    }\r\n}\r\n\r\nprint(\"end\")\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: start\r\nV: end\r\nV: I'm working.\r\nV: I'm working.\r\nV: I'm working.\r\n<\/pre>\n<p>\ucf54\ub8e8\ud2f4\uc740 \ucc44\ub110(Channel)\uc774\ub77c\ub294 \uac1c\ub150\uc744 \ud1b5\ud574 \ucf54\ub8e8\ud2f4\uc5d0\uc11c \uc0dd\uc131\ud55c \ub370\uc774\ud130\ub97c \ub610 \ub2e4\ub978 \ucf54\ub8e8\ud2f4\uc5d0\uac8c \uc804\ub2ec\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc544\ub798\uc758 \ucf54\ub4dc\ub294 \ucf54\ub8e8\ud2f4\uc5d0\uc11c 1~5\uae4c\uc9c0\uc758 \uc815\uc218\uc5d0 \ub300\ud55c \uc81c\uacf1\uac12\uc744 \uc0dd\uc131\ud558\uba74 \uc0dd\uc131\ub41c \uc815\uc218 4\uac1c\ub97c \ub610 \ub2e4\ub978 \ucf54\ub8e8\ud2f4\uc5d0\uc11c \ubc1b\uc544 \ucd9c\ub825\ud558\ub294 \uc608\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nrunBlocking {\r\n    print(\"start\")\r\n\r\n    val channel = Channel&lt;Int>()\r\n\r\n    launch {\r\n        for (x in 1..5) {\r\n            channel.send(x * x)\r\n        }\r\n    }\r\n\r\n    repeat(5) {\r\n        val v = channel.receive()\r\n        print(\"$v\")\r\n    }\r\n\r\n    print(\"end\")\r\n}\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: start\r\nV: 1\r\nV: 4\r\nV: 9\r\nV: 16\r\nV: 25\r\nV: end\r\n<\/pre>\n<p>\ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \ucabd\uc774\ub098 \ubc1b\ub294 \ucabd\uc5d0\uc11c\ub294 \uc5bc\ub9c8\ub098 \ub9ce\uc740 \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud560\uc9c0 \ub610\ub294 \ubc1b\uc744\uc9c0\ub97c \uc608\uce21\ud560 \uc218 \uc5c6\ub294 \uacbd\uc6b0\uac00 \ub300\ubd80\ubd84\uc785\ub2c8\ub2e4. \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \ucabd\uc5d0\uc11c \ucc44\ub110\uc758 close \ud568\uc218\ub97c \ud638\ucd9c\ud558\uba74 \ubc1b\ub294\ucabd\uc5d0\uc11c \ub354 \uc774\uc0c1 \ub370\uc774\ud130\uac00 \uc5c6\ub2e4\ub294 \uac83\uc744 \uc778\uc9c0\ud558\uac8c \ub418\ub294\ub370, \uc544\ub798\ub294 \uc774\uc5d0 \ub300\ud55c \ucf54\ub4dc \uc608\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nrunBlocking {\r\n    print(\"start\")\r\n\r\n    val channel = Channel&lt;Int>()\r\n\r\n    launch {\r\n        for(x in 1..5) channel.send(x*x)\r\n        channel.close()\r\n    }\r\n\r\n    for(y in channel) print(\"$y\")\r\n\r\n    print(\"end\")\r\n}\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: start\r\nV: 1\r\nV: 4\r\nV: 9\r\nV: 16\r\nV: 25\r\nV: end\r\n<\/pre>\n<p>\ub2e4\uc74c\uc740 \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \ucf54\ub8e8\ud2f4\uc744 \ud568\uc218\ud654\ud558\uc5ec \uc774 \ud568\uc218\ub97c \ud1b5\ud574 \uc0dd\uc131\ub41c \ub370\uc774\ud130\ub97c \ucc98\ub9ac\ud558\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nrunBlocking {\r\n    print(\"start\")\r\n\r\n    val squares = procedureSquares()\r\n    squares.consumeEach {\r\n        print(\"$it\")\r\n    }\r\n\r\n    print(\"end\")\r\n}\r\n\r\nprivate fun CoroutineScope.procedureSquares(): ReceiveChannel&lt;Int> = produce {\r\n    for(x in 1..5) send(x*x)\r\n}\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: start\r\nV: 1\r\nV: 4\r\nV: 9\r\nV: 16\r\nV: 25\r\nV: end\r\n<\/pre>\n<p>\ub2e4\uc74c\uc740 \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \ucf54\ub8e8\ud2f4\uc744 \ud30c\uc774\ud504\ub77c\uc778 \ud615\ud0dc\ub85c \ubb36\uc5b4 \ucc98\ub9ac\ud558\ub294 \uac83\uc73c\ub85c, \uccab\ubc88\uc9f8 \ucf54\ub8e8\ud2f4\uc5d0\uc11c \uc0dd\uc131\ud55c \uac12\uc744 \ub610 \ub2e4\ub978 \ucf54\ub8e8\ud2f4\uc5d0\uc11c \ubc1b\uc544 \ucc98\ub9ac\ud558\uc5ec \ub610 \ub2e4\ub978 \ucf54\ub8e8\ud2f4\uc73c\ub85c \uc804\ub2ec\ud558\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nrunBlocking {\r\n    print(\"start\")\r\n\r\n    val numbers = productNumbers()\r\n    val squares = squares(numbers)\r\n\r\n    for(i in 1..5) print(\"${squares.receive()}\")\r\n\r\n    print(\"end\")\r\n\r\n    coroutineContext.cancelChildren()\r\n}\r\n\r\nprivate fun CoroutineScope.productNumbers() = produce&lt;Int> {\r\n    var x = 1\r\n    while(true) {\r\n        print(\"send ${x} on productNumbers\")\r\n        send(x++)\r\n        delay(100)\r\n    }\r\n}\r\n\r\nprivate fun CoroutineScope.squares(numbers:ReceiveChannel&lt;Int>): ReceiveChannel&lt;Int> = produce {\r\n    for(x in numbers) {\r\n        print(\"send ${x} on squares\")\r\n        send(x*x)\r\n    }\r\n}\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: start\r\nV: send 1 on productNumbers\r\nV: send 1 on squares\r\nV: 1\r\nV: send 2 on productNumbers\r\nV: send 2 on squares\r\nV: 4\r\nV: send 3 on productNumbers\r\nV: send 3 on squares\r\nV: 9\r\nV: send 4 on productNumbers\r\nV: send 4 on squares\r\nV: 16\r\nV: send 5 on productNumbers\r\nV: send 5 on squares\r\nV: 25\r\nV: end\r\n<\/pre>\n<p>\ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \ucf54\ub8e8\ud2f4\uc740 1\uac1c\uc9c0\ub9cc, \uc774\ub97c \uc6d0\ud65c\ud558\uac8c \ucc98\ub9ac\ud558\uae30 \uc704\ud574 \uc5ec\ub7ec\uac1c\uc758 \ucf54\ub8e8\ud2f4\uc73c\ub85c \uc0dd\uc131\ub41c \ub370\uc774\ud130\ub97c \ucc98\ub9ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. \uc544\ub798\ub294 \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \ucf54\ub8e8\ud2f4 1\uac1c\uc640 \uc0dd\uc131\ub41c \ub370\uc774\ud130\ub97c \ucc98\ub9ac\ud558\ub294 5\uac1c\uc758 \ucf54\ub8e8\ud2f4\uc5d0 \ub300\ud55c \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nrunBlocking {\r\n    val producer = productNumbers()\r\n    repeat(5) {\r\n        launchProcessor(it, producer)\r\n\r\n    }\r\n\r\n    delay(1000L)\r\n    producer.cancel()\r\n}\r\n\r\nfun CoroutineScope.launchProcessor(id:Int, channel: ReceiveChannel&lt;Int>) {\r\n    launch {\r\n        for(msg in channel) {\r\n            print(\"Processor #$id received $msg\")\r\n        }\r\n    }\r\n}\r\n\r\nprivate fun CoroutineScope.productNumbers() = produce&lt;Int> {\r\n    var x = 1\r\n    while(true) {\r\n        print(\"send ${x} on productNumbers\")\r\n        send(x++)\r\n        delay(100)\r\n    }\r\n}\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: send 1 on productNumbers\r\nV: Processor #0 received 1\r\nV: send 2 on productNumbers\r\nV: Processor #0 received 2\r\nV: send 3 on productNumbers\r\nV: Processor #1 received 3\r\nV: send 4 on productNumbers\r\nV: Processor #2 received 4\r\nV: send 5 on productNumbers\r\nV: Processor #3 received 5\r\nV: send 6 on productNumbers\r\nV: Processor #4 received 6\r\nV: send 7 on productNumbers\r\nV: Processor #0 received 7\r\nV: send 8 on productNumbers\r\nV: Processor #1 received 8\r\nV: send 9 on productNumbers\r\nV: Processor #2 received 9\r\nV: send 10 on productNumbers\r\nV: Processor #3 received 10\r\n<\/pre>\n<p>\ubc18\ub300\ub85c \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \ucf54\ub8e8\ud2f4\uc740 \uc5ec\ub7ec\uac1c\uc774\uace0 \ucc98\ub9ac\ud558\ub294 \ucf54\ub8e8\ud2f4\uc740 1\uac1c\uc778 \uacbd\uc6b0\ub3c4 \uc788\uc2b5\ub2c8\ub2e4. \uc544\ub798\ub294 \ub370\uc774\ud130\ub97c \uc0dd\uc131\ud558\ub294 \ucf54\ub8e8\ud2f4 2\uac1c\uc640 \uc0dd\uc131\ub41c \ub370\uc774\ud130\ub97c \ucc98\ub9ac\ud558\ub294 \ucf54\ub8e8\ud2f4 1\uac1c\uc5d0 \ub300\ud55c \uc608\uc81c\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nrunBlocking {\r\n    val channel = Channel&lt;String>()\r\n    launch {\r\n        sendString(channel, \"foo\", 200L)\r\n    }\r\n\r\n    launch {\r\n        sendString(channel, \"BAR\", 500L)\r\n    }\r\n\r\n    repeat(6) {\r\n        print(\"${channel.receive()}\")\r\n    }\r\n\r\n    coroutineContext.cancelChildren()\r\n}\r\n\r\nprivate suspend fun sendString(channel: SendChannel&lt;String>, s:String, time:Long) {\r\n    while(true) {\r\n        delay(time)\r\n        channel.send(s)\r\n    }\r\n}\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: foo\r\nV: foo\r\nV: BAR\r\nV: foo\r\nV: foo\r\nV: BAR\r\n<\/pre>\n<p>\uc544\ub798\uc758 \uc608\uc81c\ub294 2\uac1c\uc758 \ucf54\ub8e8\ud2f4\uc5d0\uc11c \ud558\ub098\uc758 \ub370\uc774\ud130\uc5d0 \ub300\ud574 \uc5b4\ub5a4 \ucc98\ub9ac\ub97c \ud574\uc11c \uc8fc\uace0 \ubc1b\ub294 \uae30\ub2a5\uc5d0 \ub300\ud55c \ucf54\ub4dc\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\r\nprint(\"start\")\r\ndata class Ball(var hits:Int)\r\n\r\nsuspend fun player(name:String, table: Channel<Ball>) {\r\n    for(ball in table) {\r\n        ball.hits++\r\n        print(\"$name $ball\")\r\n        delay(300)\r\n        table.send(ball)\r\n    }\r\n}\r\n\r\nrunBlocking {\r\n    var table = Channel&lt;Ball>()\r\n\r\n    launch {\r\n        player(\"ping\", table)\r\n    }\r\n\r\n    launch {\r\n        player(\"pong\", table)\r\n    }\r\n\r\n    table.send(Ball(0))\r\n    delay(1000)\r\n    coroutineContext.cancelChildren()\r\n}\r\nprint(\"end\")\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class='code'>\r\nV: start\r\nV: ping Ball(hits=1)\r\nV: pong Ball(hits=2)\r\nV: ping Ball(hits=3)\r\nV: pong Ball(hits=4)\r\nV: end\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ucf54\ub8e8\ud2f4\uc740 \uc2a4\ub808\ub4dc\uc640 \uae30\ub2a5\uc801\uc73c\ub85c \uac19\uc9c0\ub9cc, \uc2a4\ub808\ub4dc\uc5d0 \ube44\uad50\ud558\uba74 \uc880\ub354 \uac00\ubccd\uace0 \uc720\uc5f0\ud558\uba70 \ud55c\ub2e8\uacc4 \ub354 \uc9c4\ud654\ub41c \ubcd1\ub82c \ud504\ub85c\uadf8\ub798\ubc0d\uc744 \uc704\ud55c \uae30\uc220\uc785\ub2c8\ub2e4. \ud558\ub098\uc758 \uc2a4\ub808\ub4dc \ub0b4\uc5d0\uc11c \uc5ec\ub7ec\uac1c\uc758 \ucf54\ub8e8\ud2f4\uc774 \uc2e4\ud589\ub418\ub294 \uac1c\ub150\uc778\ub370, \uc544\ub798\uc758 \ucf54\ub4dc\ub294 \ub3d9\uc77c\ud55c \uae30\ub2a5\uc744 \uc2a4\ub808\ub4dc\uc640 \ucf54\ub8e8\ud2f4\uc73c\ub85c \uac01\uac01 \uad6c\ud604\ud55c \ucf54\ub4dc\uc758 \uc608\uc2dc\uc785\ub2c8\ub2e4. Thread(Runnable { for(i in 1..10) { Thread.sleep(1000L) print(&#8220;I&#8217;m working in Thread.&#8221;) } }).start() GlobalScope.launch() { repeat(10) { delay(1000L) print(&#8220;I&#8217;m working in Coroutine.&#8221;) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=10279\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;\uc608\uc81c\ub85c \uc815\ub9ac\ud558\ub294 \ucf54\ud2c0\ub9b0\uc758 \ucf54\ub8e8\ud2f4(Kotlin Coroutine)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61,1],"tags":[],"class_list":["post-10279","post","type-post","status-publish","format-standard","hentry","category-android","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10279"}],"version-history":[{"count":9,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10279\/revisions"}],"predecessor-version":[{"id":10289,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/10279\/revisions\/10289"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10279"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}