- 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.
- Building Computer Vision Projects with OpenCV 4 and C++
- Access 2016數(shù)據(jù)庫(kù)教程(微課版·第2版)
- 數(shù)據(jù)庫(kù)基礎(chǔ)與應(yīng)用:Access 2010
- Spark大數(shù)據(jù)分析實(shí)戰(zhàn)
- 大數(shù)據(jù):規(guī)劃、實(shí)施、運(yùn)維
- 數(shù)據(jù)化網(wǎng)站運(yùn)營(yíng)深度剖析
- Learning Proxmox VE
- 數(shù)據(jù)庫(kù)原理與設(shè)計(jì)(第2版)
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第3版)
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- 數(shù)據(jù)庫(kù)查詢優(yōu)化器的藝術(shù):原理解析與SQL性能優(yōu)化
- 離線和實(shí)時(shí)大數(shù)據(jù)開發(fā)實(shí)戰(zhàn)
- 標(biāo)簽類目體系:面向業(yè)務(wù)的數(shù)據(jù)資產(chǎn)設(shè)計(jì)方法論
- Unity for Architectural Visualization
- Delphi High Performance