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

Coroutines

Path breaking and, probably, the most exciting feature in Kotlin are coroutines. They are a new way to write asynchronous, non-blocking code somewhere like the threads, but way more simple, efficient, and lightweight. Coroutines were added in Kotlin 1.1 and are still experimental, so think before using it in production.

In the later chapters of this book, you'll learn about Schedulers in RxKotlin, which encapsulates the complexities of threading, but you can use it only in RxKotlin chain, while you can use coroutines anywhere and everywhere. That is indeed a path-breaking feature of Kotlin. They provide a great abstraction on threads, making context changes and concurrency easier.

Keep in mind that RxKotlin does not use coroutines yet; the reason is quite simple–both coroutines and Schedulers in RxKotlin share nearly the same internal architecture; while coroutines are new, Schedulers have been there for a long time with RxJava, RxJs, RxSwift, and more.

Coroutines are the best fit for developers to implement concurrency when they're not using/can't use RxKotlin Schedulers.

So, let's start by adding it to our project. If you are using Gradle, follow these steps (apply plugin could be 'kotlin' or 'kotlin-android', depending on whether you use it for JVM or Android):

    apply plugin: 'kotlin' 
 
    kotlin { 
      experimental { 
        coroutines 'enable' 
      } 
    } 

And then, we have to add the following dependency:

    repositories { 
      ... 
      jcenter() 
    } 
    dependencies { 
      ... 
      compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.16" 
    } 

If you are using Maven, then add the following code block in the pom.xml file:

    <plugin> 
      <groupId>org.jetbrains.kotlin</groupId> 
      <artifactId>kotlin-maven-plugin</artifactId> 
      ... 
      <configuration> 
        <args> 
            <arg>-Xcoroutines=enable</arg> 
        </args> 
      </configuration> 
    </plugin> 
    <repositories> 
      ... 
      <repository> 
        <id>central</id> 
        <url>http://jcenter.bintray.com</url> 
      </repository> 
    </repositories> 
    <dependencies> 
      ... 
      <dependency> 
        <groupId>org.jetbrains.kotlin</groupId> 
        <artifactId>kotlinx-coroutines-core</artifactId> 
        <version>0.16</version> 
      </dependency> 
    </dependencies> 

Apache Maven is a software project management and comprehension tool. Based on the concept of a Project Object Model (POM), Maven can manage a project's build, reporting, and documentation from a central piece of information. Please refer to the following URL for more information–https://maven.apache.org/.

So, what exactly is a coroutine? While developing applications, we often come into situations where we need to perform long running or time taking operations, such as network call, database operations, or some complex computations. The only option in Java is to use a thread to handle such situations, which is very complex itself. Whenever we face those situations, we feel the need for a simple yet powerful API to handle such cases. Developers from the .NET domain, especially those who used C# before, are familiar with the async/await operators; this is somehow the closest to Kotlin coroutines.

主站蜘蛛池模板: 武城县| 邹平县| 德庆县| 读书| 施甸县| 巴彦淖尔市| 平塘县| 隆子县| 特克斯县| 崇礼县| 孙吴县| 孝感市| 都兰县| 凌海市| 政和县| 洞头县| 鸡西市| 邯郸市| 武威市| 长武县| 牟定县| 福安市| 弋阳县| 长岛县| 手游| 冀州市| 华池县| 高密市| 贵定县| 梓潼县| 尤溪县| 武城县| 峨山| 江津市| 汝阳县| 阿勒泰市| 濮阳县| 宁蒗| 安丘市| 南郑县| 建平县|