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

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.

主站蜘蛛池模板: 屏边| 山西省| 靖江市| 莱芜市| 正蓝旗| 北流市| 开原市| 大足县| 阿克| 东平县| 永兴县| 西城区| 治县。| 会同县| 红原县| 泉州市| 扶余县| 胶州市| 登封市| 甘泉县| 双桥区| 江油市| 丰顺县| 永嘉县| 响水县| 清流县| 黄龙县| 连江县| 京山县| 光山县| 晋州市| 洛扎县| 周口市| 镇宁| 黄平县| 肥城市| 道真| 临湘市| 东港市| 江川县| 铜陵市|