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

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.

主站蜘蛛池模板: 宿州市| 平顶山市| 丰顺县| 桐柏县| 永宁县| 金山区| 隆尧县| 沙坪坝区| 虎林市| 营山县| 五莲县| 阜平县| 赤壁市| 靖远县| 桂林市| 丹寨县| 平遥县| 涞水县| 正定县| 泊头市| 会昌县| 唐河县| 赣榆县| 大连市| 绥化市| 社旗县| 漳浦县| 天全县| 德江县| 濮阳县| 大洼县| 台中市| 吉木萨尔县| 河北省| 永城市| 盐津县| 青神县| 胶南市| 四子王旗| 花莲县| 太康县|