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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.62