- Reactive Programming in Kotlin
- Rivu Chakraborty
- 410字
- 2021-07-02 22:26:39
Getting started with coroutines
So, let's take the following example into consideration:
suspend fun longRunningTsk():Long {//(1) val time = measureTimeMillis {//(2) println("Please wait") delay(2,TimeUnit.SECONDS)//(3) println("Delay Over") } return time } fun main(args: Array<String>) { runBlocking {//(4) val exeTime = longRunningTsk()//(5) println("Execution Time is $exeTime") } }
We will inspect through the code, but let's first see the output:
Please wait Delay Over Execution Time is 2018
So, now, let's understand the code. On comment (1), while declaring the function, we mark the function with the suspend keyword, which is used to mark a function as suspending, that is, while executing the function the program should wait for its result; therefore, execution of suspending a function in main thread is not allowed (giving you a clear barrier between main thread and suspending functions). On comment (2), we started a block with measureTimeMillis and assigned its value to the (val) time variable. The job of measureInMillis is quite simple–it executes the block passed to it while measuring its execution time, and returns the same. We will use the delay function on comment (3) to intentionally delay the program execution by 2 seconds. The runBlocking block in the main function on comment (4) makes the program wait until the called longRunningTsk function on comment (5) completes. So, this was a quite simple example; however, we are making the main thread wait here. Sometimes, you will not want this; instead, you will want to do asynchronous operations. So, let's try to achieve this as well:
fun main(args: Array<String>) { val time = async(CommonPool) { longRunningTsk() }//(1) println("Print after async ") runBlocking { println("printing time ${time.await()}") }//(2) }
Here, we kept longRunningTsk same, just modified the main function. On comment (1), we assigned the time variable to the value of longRunningTsk inside the async block. The async block is quite interesting; it executes the code inside its block asynchronously on the coroutine context passed to it.
There are basically three types of coroutine contexts. Unconfined means it'll run on the main thread, CommonPool runs on the common thread pool, or you can create a new coroutine context as well.
On comment (2) we run a blocking code that will make the main function wait until the value of the time variable is available; the await function helps us accomplish this task–it tells the runBlocking block to wait until the async block completes execution to make the value of time available.
- 數據可視化:從小白到數據工程師的成長之路
- Learning JavaScriptMVC
- 數據化網站運營深度剖析
- Dependency Injection with AngularJS
- Python醫學數據分析入門
- Microsoft Power BI數據可視化與數據分析
- Scratch 3.0 藝術進階
- MATLAB Graphics and Data Visualization Cookbook
- 信息學競賽寶典:數據結構基礎
- 云數據中心網絡與SDN:技術架構與實現
- 數據科學實戰指南
- Instant Autodesk AutoCAD 2014 Customization with .NET
- 大數據與機器學習:實踐方法與行業案例
- Mastering ROS for Robotics Programming(Second Edition)
- 改進的群智能算法及其應用