官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 永昌县| 迁安市| 陇西县| 偏关县| 筠连县| 漠河县| 高青县| 邵武市| 太谷县| 颍上县| 德化县| 仁寿县| 六枝特区| 屏东市| 清水河县| 大余县| 梧州市| 福鼎市| 威远县| 屯昌县| 福鼎市| 德惠市| 叶城县| 百色市| 玉山县| 大名县| 阿拉善右旗| 吉林省| 元阳县| 五河县| 吴堡县| 章丘市| 温宿县| 榆社县| 广南县| 定襄县| 瑞金市| 宜兰市| 柳林县| 垦利县| 翁源县|